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

conversion: behavioural changes in String, MarshalText and MarshalJSON #144

Merged
merged 3 commits into from
Nov 27, 2023

Conversation

holiman
Copy link
Owner

@holiman holiman commented Nov 26, 2023

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 of big.Int for uint256.Int will 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, like big.Int does.
  • MarshalText() now returns the integer in decimal format, previously hexadecimal.
  • MarshalJSON() now returns the integer in decimal format, previously hexadecimal.
  • UnmarshalText now accepts either hex, 0x234 or 234. Previously it accepted only hex.
  • UnmarshalJSON now accepts either hex-string, "0x234", dec-string "234" or naked numeric decimal 234. Previously it accepted only string-hex.

JSON marshalling is, alas, not 100% compatible, since big.Int marshals 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 at 53 bits or so.

To clarify a bit more, re backwards compatibility.

Example 1

If you previously had a struct,

type MyThing struct{
	Value *big.Int
}

And you json-encoded that to disk. the format would be e.g {"Value":5}. If you then replace big with uint256

type MyThing struct{
	Value *uint256.Int
}

Before this PR: The json-encoded old format could not be unmarshalled into the new uint256-format, since uint256.UnmarshalJSON only accepted hex-encoded strings.

After this PR: The json-encoded old format can be unmarshalled into the new uint256-format, since uint256.UnmarshalJSON now accepts json numerics.

@holiman
Copy link
Owner Author

holiman commented Nov 26, 2023

cc @karalabe

Copy link

codecov bot commented Nov 26, 2023

Codecov Report

Merging #144 (ab16ba9) into master (b4f79ca) will not change coverage.
The diff coverage is 100.00%.

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     

conversion.go Outdated Show resolved Hide resolved
conversion.go Outdated Show resolved Hide resolved
conversion.go Outdated Show resolved Hide resolved
conversion.go Outdated Show resolved Hide resolved
conversion_test.go Outdated Show resolved Hide resolved
conversion_test.go Outdated Show resolved Hide resolved
@holiman holiman merged commit f24ed59 into master Nov 27, 2023
4 of 5 checks passed
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

Successfully merging this pull request may close these issues.

3 participants