-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
JSON Number Consistency #6714
Comments
integer
not string
integer
not string
@ethanfrey @webmaster128 Is this an issue in cosmjs? |
Both yaml and JSON support numbers > 53 bit in their spec. Unfortunately, many implementations convert them to IEEE 754 floats, making it impossible to operate on them without rounding errors. JSON integer support > 53 bit
For fields that can exceed For fields that are auto-incrementing and start at 0 or 1, it depends on the expected range. Then the question becomes: do you expect more than 9007199254740991 accounts (more than 1 millions accounts per human being) or more than 9007199254740991 transactions per account (285616 years sending one tx per millisecond)? |
I have seen account number and sequence change from int to string to int again. I just adapt the client parse logic. I think both are fine (what Simon says), it is more updating your parsing code. (btw, what js lib is Terra using?) |
Why is YAML being discussed here? This has nothing to do with YAML. So a few things:
What you're stating has nothing to do with Text (YAML) output or In the example you've posted, the Account has coins which amount are In order to have unification in this regard -- i.e. all numbers as strings, you'll need to implement custom JSON marshalling for every single type. |
@alexanderbez thanks for renaming the issue title. This is not the only thing that is inconsistent. When you request POST Ex)
@webmaster128 agree on that two fields, but the sdk has more fields with uint64 data type in each module's params or in the block info. We can determine whether the field will exceed int max or not, but it is unnecessary works to do that always. @ethanfrey we are using our own js library here |
Yeah I guess that is pretty annoying. Hopefully, we'll be moving away from Amino JSON soon. |
I think this inconsistency came with 82a2c5d |
@alexanderbez now I understand the problem is not YAMl, it is on custom JSON Marshaler. I updated the issue description. Can you see the fixed issue? Here is summary, Account custom JSON Marshaler is using json.Marshal, but other parts are using amino codec.Cdc.MarshalJSON to build custom JSON Marshaler. |
I agree. A generic solution should use strings for integers. Otherwise you run in big trouble like $ echo 18446744073709551611 | jq
18446744073709552000 |
Other issue from the same code #6745 |
Yeah since we're using Amino still, I suppose any custom JSON marshaling should use strings for numbers. |
We can refer to the protobuf spec for json number encoding. |
Summary of Bug
After updating cosmos-sdk@v0.38.x, some struct like Account or RelegationResponse are using json.Marshal() function on its custom MarshalJSON function. But others are using amino codec like Validator.
This inconsistency causes huge suffer from number parsing on client program. json.Marshal is treating primitive int64/uint64 as
number
notstring
, but amino codec is treating those asstring
.Moreover
sdk.Int
is encoded asstring
on both codec, it is too wired. Some integer types on some objects are encoded asstring
and some integer types on some objects are encoded asnumber
.Ex) Account info -
account number
&sequence number
(It was string util v0.37.x)Ex) Validator query -
unbonding_height
is int64 treated asstring
Maybe there are lots of uint64 data relayed to client in this format.
I think, for the safe application communication, the string format is recommended.
Problematic case can be easily found like decoding response at javascript. All the client program will be affected.
Version
v0.38.x
Steps to Reproduce
create account & query account as JSON format
For Admin Use
The text was updated successfully, but these errors were encountered: