Skip to content

Instantly share code, notes, and snippets.

@timblair
Created July 17, 2012 10:38
Show Gist options
  • Save timblair/3128650 to your computer and use it in GitHub Desktop.
Save timblair/3128650 to your computer and use it in GitHub Desktop.
JSON encoding wrapper to handle CF 9.0.2's failure at serialising strings comprised of all zeroes.
component {
public string function encode(required any data) {
var json = serializeJSON(arguments.data);
if (!find("00", json)) { return json; }
if (left(json, 2) == "00") { json = '"#json#"'; } // just a simple value
// value in a structure
if (find('":00', json)) { json = rereplace(json, '[^\\]":([0]+)', '":"\1"', "ALL"); }
// value in an array (beginning, end, middle)
if (find('[00', json)) { json = rereplace(json, '[^\\]?\[([0]+)', '["\1"', "ALL"); }
if (find('00]', json)) { json = rereplace(json, ',([0]+)\]', ',"\1"]', "ALL"); }
if (find(',00', json) && find('00,', json)) { json = rereplace(json, ',([0]+),', ',"\1",', "ALL"); }
return json;
}
public string function decode(required string json) {
return deserializeJSON(arguments.json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment