ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.87k stars 295 forks source link

json: boolean cannot be decoded from a json string, but a number can #157

Closed pwittrock closed 8 years ago

pwittrock commented 8 years ago

decodeBool fails to decode "true", but decNum is able to decode "5"

Decoding numbers unquotes fields ("5" can be decoded into an integer field), but decoding booleans does not perform unquoting ("true" fails with an error).

decode boolean line

decode number line

ugorji commented 8 years ago

decode bool

json encoding has an option called: IntegerAsString. It is used to support storing numbers which cannot be encoded as IEEE doubles, in json. For example, you can store an integer using only digits (no characters) and consequently have no loss of precision (doubles lose precision by definition, which is why floating point arithmetic is not exact).

As we support encoding an integer as a string, we also support decoding numbers stored in a string.

Booleans only have true | false, so there's no support for BooleanAsString.