Add JSON serialization to big number types #9898
Merged
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:
Summary of Changes:
Allow serialisation: and
.to_json
to allBig*
structs (BigInt
,BigFloat
,BigDecimal
)Serialisation
Big*.to_json
is enabled through addingJSON::Builder.number
methods for eachBig*
type, serialising into Number typesAllow deserialisation of
BigFloat
fromInt
orFloat
Allow all
Big*
structs to be used as JSON keys: add.to_json_object_key
Add missing
pending_win32 "does for BigDecimal"
toyaml/serialization_spec.cr
Further Discussion:
This PR seeks to resolve Implement to_json(json : JSON::Builder) on BigDecimal #7856, and is built from the exchanges in Extend Big* classes support for JSON serialization #8070 .
The reason for opening this PR is because issue Implement to_json(json : JSON::Builder) on BigDecimal #7856 should be resolved swiftly as
Big*
finds lots of usage in terms of serialisation, yet Extend Big* classes support for JSON serialization #8070 has been open and without activity for almost a year. In this PR, all comments from PR Extend Big* classes support for JSON serialization #8070 are addressed, e.g. disallowingBigInt
andBigFloat
to be deserialised from aString
, allowingBig*
to only serialise intoNumber
and not String.With regards to RX14's suggestion on building a custom converter to serialize/ deserialize
Big*
structs into/ fromString
(Extend Big* classes support for JSON serialization #8070 (comment)), this can be done as a separate PR, or if the maintainers would like this to be addressed here, I would not mind adding it in. However, IMHO, I think the commit in this PR currently is adequate to resolve the original issue, and any other modification can be addressed in a separate PR.