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

ser: some way to opt-in canonical JSON output #309

Closed
lucab opened this issue Apr 24, 2017 · 10 comments
Closed

ser: some way to opt-in canonical JSON output #309

lucab opened this issue Apr 24, 2017 · 10 comments

Comments

@lucab
Copy link

lucab commented Apr 24, 2017

When relying on digests of JSON data, it is often required that the content should be in some canonical form so that different serializer can deterministically reproduce the same data bit-by-bit.

The topic is a bit tricky as there is not a single JSON canonical format, but several similar efforts:

(I'd personally be interested in one of the latter twos)

I'm not sure whether a CanonicalFormatter would be enough for this or if it requires something more invasive?

@dtolnay
Copy link
Member

dtolnay commented Apr 24, 2017

https://github.com/vtduncan/canonical_json may implement this already. If it can be as simple as swapping in Formatter, I would be open to providing it in serde_json.

@lucab
Copy link
Author

lucab commented Apr 24, 2017

Ah, it looks like I didn't do enough due diligence investigation before filing this, as I missed the above crate. Also, it looks like it needs update to 1.0 and I hit this at the right time, as you reported it yesterday 😄

But yes, I would love to see it integrated here.
/cc @vtduncan

@cyberphone
Copy link

@lucab If you haven't already implemented a JSON canonicalizer you may take a peek at: https://github.com/cyberphone/json-canonicalization

@dtolnay dtolnay assigned dtolnay and unassigned dtolnay May 7, 2018
@dtolnay
Copy link
Member

dtolnay commented May 7, 2018

I'm happy to focus on conventional JSON here and leave Canonical JSON to a different library. Thanks!

@dtolnay dtolnay closed this as completed May 7, 2018
@lucab
Copy link
Author

lucab commented May 7, 2018

@dtolnay ack, thanks. While at it, it looks the repo you originally linked disappeared in the meanwhile (and it was quite obsolete even before). Do you know about any up-to-date efforts on this?

@cyberphone
Copy link

Standard-in-progress needing the same basic features:
https://tools.ietf.org/id/draft-erdtman-jose-cleartext-jws-00.html

@dtolnay
Copy link
Member

dtolnay commented May 7, 2018

I think it moved to https://github.com/zmanian/canonical_json.

But that library is a full fork of serde_json. The way I would recommend exposing this in a library instead is as a Formatter implementation:

let mut ser = serde_json::Serializer::with_formatter(&mut out, canonical_json::Formatter);
t.serialize(&mut ser)?;

// equivalent for convenience
let cj = canonical_json::to_string(&t)?;

@cyberphone
Copy link

The hard part is the formatting of numbers. I just ported 2000 lines of java code to .NET to achieve compatibility wit the Internet-Draft:
https://github.com/cyberphone/json-canonicalization/blob/master/dotnet/es6numberserialization/README.md

@radu-matei
Copy link

Hey, @dtolnay!
Quick question about implementing this as a Formatter -- one of the key aspects of canonical JSON is the lexicographical order of the keys in the JSON -- is there a way to sort the order the object keys only through a formatter?

Thanks, and thanks for all the work on a really nice library and API!

@dtolnay
Copy link
Member

dtolnay commented Mar 17, 2019

Yes @radu-matei, the formatter will need to sort the entries -- probably in Formatter::end_object since by then all the entries have been seen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants