杰克逊:原始类型的序列化类型信息

时间:2019-05-31 21:07:03

标签: java jackson jackson2

使用jackson 2.9.8时,是否有一种方法可以始终序列化每个单个序列化对象(甚至是原始类型)的类型信息(完全限定的类名)?

作为示例,我希望以下代码打印["java.util.ArrayList",["java.lang.String","ONE"],["java.lang.String","TWO"]]而不是["java.util.ArrayList",["ONE","TWO"]]

public static void main(String[] args) throws Exception {
  final ObjectMapper mapper = new ObjectMapper();
  mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
  mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
  mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

  List<String> list = new ArrayList<String>();
  list.add("ONE");
  list.add("TWO");

  Writer writer = new StringWriter();
  mapper.writerFor(list.getClass()).writeValue(writer, list);

  System.out.println(writer.toString());
}

干杯。

0 个答案:

没有答案