JsonSerializer.cfc
JsonSerializer.cfc copied to clipboard
String values "YES" and "NO" converted to Boolean
We found a bug. If a string has a value "NO" what is ISO code of Norwegian it get's converted to "false". This is not correct and should be kept as is as it is a string.
// Set up our serializer, setting up the key-casing and the value
// conversion rules.
serializer = new lib.JsonSerializer()
.asString( "isoCode" )
;
// Imagine that these keys are all upper-case because they came
// out of a database (or some other source in which the keys may
// have been entered without proper casing).
country = {
ISOCODE = "NO",
};
The string must not change as it is a string and not boolean. Any idea how this can be solved in CF10?
Looked into the lib and found a solution.
Changed line if ( isNumeric( input )) { to if ( isNumeric( input ) || isBoolean( Input ) ) {.
if ( ( hint == "string" ) || ( hint == "any" ) ) {
// If the string appears to be numeric or boolean, then we have to prefix it to make sure
// ColdFusion doesn't accidentally convert it to a number or boolean value.
if ( isNumeric( input ) || isBoolean( Input ) ) {
writeOutput( """" & input & """" );
} else {
writeOutput( serializeJson( input ) );
}
I verified that both YES and NO works.
I found this same bug recently, and came up with the same solution.
We also ran into the same issue, and ended up resolving it the same way. Would be nice if it was rolled into the main library. Will submit a pull request if needed.