saferJSON for ColdFusion
The problem(s) As any ColdFusion dev can tell you, working with JSON in ColdFusion is like feeding yourself soup with a razor blade. Blood everywhere.
The problem is exemplified by the fact that ColdFusion's isJSON() function doesn't protect the deserializeJSON() function from erroring out.
This code should be bullet-proof, but it isn't:
<!--- if this is a valid JSON string ---> <cfif isJSON( your_string )> <!--- deserialize the JSON string (since it's valid) ---> <cfset your_new_object = deserializeJSON( your_string )> </cfif>
Problem 1 In ColdFusion, if you try to deserialize a string that is not valid JSON you will receive a hard error. Ideally, deserialization should fail gracefully.
Problem 2 Furthermore, the isJSON() method should only return true for strings that can actually be deserialized.
The Solution(s) For these reasons, I created a ColdFusion CFC (safeJSON.cfc) which has two methods--one for better JSON validation, and one for more graceful deserialization.
I hope this helps some of you as much as it's helped me.
Requires ColdFusion 10+
Get it from GitHub.











