Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we make object as key in map when using JSON?

Tags:

As the code below:

public class Main {      public class innerPerson{         private String name;         public String getName(){             return name;         }     }       public static void main(String[] args){         ObjectMapper om = new ObjectMapper();          Map<innerPerson, String> map = new HashMap<innerPerson,String>();          innerPerson one = new Main().new innerPerson();         one.name = "david";          innerPerson two = new Main().new innerPerson();         two.name = "saa";          innerPerson three = new Main().new innerPerson();         three.name = "yyy";          map.put(one, "david");         map.put(two, "11");         map.put(three, "true");            try {             String ans = om.writeValueAsString(map);              System.out.println(ans);           } catch (JsonGenerationException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (JsonMappingException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }      }  } 

The output is:

{"Main$innerPerson@12d15a9":"david","Main$innerPerson@10a3b24":"true","Main$innerPerson@e91f5d":"11"} 

Is it possible to make the key of the map be exact data but not object's address only? How?

like image 486
Sam YC Avatar asked Jul 24 '12 10:07

Sam YC


People also ask

Can an object be a key in JSON?

JSON objects are written in key/value pairs. JSON objects are surrounded by curly braces { } . Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon.

Can an object be a map key?

Answer to your question is yes, objects of custom classes can be used as a key in a HashMap.

Is a JSON object a map?

A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value.


1 Answers

Can we make object as key in map when using JSON?

Strictly, no. The JSON map data structure is a JSON object data structure, which is a collection of name/value pairs, where the element names must be strings. Thus, though it's reasonable to perceive and bind to the JSON object as a map, the JSON map keys must also be strings -- again, because a JSON map is a JSON object. The specification of the JSON object (map) structure is available at http://www.json.org.

Is it possible to make the key of the map be exact data but not object's address only? How?

Costi correctly described the behavior of the default map key serializer of Jackson, which just calls the toString() method of the Java map key. Instead of modifying the toString() method to return a JSON-friendly representation of the map key, it's also possible and reasonably simple to implement custom map key serialization with Jackson. One example of doing so is available at Serializing Map<Date, String> with Jackson.

like image 147
Programmer Bruce Avatar answered Sep 23 '22 15:09

Programmer Bruce