Jackson Object mapper to convert java map to json maintaining order of keys -


I'm using the jackson.map.ObjectMapper API to convert the json string to the map I am I'm using writeValueAsString method to get this.

I pass a map sorted by value to the writeValueAsString method. The JSON string I get is surrounded by keys.

Is there a way to change the map using Jackson without affecting the order of objects in Maps in Maps.

I tried setting the value to Feature.SORT_PROPERTIES_ALPHABETICALLY , but according to the documentation it is applicable only for POJO types.

said any ideas for implementing the behavior.

with Jackson 2.3.1 (you do not know for previous versions) as an example Treemark, Sort can be respected.

Here is an example in Junior 4:

  @Test pushes public Zero Test (Cyrilize) JsonProcessingException {ObjectMapper om = new ObjectMapper (); Om.configure (SerializationFeature.WRITE_NULL_MAP_VALUES, false); Om.configure (SerializationFeature.INDENT_OUTPUT, true); Om.setSerializationInclusion (Include.NON_NULL); SortedMap & LT; String, string & gt; Sorted map = new trendmark & ​​lt; String, string & gt; (); Click the & lt; String, string & gt; Map = new hashmap & lt; String, string & gt; (); Map.put ("aaa", "aAA"); Map.put ("bbb", "bbb"); Map.put ("CCC", "CCC"); Map.put ("ddd", "DDD"); SortedMap.putAll (map); Println (om.writeValueAsString (map)); Println (om.writeValueAsString (sortedMap)); }   

And here's the result:

with a map

  {"aaa": "AAA", "deed" : "AAA", "CD", "CCC", "BBB": "BBB"}   

a sorted map

  "BBB": "Bbb", "CCC": "CCC", "Deed D": "DDD"}   

`

1 Sorting with a map Order will not be given, alphabet will be ordered using the second key with the tree mark. You can pass a comparator to the papermap for a different order.

EDIT: It also works on Jackson with a Linkedic Mac, even if it is not a random map. This is the implementation of the map which maintains the order which was inserted into the key on the map. It can be what you want.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -