Skip to content

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

Merged
holiman merged 3 commits intomasterfrom
marshalling_compat
Nov 27, 2023
Merged

conversion: behavioural changes in String, MarshalText and MarshalJSON#144
holiman merged 3 commits intomasterfrom
marshalling_compat

Conversation

@holiman
Copy link
Copy Markdown
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
Copy Markdown
Owner Author

holiman commented Nov 26, 2023

cc @karalabe

@codecov
Copy link
Copy Markdown

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     

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