bennadel / JsonSerializer.cfc

A JavaScript Object Notation (JSON) serialization utility for ColdFusion.
Other
47 stars 21 forks source link

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

Open alexhass opened 10 years ago

alexhass commented 10 years ago

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 commented 10 years ago

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.

getdeformed commented 9 years ago

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

dheighton commented 9 years ago

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.