conversion: behavioural changes in String, MarshalText and MarshalJSON#144
Merged
conversion: behavioural changes in String, MarshalText and MarshalJSON#144
String, MarshalText and MarshalJSON#144Conversation
Owner
Author
|
cc @karalabe |
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #144 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 5 5
Lines 1642 1643 +1
=========================================
+ Hits 1642 1643 +1 |
namiloh
reviewed
Nov 26, 2023
namiloh
reviewed
Nov 26, 2023
namiloh
reviewed
Nov 26, 2023
karalabe
reviewed
Nov 27, 2023
karalabe
reviewed
Nov 27, 2023
25d5103 to
0016c17
Compare
…s, to maximise big.Int compatibility
0016c17 to
6dee50a
Compare
karalabe
reviewed
Nov 27, 2023
holiman
commented
Nov 27, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR changes the way marshalling and unmarshalling behaves, in order to maximize compatibilty with
big.Int: as in, maximize the chance that a 'drop in' replacement ofbig.Intforuint256.Intwill work seamlessly.String()- before this change,String()would return the integer in hexadecimal format. This PR changes it to instead return the input in decimal format, likebig.Intdoes.MarshalText()now returns the integer in decimal format, previously hexadecimal.MarshalJSON()now returns the integer in decimal format, previously hexadecimal.UnmarshalTextnow accepts either hex,0x234or234. Previously it accepted only hex.UnmarshalJSONnow accepts either hex-string,"0x234", dec-string"234"or naked numeric decimal234. Previously it accepted only string-hex.JSON marshalling is, alas, not 100% compatible, since
big.Intmarshals to json numeric format:{ Foo: 5}as opposed to string-format:{ Foo: "5" }. The former is not ideal for large numbers, since platforms like javascript do not support arbitary large numbers, usually capped at53bits or so.To clarify a bit more, re backwards compatibility.
Example 1
If you previously had a struct,
And you
json-encoded that to disk. the format would be e.g{"Value":5}. If you then replacebigwithuint256Before this PR: The
json-encoded old format could not be unmarshalled into the newuint256-format, sinceuint256.UnmarshalJSONonly accepted hex-encoded strings.After this PR: The
json-encoded old format can be unmarshalled into the newuint256-format, sinceuint256.UnmarshalJSONnow accepts json numerics.