JsonSerializer.cfc icon indicating copy to clipboard operation
JsonSerializer.cfc copied to clipboard

String values "YES" and "NO" converted to Boolean

Open alexhass opened this issue 11 years ago • 3 comments

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?

alexhass avatar Jul 28 '14 09:07 alexhass

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.

alexhass avatar Jul 28 '14 10:07 alexhass

I found this same bug recently, and came up with the same solution.

getdeformed avatar Mar 05 '15 19:03 getdeformed

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.

dheighton avatar Mar 05 '15 19:03 dheighton