Created
August 27, 2015 10:37
-
-
Save roulupen/f7bc8f78c202de677706 to your computer and use it in GitHub Desktop.
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
<cffunction name="JSONUtil" access="private" returntype="string" output="false" hint="Serialize complex object into JSON"> | |
<cfargument name="obj" type="any" required="true" hint="Object which will be converted into JSON" /> | |
<cfset var returnValue = createObject("Java","java.lang.StringBuilder") /> | |
<cfset var currentMetaData = getMetaData(obj) /> | |
<cfif isStruct(currentMetaData) AND structKeyExists(currentMetaData, "TYPE") AND compareNoCase(currentMetaData.TYPE, "component") EQ 0> | |
<cfset returnValue.append("{") /> | |
<cfset var isNOTFirstElem = false /> | |
<cfif structKeyExists(currentMetaData, "properties")> | |
<cfloop array="#currentMetaData.properties#" index="property"> | |
<cfif isNOTFirstElem> | |
<cfset returnValue.append(",") /> | |
</cfif> | |
<cfset var tempObj = evaluate("obj." & "get#property.name#()") /> | |
<cfif IsSimpleValue( tempObj )> | |
<cfset returnValue.append("""#property.name#"":""#reReplace(tempObj, '"', '\"', 'ALL')#""") /> | |
<cfelse> | |
<cfset returnValue.append("""#property.name#"":#JSONUtil(tempObj)#") /> | |
</cfif> | |
<cfset isNOTFirstElem = true /> | |
</cfloop> | |
</cfif> | |
<cfset returnValue.append("}") /> | |
<cfelseif isArray(obj)> | |
<cfset returnValue.append("[") /> | |
<cfset var isNOTFirstElem = false /> | |
<cfloop array="#obj#" index="arrayIndex"> | |
<cfif isNOTFirstElem> | |
<cfset returnValue.append(",") /> | |
</cfif> | |
<cfif IsSimpleValue( arrayIndex )> | |
<cfset returnValue.append("""#reReplace(arrayIndex, '"', '\"', 'ALL')#""") /> | |
<cfelse> | |
<cfset returnValue.append("#JSONUtil(arrayIndex)#") /> | |
</cfif> | |
<cfset isNOTFirstElem = true /> | |
</cfloop> | |
<cfset returnValue.append("]") /> | |
<cfelseif isQuery(obj)> | |
<cfset returnValue.append("{ ""recordCount"": #obj.recordCount#, ""data"": [") /> | |
<cfloop query="obj"> | |
<cfif obj.CurrentRow EQ 1> | |
<cfset returnValue.append("{") /> | |
<cfelse> | |
<cfset returnValue.append(",{") /> | |
</cfif> | |
<cfset var isNOTFirstElem = false /> | |
<cfloop array="#currentMetaData#" index="columnDetails"> | |
<cfif isNOTFirstElem> | |
<cfset returnValue.append(",") /> | |
</cfif> | |
<cfset returnValue.append("""#columnDetails.Name#"":""#reReplace(obj[columnDetails.Name][obj.CurrentRow], '"', '\"', 'ALL')#""") /> | |
<cfset isNOTFirstElem = true /> | |
</cfloop> | |
<cfset returnValue.append("}") /> | |
</cfloop> | |
<cfset returnValue.append("] }") /> | |
<cfelseif isStruct(obj)> | |
<cfset returnValue.append("{") /> | |
<cfset var isNOTFirstElem = false /> | |
<cfloop collection="#obj#" item="collectionItem"> | |
<cfif isNOTFirstElem> | |
<cfset returnValue.append(",") /> | |
</cfif> | |
<cfif StructKeyExists(obj, "#collectionItem#")> | |
<cfif IsSimpleValue(StructFind(obj, collectionItem))> | |
<cfset returnValue.append("""#collectionItem#"":""#reReplace(StructFind(obj, collectionItem), '"', '\"', 'ALL')#""")> | |
<cfelse> | |
<cfset returnValue.append("""#collectionItem#"":#JSONUtil( StructFind(obj, collectionItem) )#")> | |
</cfif> | |
<cfelse> | |
<cfset returnValue.append("""#collectionItem#"":""""")> | |
</cfif> | |
<cfset isNOTFirstElem = true /> | |
</cfloop> | |
<cfset returnValue.append("}") /> | |
<cfelseif IsSimpleValue(obj)> | |
<cfset returnValue.append("""#reReplace(obj, '"', '\"', 'ALL')#""") /> | |
<cfelse> | |
<cfthrow message="JSONUtil doesn't support object of type: #currentMetaData.getName()#" type="JSONParsingError" /> | |
</cfif> | |
<cfreturn returnValue.toString() /> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment