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

Getting out-of-order outputs when using map builder #89

Closed
cavenaghi9 opened this issue May 4, 2019 · 6 comments
Closed

Getting out-of-order outputs when using map builder #89

cavenaghi9 opened this issue May 4, 2019 · 6 comments

Comments

@cavenaghi9
Copy link

I try to put key and values with MapBuilder, for instance

mapBuilder.put(-2, "v1");
mapBuilder.put(0, "v2");

However, key 0 will on first item, when encoding map. Is it possible to keep the same order what I inserted?

@c-rack
Copy link
Owner

c-rack commented May 4, 2019

Map internally uses a HashMap. If you come up with a PR that changes the current HashMap implementation to a TreeMap one, I will merge it.

@beclever
Copy link

I found that it was not the https://github.com/c-rack/cbor-java/blob/master/src/main/java/co/nstant/in/cbor/model/Map.java but the sortedMap of TreeMap
type in https://github.com/c-rack/cbor-java/blob/master/src/main/java/co/nstant/in/cbor/encoder/MapEncoder.java caused the problem. In the case, -2 encode as 33, 0 encode as 0, so key 0 will on first item. I don't know why it should bee sortedMap here?

@c-rack
Copy link
Owner

c-rack commented Aug 5, 2019

The TreeMap is used because the CBOR specification has a section about a canonical format:
https://tools.ietf.org/html/rfc7049#section-3.9

One could consider to introduce a non-canoncial mode, which does not use a TreeMap. Any PRs in that direction are welcome.

@illumi
Copy link

illumi commented Oct 29, 2019

I'm also interested in map insertion order. How would you prefer to configure such a mode?

@c-rack c-rack closed this as completed in f6bab9b Nov 1, 2019
@c-rack
Copy link
Owner

c-rack commented Nov 1, 2019

@cavenaghi9 @beclever @illumi I have pushed an update that keeps the order of map entries if the non-canonical mode is active (see README). Publishing to maven central follows.

@vcamargo
Copy link

@c-rack when the version 0.9 of the library will be available on maven central repository?

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

No branches or pull requests

5 participants