-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Improved serialization of java.util.Map #823
Comments
Reported by [email protected] |
[email protected] said: |
File: JsonMapAdapter.java |
[email protected] said: |
File: JsonMapAdapter.java |
[email protected] said:
I attached a new version of the JsonMapAdapter class, which tries to address the An important point for mapped notation is not to forget to call the xml2JsonNs Please test if it works for you and provide input about discovered problems. TODO: Right now, unmarshalling would only work for a value of class C, if at |
mike_meessen said: I ran into an issue though: Element keyValueElement = doc.createElement(entry.getKey().toString()); entity.getKey().toString() produces e.g. "4", which is not a valid xml entity and causes: SEVERE: org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. Any idea how to fix this? If I change the line to the following, it works but output is obviously wrong: Element keyValueElement = doc.createElement("x" + entry.getKey().toString()); All in all, XML isn't Json and having to jump through it ends up being a real pain :/ |
eskatos said: From what I understand it's impossible to solve the issue except by removing JAXB from the equation. That's a real pitty that JAX-RS 1 cannot produce simple json like that. I don't know about JAX-RS 2 but if it's still based on JAXB the problem will remain. I hope someone can prove us wrong and tell us "there is a way and this is ...". Regards |
This issue was imported from java.net JIRA JERSEY-551 |
Currently, to serialize java.util.Map objects to JSON, one needs to write a
special @XmlJavaTypeAdapter annotation and usually covert the map into list of
(key,value) entires, which are then marshalled/unmarshalled by JAXB.
The representation for a key/value pair produced by this approach usually looks
like this:
{"key": "MyKey", "value" : "MyValue"}
This is not very natural for JSON, as
{"MyKey":"MyValue"}
would be a more usual
JSON-like representation. Unfortunately, this JSON-friendly representation
cannot be easily produced by jersey-json, because it works via JAXB and JAXB
cannot produce this out of the box easily, as it relies on XML Schemas and this
representation requires XML tag names that are dynamic, which is impossible.
Therefore, it would be nice to introduce the possibility to serialize Maps into
a natural JSON format.
Please find attached a class called JsonMapAdapter.
It can be used like this to achieve the desired effect:
@XmlRootElement(name="YourClassElementName")
class YourClass {
...
@xmlelement(name="YourMapElementName")
// Here is the trick: Use use special map adapter for maps (see below)
@XmlJavaTypeAdapter(JsonMapAdapter.class)
private Map<String,String> yourMap = new LinkedHashMap<String,String>();
...
}
Please see this mailing list discussion for more information:
http://old.nabble.com/Custom-mapping-for-Maps-in-JAXB-ts28779221r0.html
NOTE: This approach seems to work nicely for Map<String,String>. But if values
are JAXB-annotated classes, it most likely would not work out of the box. The
way to solve this issue still needs to be investigated.
Environment
Operating System: All
Platform: All
URL: http://old.nabble.com/Custom-mapping-for-Maps-in-JAXB-ts28779221r0.html
The text was updated successfully, but these errors were encountered: