Last active
November 17, 2021 17:50
-
-
Save JamoCA/e153c2ea40bfd75b60d180fbb709fe5b to your computer and use it in GitHub Desktop.
isJsonStructure(): A CFML user-defined function (UDF) that returns true if the string is a JSON-serialized struct or array. 2021-08-20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// 2020-08-20 isJsonStructure(string, type=all); | |
// https://gist.github.com/JamoCA/e153c2ea40bfd75b60d180fbb709fe5b | |
public boolean function isJsonStructure(required string string, string type="any") output=false hint="I return true if the string is a JSON-serialized struct or array." { | |
if (not isJson(arguments.string)){ | |
return false; | |
} | |
local.parsed = deserializeJson(arguments.string); | |
if (not local.keyExists("parsed")){ | |
return false; | |
} | |
if (isStruct(local.parsed) and (not len(trim(arguments.type)) or listFindNoCase("any,struct,structure,object,record,dictionary,hashtable,hash table,keyed list,associative array", trim(arguments.type)))){ | |
return true; | |
} else if (isArray(local.parsed) and (not len(trim(arguments.type)) or listFindNoCase("any,array,vector,list,sequence", trim(arguments.type)))){ | |
return true; | |
} | |
return false; | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment