Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Large numbers lose precision when decoding #29

Open
NewPProd opened this issue Dec 30, 2020 · 2 comments
Open

Large numbers lose precision when decoding #29

NewPProd opened this issue Dec 30, 2020 · 2 comments

Comments

@NewPProd
Copy link

A web service I need to use is returning the following in its JSON:
"Id":1618065507111835497

But when using this Lua library to decode it, the result is losing precision as "Id" is returned as:
1.6080655071118e+18

In this case I can't change what the webservice is providing. I'm quite happy referring the data as a string - it's just an id value.
I am limited to only using Lua 5.1.4.

Any help appreciated (e.g. pass back numbers that can't be correctly converted as strings?).

Thanks,
Rob

@jtackaberry
Copy link

But when using this Lua library to decode it, the result is losing precision as "Id" is returned as:

Decoding looks ok to me:

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> json = require 'json'
> d = json.decode('{"id":1618065507111835497}')
> print(d.id)
1618065507111835497

But encoding does lose the precision:

> json.encode({id=1618065507111835497})
{"id":1.6180655071118e+18}

@NewPProd
Copy link
Author

NewPProd commented Jan 4, 2021

I think the problem is that Lua 5.1.4 does not include 64 bit integers, this was added in Lua 5.3.
When I try and perform the same decode example on Lua 5.1.4 I lose precision.
I'm restricted to using Lua 5.1.4 as it is embedded in the host application.

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> json = require 'json'
> d = json.decode('{"id":1618065507111835497}')
> print(d.id)
1.6180655071118e+018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants