-
Notifications
You must be signed in to change notification settings - Fork 109
Change Object underlying type from BTreeMap to HashMap #56
Comments
I can make the appropriate changes if this is approved |
Fixes rust-lang-deprecated#56 One side effect of this, is Json no longer derives PartialOrd since HashMap doesn't derive it. That said, I don't really see why it should derive it in the first place. [breaking-change]
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 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 |
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 |
@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? |
@frewsxcv I prefer the first one because the keys appear in the insertion order, not alphabetically. |
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.
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.
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. |
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. |
In serde_json we have a cfg that controls the representation - BTreeMap by default or LinkedHashMap if you need to preserve insertion order. |
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. |
Right now, an
Object
ispub type Object = BTreeMap<string::String, Json>;
According to the Rust docs:
Based off of those distinctions, why should
Object
be aBTreeMap
? From what I understand, we don't need it sorted.The text was updated successfully, but these errors were encountered: