> yes I know about strings and numeric conversions.  I want to 
> know is there a way to force json to force values to strings. 
>  I have played with the parser but it is a PITA and played 
> with adding " " at the query level but that is too 
> cumbersome.  I can't believe JSon does not have any options 
> or settings that force all values to strings.  Anyone played 
> with this at all?

JSON is just a data format. It doesn't have "options".

JSON is defined to be a subset of the JavaScript object literal format.
Forget JSON for a moment and let's just talk about pure JavaScript code:

   var address = {
      "city": "Augusta",
      "state": "ME",
      "zip": 04330
   };

   alert( address.zip );

In most browsers, that will alert 2264 (the decimal value of the octal
number 04330).

If you let JavaScript see a number with a leading zero, it *will* interpret
it as octal (unless the number contains an 8 or 9 anywhere - in that case it
will be interpreted as decimal after all) - again in most browsers.

Your only solution is to keep JavaScript from seeing that 04330 as a number.
You can do that either by:

1) Quoting the number so it's a string, not a number:

   var address = {
      "city": "Augusta",
      "state": "ME",
      "zip": "04330"
   };

Or 2) using a custom "sort-of-JSON" parser that doesn't use eval() and
handles numeric strings as strings:

   var address = '{ "city": "Augusta", "state": "ME", "zip": 04330 }';
   // Now address is simply a long string, and you can write your own
   // parser for it that treats the components of the string any
   // way you want.

Option 1 is probably the simpler of the two.

-Mike

Reply via email to