Jackson XML:如何将空/空集合序列化为空节点

时间:2017-09-01 14:03:03

标签: java jackson-dataformat-xml

我正在使用Jackson XML 2.8.9,遗憾的是我无法找到将空/空集合序列化为空节点的任何方法。

负责序列化为XML的方法:

protected byte[] toXml(final Collection<ReportView> reports) throws IOException
{
    final XmlMapper mapper = new XmlMapper();
    // place for code which will solve my problem
    return mapper.writerWithDefaultPrettyPrinter().withRootName("report").writeValueAsBytes(reports);
}

我试图使用:

  • 序列化包含:

    mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    
  • 序列化提供商:

    final XmlSerializerProvider provider = new XmlSerializerProvider(new XmlRootNameLookup());
    provider.setNullValueSerializer(new JsonSerializer<Object>()
    {
        @Override
        public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException
        {
            jgen.writeString("");
        }
    });
    mapper.setSerializerProvider(provider);
    
  • Jackson 2.9.0 EMPTY_ELEMENT_AS_NULL功能:

    mapper.configure(FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL, false);
    

不幸的是没有效果。有谁知道如何实现它?

测试方法:

@Test
public void testToXml() throws IOException
{
    final Map<String, Object> payload = new LinkedHashMap<>();
    payload.put("amp", "&");
    payload.put("empty", Collections.emptyList());
    final Date date = new Date();
    final ReportView reportView = new ReportView(payload, date, "system");

    // when
    final byte[] xmlBytes = reportService.toXml(Arrays.asList(reportView));

    // then
    final StringBuilder expected = new StringBuilder();
    expected.append("<report>");
    expected.append("  <item>");
    expected.append("    <payload>");
    expected.append("      <amp>&amp;</amp>");
    expected.append("      <empty></empty>");
    expected.append("    </payload>");
    expected.append("    <timestamp>" + date.getTime() + "</timestamp>");
    expected.append("    <changingUser>system</changingUser>");
    expected.append("  </item>");
    expected.append("</report>");

    final String xmlText = new String(xmlBytes).replace("\n", "").replace("\r", "");
    assertThat(xmlText).isEqualTo(expected.toString());
}

ReportView上课:

public class ReportView {
    private final Map<String, Object> payload;
    private final Date timestamp;
    private final String changingUser;

    public ReportView(Map<String, Object> payload, Date timestamp, String changingUser) {
        this.payload = payload;
        this.timestamp= timestamp;
        this.changingUser = changingUser;
    }

    public String getChangingUser() {
        return changingUser;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public Map<String, Object> getPayload() {
        return payload;
    }
}

我准备了一个包含示例代码的存储库:https://github.com/agabrys/bugs-reports/tree/master/jackson-xml/empty-elements-serialization

编辑:

我扩展了测试toXml方法,并进行了一些代码清理。

我还尝试根据ModuleSerializerModifier创建解决方案。不幸的是,两者都失败了。我在jackson-dataformat-xml积压工作中创建了一个问题:

编辑:

我已经提示如何解决异常问题(请参阅NPE after overriding map serializer with custom implementation (XmlBeanSerializerModifier.modifyMapSerializer)),但仍然无法解决缺少empty/null值的问题。

1 个答案:

答案 0 :(得分:0)

我需要解决相同的问题,这就是我的工作方式:

首先,我创建一个序列化程序,将空值序列化为空字符串:

public class NullAsEmptyStringSerializer extends JsonSerializer<Object> {

  static final JsonSerializer<Object> INSTANCE = new NullAsEmptyStringSerializer();

  private static final String EMPTY_STRING = "";
  private final StringSerializer stringSerializer = new StringSerializer();

  @Override
  public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers)
      throws IOException {
    stringSerializer.serialize(EMPTY_STRING, gen, serializers);
  }
}

然后,我创建一个序列化修改器,用我的新序列化器覆盖Bean属性的空序列化器:

public class NullToEmptyPropertySerializerModifier extends BeanSerializerModifier {

  @Override
  public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
      BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
    for (BeanPropertyWriter beanProperty : beanProperties) {
      beanProperty.assignNullSerializer(NullAsEmptyStringSerializer.INSTANCE);
    }

    return beanProperties;
  }
}

最后,我将xml映射器配置为使用我的修饰符:

NullToEmptyPropertySerializerModifier modifier = new NullToEmptyPropertySerializerModifier();
SerializerFactory serializerFactory = BeanSerializerFactory.instance.withSerializerModifier(modifier);
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setSerializerFactory(serializerFactory);

尝试查看它是否适用于字符串和对象(PersonDog是虚拟数据持有者对象):

Dog dog = new Dog("bobby");
Person person = new Person("utku", null, 29, null);
String serialized = xmlMapper.writeValueAsString(person);
System.out.println(serialized);

给出以下输出:

<Person><name>utku</name><address></address><age>29</age><dog></dog></Person>