Skip to content

json::to_pretty_writer output should be consistent #8557

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

Closed
ozten opened this issue Aug 16, 2013 · 4 comments
Closed

json::to_pretty_writer output should be consistent #8557

ozten opened this issue Aug 16, 2013 · 4 comments

Comments

@ozten
Copy link
Contributor

ozten commented Aug 16, 2013

Currently when you build up a Json object and write it to a file using json::to_pretty_writer, the order of the keys in objects is random.

There are two possible improvements that come to mind:

  1. Write objects based on the natural sort order of the keys

  2. Write objects based on the order in which keys were added to the object

Most mature JSON libraries I've used do #2, but even #1 would be an acceptable first step to getting deterministic serialized output.

@alexcrichton
Copy link
Member

Are you using rust master? Internally json now uses a TreeMap instead of a HashMap so I figured they'd be deterministic unless they're getting purposefully scrambled...

@ozten
Copy link
Contributor Author

ozten commented Aug 17, 2013

Thanks! (No I was working on 0.7)

@ozten ozten closed this as completed Aug 17, 2013
@alexcrichton
Copy link
Member

Regardless, thanks for the report anyway! If you're trying out rust, I would recommend using the master branch anyway. It's got boatloads of fixes from 0.7 and it's almost always stable.

@erickt
Copy link
Contributor

erickt commented Aug 17, 2013

@ozten: I caution you on relying too heavily on this feature though. The json spec explicitly states that the key/value pairs in an object are unsorted. Right now Rust's json.rs library sorts all Maps before generating a json string, but we may skip that step in the future to speed up json::to_writer. However if we do do that, I believe we'll be able to keep json::to_pretty_writer sorting the maps. That function shouldn't be used on a critical path, so we can afford to spend some cycles making the output human readable.

flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 24, 2022
add `or_then_unwrap`

Closes rust-lang#8557

changelog: New lint [`or_then_unwrap`]
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

No branches or pull requests

3 participants