Skip to content

Instantly share code, notes, and snippets.

@timblair
Created June 26, 2012 15:19
Show Gist options
  • Save timblair/2996391 to your computer and use it in GitHub Desktop.
Save timblair/2996391 to your computer and use it in GitHub Desktop.
Test case for ColdFusion JSON serialisation/deserialisation
<cfsetting enablecfoutputonly="true">
<cfset variables.test_cases = {
raw_numeric = 123,
raw_float = 123.456,
zero_numeric = 000,
prefixed_numeric = 000123,
prefixed_float = 000123.456,
string_numeric = "123",
string_float = "123.456",
prefixed_string_numeric = "000123",
prefixed_string_float = "000123.456",
raw_string = "qwerty"
}>
<cfoutput><pre>CF #server.coldfusion.productversion# | Type | Value | Serialised | Deserialised#chr(10)#</cfoutput>
<cfoutput>-------------------------+----------+-------------+---------------+-------------#chr(10)#</cfoutput>
<cfloop collection="#variables.test_cases#" item="key">
<cfset variables.out = {
key = ljustify(lcase(key), 24),
typ = ljustify(listlast(variables.test_cases[key].getClass().getName(), "."), 8),
raw = ljustify(variables.test_cases[key], 11),
jsn = ljustify(serializejson(variables.test_cases[key]), 13),
dsj = deserializejson(serializejson(variables.test_cases[key]))
}>
<cfoutput>#variables.out.key# | #variables.out.typ# | #variables.out.raw# | #variables.out.jsn# | #variables.out.dsj##chr(10)#</cfoutput>
</cfloop>
<cfoutput></pre></cfoutput>
<cftry>
<cfset variables.json = createobject("java", "org.json.JSONObject").init(variables.test_cases).toString()>
<cfoutput><h3>org.json.JSONObject</h3></cfoutput>
<cfoutput><pre>#replace(variables.json, ",", chr(10), "all")#</pre></cfoutput>
<cfcatch type="any" />
</cftry>
<!---
Sample output for two different versions of ColdFusion. The "correct" output
is where the item in the "deserialised" column is exactly the same as that in
the "value" column. Neither CF9.0.0 nor CF9.0.2 passes 100%.
CF 9,0,0,251028 | Type | Value | Serialised | Deserialised
-------------------------+----------+-------------+---------------+-------------
prefixed_string_numeric | String | 000123 | 123.0 | 123
string_float | String | 123.456 | 123.456 | 123.456
prefixed_float | String | 000123.456 | 123.456 | 123.456
string_numeric | String | 123 | 123.0 | 123
zero_numeric | String | 000 | 0.0 | 0
raw_float | String | 123.456 | 123.456 | 123.456
raw_numeric | String | 123 | 123.0 | 123
prefixed_string_float | String | 000123.456 | 123.456 | 123.456
raw_string | String | qwerty | "qwerty" | qwerty
prefixed_numeric | String | 000123 | 123.0 | 123
CF 9,0,2,282541 | Type | Value | Serialised | Deserialised
-------------------------+----------+-------------+---------------+-------------
prefixed_string_numeric | String | 000123 | "000123" | 000123
string_float | String | 123.456 | 123.456 | 123.456
prefixed_float | String | 000123.456 | "000123.456" | 000123.456
string_numeric | String | 123 | 123 | 123
zero_numeric | String | 000 | 000 | 0
raw_float | String | 123.456 | 123.456 | 123.456
raw_numeric | String | 123 | 123 | 123
prefixed_string_float | String | 000123.456 | "000123.456" | 000123.456
raw_string | String | qwerty | "qwerty" | qwerty
prefixed_numeric | String | 000123 | "000123" | 000123
--->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment