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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message