After Json unmarshaling big ints become floats

5,842 views
Skip to first unread message

mikalai....@gmail.com

unread,
Aug 22, 2013, 11:54:42 AM8/22/13
to golan...@googlegroups.com
Hi!


Is there a way to have big ints as ints after unmarshaling json?

Best Regards,
Nick


ck

unread,
Aug 22, 2013, 9:08:19 PM8/22/13
to golan...@googlegroups.com
Checked it! I have to say that integer marshalled by json will be get
float64 after unmarshalled. You should convert it to any type of integer if
you wish.
On the another way, you can see here, https://github.com/bitly/go-simplejson



Stephen Gutekanst

unread,
Aug 22, 2013, 9:24:42 PM8/22/13
to golan...@googlegroups.com, mikalai....@gmail.com
Nick,

JSON (JavaScript Object Notation), for clarification, uses Javascript syntax and definitions. JavaScript only supports 64-bit floating point numbers.

You'll need to convert them back to other datatypes as you deem necessary. But you'll need to be aware that you still can only use up to the precision of an 64-bit floating point number.

If this is a deal-breaker for you, you might look into BSON which supports some other numerical data types.

Stephen

Matthew Kane

unread,
Aug 22, 2013, 9:55:14 PM8/22/13
to mikalai....@gmail.com, golang-nuts
If you unmarshal into a map[string]int: http://play.golang.org/p/sa-NJuAvOD

Obviously this won't work if you have anything besides numbers.

If you unmarshal into a struct: http://play.golang.org/p/jWBLztNDZg

If you use a json.Decoder, you can tell it to unmarshal numeric
literals into a json.Number, which can then give you back an integer:
http://play.golang.org/p/eB0XGRwIKH
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
matt kane
twitter: the_real_mkb / nynexrepublic
http://hydrogenproject.com

Jeff Hodges

unread,
Aug 22, 2013, 11:18:49 PM8/22/13
to golan...@googlegroups.com, mikalai....@gmail.com
Actually, it's worse: JSON only supports integers up to (2^55)-1. Javascript, by spec and implementation, stores all numbers as doubles and the JSON spec follows it. 

Kevin Gillette

unread,
Aug 25, 2013, 10:56:23 PM8/25/13
to golan...@googlegroups.com, mikalai....@gmail.com
On Thursday, August 22, 2013 9:18:49 PM UTC-6, Jeff Hodges wrote:
Actually, it's worse: JSON only supports integers up to (2^55)-1. Javascript, by spec and implementation, stores all numbers as doubles and the JSON spec follows it. 

I see no part of http://www.ietf.org/rfc/rfc4627.txt that indicates that the JSON specification in any way limits the range of numbers, nor does it mention that semantics are adopted from ECMAScript. It does say, "An implementation may set limits on the range of numbers," though that cannot be used to infer that "JSON supports" something only to a restricted extent.

roger peppe

unread,
Aug 27, 2013, 9:12:44 AM8/27/13
to mikalai....@gmail.com, golang-nuts
Two solutions. One is to unmarshal into a known type.
For instance (replace *big.Int by int64 or int as appropriate there).
http://play.golang.org/p/_6FJJfvuHg

Another solution, useful when you don't know the expected types in
advance, is to use json.Number: http://play.golang.org/p/W4fKXZTkNG
(You'll need to decide how to parse the Number after you've
got it).

guoyunl...@gmail.com

unread,
Sep 21, 2017, 9:42:34 AM9/21/17
to golang-nuts
maybe help for . https://golang.org/pkg/encoding/json/#Decoder.UseNumber

在 2013年8月22日星期四 UTC+8下午11:54:42,Nick Savchenko写道:
Reply all
Reply to author
Forward
0 new messages