Convert XML with duplicate elements to JSON using jackson

安稳与你 提交于 2020-01-14 09:34:10

问题


I have some simple data in XML format which I need to convert to JSON and also be able to convert the JSON back into the same XML string. But I'm having problems with doing this using existing jackson (version 2.0.6) libraries.

Here's an example of XML data with similar structure

<channels>
  <channel>A</channel>
  <channel>B</channel>
  <channel>C</channel>
</channels>

To be able to convert this back to the original XML, I'd like the JSON to look something like this

{
  "channels": {
    "channel": [
      "A",
      "B",
      "C"
    ]
  }
}

However jackson gives me

{"channel":"C"}

The root element name is not preserved and instead og creating array of channels, the last one overwrites the previous ones.

Looking at the source code of com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer I found that the library doesn't support this, but allows for overriding and changing the behavior.

/**
 * Method called when there is a duplicate value for a field.
 * By default we don't care, and the last value is used.
 * Can be overridden to provide alternate handling, such as throwing
 * an exception, or choosing different strategy for combining values
 * or choosing which one to keep.
 *
 * @param fieldName Name of the field for which duplicate value was found
 * @param objectNode Object node that contains values
 * @param oldValue Value that existed for the object node before newValue
 *   was added
 * @param newValue Newly added value just added to the object node
 */
protected void _handleDuplicateField(String fieldName, ObjectNode objectNode,
                                     JsonNode oldValue, JsonNode newValue)
    throws JsonProcessingException
{
    // By default, we don't do anything
    ;
}

So my questions are

  1. Has anyone written a custom deserializer to support this feature? Or is there another way to work around this.
  2. How do I preserve the root element name?

Below is a test example

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class Test {
  public static void main(String[] args) throws Exception {
    String xml="<channels><channel>A</channel><channel>B</channel><channel>C</channel></channels>";

    XmlMapper xmlMapper = new XmlMapper();
    JsonNode node=xmlMapper.readValue(xml,JsonNode.class);
    System.out.println(node.toString());
  }
}

回答1:


What really matters here are your classes -- just showing XML in itself does not give enough information to know what is going on.

I suspect that you will need Jackson 2.1 (once it gets released in a week or two), since it finally supports "unwrapped Lists" correectly. Prior to this, only "wrapped" Lists work correctly.



来源:https://stackoverflow.com/questions/12427076/convert-xml-with-duplicate-elements-to-json-using-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!