Repository navigation
Conversation
|
I was going to say that we don't do this on purpose cause some impls (mainly JS) can't discriminate .. but apparently I updated the spec to say that implementations should do this .. so this is reasonable. https://ipld.io/specs/codecs/dag-json/spec/#numbers |
|
Originally DAG-JSON was only supposed to be for debugging and it shouldn't end up in CIDS as it's to easy to arrive at different CIDS (thoughm that's just my opinion). I know that there are systems that use DAG-JSON and produce CIDs off it. I've spent a lot of time recently on floats and how to encode them in JSON. I care about that in regards to the AT Protocol, which is also IPLD based in the wider sense. They use a different JSON encoding, but floats have the same problems. I'd be in favour in preserving the fractions in all libraries where this is possible. As this could then also help folks in the ATProto ecosystem. https://github.com/vmx/json-floats-native-types has an extensive list of how programming languages/libraries behave and what workarounds are available. |
|
A practical case is For a schema-backed example, @sssion reported this in #403: |
dagjson.Encodewrites a whole-valued float such as32.0as32. A decode then produces an Int node, and aLinkSystemstore/load round trip into a schema Float field fails withAssignInton a Float node. Related to the case @sssion reported in #403.This wraps refmt's encoder in
EncodeOptions.Encode, adding.0only when a Float token's numeric write would otherwise look like an integer. Integer-looking floats outside int64 use scientific notation, avoiding the decoder's overflowing integer prefix (for example1e20becomes1.0e+20).Integer/string tokens and existing decimal/exponent float output keep their formatting. Generic JSON and the deprecated
Marshalwith a caller-supplied token sink are unchanged.This changes encoded bytes when a float previously looked like an integer, so callers deriving CIDs from those bytes should expect different CIDs.
Regressions exercise both encoding entrypoints, nested mixed kinds, signed zero and float extremes, plus typed
LinkSystem.Store/Loadwith a byte buffer and a fractional-value control.Local checks on Linux arm64:
skipgenbehavteststag: passed, 37 tests skipped per run.Windows/macOS and remote CI have not run here.
AI assistance was used for the patch and regression tests.