How do I serialize a java.util.Map with the Jackson JSON processor if the key is a custom class?
-
I am using Jackson 1.8.2, I have a POJO where one of the attributes is Map<MyKey, String> and MyKey is a class that has two attributes, a Long and an Integer. When Jackson serializes the POJO that contains the Map<MyKey, String> I get{ âMyKey@12f34fâ, âstringValueâ} for the map. I can not find a concrete example anywhere *that works* so instead I would get something like {{âLongAttrâ:123,âIntegerAttâ:456}, âstringValueâ}. I can not get @JsonSerialize(asKey=â¦) or @JsonSerialize(keyUsing=â¦) to work on the attribute in the POJO that is the map (seems to be ignored by Jackson, the custom JsonSerializer I have never gets executed) . What is the proper way to do this?
-
Answer:
Sorry, you can't do this. Look at the JSON spec: http://www.json.org/ It's very simple. The first words of the first bullet point say it clearly: "A collection of name/value pairs". Keep in mind that JSON is basically lifted from Javascript syntax for object and array literals. Your map in Java is mapped to/from a Javascript object, when and if it is evaluated in a browser context. A Javascript object has fields, and the fields have names. Names can't be objects, they are strings. Jackson is doing what it can here, resorting to toString(). You can, of course, create an object where the fields have names that are JSON strings. And, of course, this would be insane. On second thought, you probably can't even do that, since field names obviously can't hold arbitrary characters. So this would require some string encoding of JSON that obeys Javascript syntax for field names! Is there a quora board for mad computer science?
Kjetil Valstadsve at Quora Visit the source
Other answers
As says, in JSON, keys must be strings. However, you can still represent your map in JSON, just not as a JSON object: [ [ {"LongAttr":123, "IntegerAtt":456}, "stringValue" ], [ {"LongAttr":129, "IntegerAtt":442}, "somethingelse" ], ... ] You can even probably persuade Jackson to do this.
Toby Thain
Related Q & A:
- How do I make a window in Java?Best solution by Stack Overflow
- How can I make a Spinner only by java?Best solution by stackoverflow.com
- How do I do a HTTP GET in Java (android?Best solution by Stack Overflow
- How do I get a job in IT or Software Development if I have no experience and only a 3rd class degree?Best solution by answers.yahoo.com
- How do i print a full map?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.