GithubHelp home page GithubHelp logo

Comments (9)

frewsxcv avatar frewsxcv commented on June 22, 2024

I can make the appropriate changes if this is approved

from rustc-serialize.

alexcrichton avatar alexcrichton commented on June 22, 2024

I find that a generic storage like JSON often requires the keys to be sorted at one point or another or at least vastly simplifies some form of operation over JSON. I do agree, however, that strictly speaking it may want to be a HashMap instead of a sorted tree.

Something like this, however, I think may be best left to the stabilization of this crate in the long run. I would hate for us to choose HashMap now only to go back later to BTreeMap, for example. Also, as one data point, I personally switched toml-rs from HashMap to BTreeMap as well.

from rustc-serialize.

s-panferov avatar s-panferov commented on June 22, 2024

Personally I would prefer a collection, that could preserve an order of insertions. This is very convenient for things like rustless where you work with JSON API. When I read JSON output I prefer to see:

{ 
    "id": 100500,
    "name": "Cool thing"
}

instead of (what we have now):

{ 
    "name": "Cool thing",
    "id": 100500
}

Maybe we can rewrite json to support generic collection?

from rustc-serialize.

frewsxcv avatar frewsxcv commented on June 22, 2024

@s-panferov Do you prefer the first one because it's sorted alphabetically? If so, why not just change the JSON writer to sort keys before outputting?

from rustc-serialize.

s-panferov avatar s-panferov commented on June 22, 2024

@frewsxcv I prefer the first one because the keys appear in the insertion order, not alphabetically.

from rustc-serialize.

frewsxcv avatar frewsxcv commented on June 22, 2024

Ah, I missed that in the first sentence of your original comment, sorry about that.

Forgive me if I sound defensive about this. I don't feel that strongly about this, but instinctively for something that is defined to be 'unordered', to me it makes sense to use an unordered data structure.

Personally I would prefer a collection, that could preserve an order of insertions.

This is not necessarily what everyone needs. If this is for debugging purposes, I personally would find more value in having the keys be sorted alphabetically when being displayed. I'm not in favor of changing the internal data structure solely for this specific use case.

Maybe we can rewrite json to support generic collection?

This might be an option, but as far as I know, there is no official Map collection trait yet, so this wouldn't be possible.

from rustc-serialize.

agentgt avatar agentgt commented on June 22, 2024

Jackson (the Java JSON library) preserves order and treats JSON very similar to XML (a tree with elements in the order they appear). That is it has its own tree data structure. I will add it is an extremely mature JSON library and thus a good one to model.

I think the way Jackson does it is the correct approach even though the JSON spec does not really say anything about order.

Insertion order (or in document order) is particularly useful if one has both an XML and JSON representation (which is typical in REST APIs) and of course unit testing (which I suppose is the same point of debugging). But ordered JSON is also important for streaming purposes which Jackson also supports.

from rustc-serialize.

dtolnay avatar dtolnay commented on June 22, 2024

In serde_json we have a cfg that controls the representation - BTreeMap by default or LinkedHashMap if you need to preserve insertion order.

from rustc-serialize.

alexcrichton avatar alexcrichton commented on June 22, 2024

I'm going to close this now that this crate is deprecated in favor of serde. We're discontinuing feature development in rustc-serialize but will still continue to merge bug fixes if they arise.

from rustc-serialize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.