json Jackson 复杂列表序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8416904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:05:58  来源:igfitidea点击:

Hymanson complex list serialization

jsonserializationHymanson

提问by tmp120210

I'm experementing with Hymanson serialization/deserialization. For instance, I have such class:

我正在试验 Hymanson 序列化/反序列化。例如,我有这样的课程:

class Base{
    String baseId;
}

And I want to serialize List objs; To do it with Hymanson, I need to specify a list's elements real type, due to the java type erasure. This code will work:

我想序列化 List objs; 为了用 Hymanson 做到这一点,由于 java 类型擦除,我需要指定一个列表的元素真实类型。此代码将起作用:

List<Base> data = getData();
return new ObjectMapper().writerWithType(TypeFactory.collectionType(List.class, Base.class)).writeValueAsString(data);

Now, I want to serialize more complex class:

现在,我想序列化更复杂的类:

class Result{
     List<Base> data;
}

How should I tell Hymanson to properly serialize this class?

我应该如何告诉Hyman逊正确序列化这个类?

回答by Programmer Bruce

Just

只是

new ObjectMapper().writeValueAsString(myResult);

The type of the list won't be lost due to type erasure in the same way it would be in the first example.

列表的类型不会因类型擦除而丢失,就像在第一个示例中一样。



Note that for vanilla serialization of a list or generic list, it's not necessary to specify the list component types, as demonstrated in the example in the original question. All three of the following example serializations represent the List<Bar>with the exact same JSON.

请注意,对于列表或通用列表的普通序列化,没有必要指定列表组件类型,如原始问题中的示例所示。以下所有三个示例序列化都表示List<Bar>具有完全相同的 JSON。

import java.util.ArrayList;
import java.util.List;

import org.codehaus.Hymanson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.Hymanson.annotate.JsonMethod;
import org.codehaus.Hymanson.map.ObjectMapper;
import org.codehaus.Hymanson.map.ObjectWriter;

public class HymansonFoo
{
  public static void main(String[] args) throws Exception
  {
    Baz baz = new Baz("BAZ", 42);
    Zab zab = new Zab("ZAB", true);
    List<Bar> bars = new ArrayList<Bar>();
    bars.add(baz);
    bars.add(zab);

    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);

    String json1 = mapper.writeValueAsString(bars);
    System.out.println(json1);
    // output:
    // [{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}]

    Foo foo = new Foo(bars);

    String json2 = mapper.writeValueAsString(foo);
    System.out.println(json2);
    // output:
    // {"bars":[{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}]}

    mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
    ObjectWriter typedWriter = mapper.writerWithType(mapper.getTypeFactory().constructCollectionType(List.class, Bar.class));

    String json3 = typedWriter.writeValueAsString(bars);
    System.out.println(json3);
    // output:
    // [{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}]
  }
}

class Foo
{
  List<Bar> bars;
  Foo(List<Bar> b) {bars = b;}
}

abstract class Bar
{
  String name;
  Bar(String n) {name = n;}
}

class Baz extends Bar
{
  int size;
  Baz(String n, int s) {super(n); size = s;}
}

class Zab extends Bar
{
  boolean hungry;
  Zab(String n, boolean h) {super(n); hungry = h;}
}

A typed writer is useful when serializing with additional type information. Note how the json1and json3outputs below differ.

类型化编写器在使用附加类型信息进行序列化时很有用。请注意下面的json1json3输出有何不同。

import java.util.ArrayList;
import java.util.List;

import org.codehaus.Hymanson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.Hymanson.annotate.JsonMethod;
import org.codehaus.Hymanson.map.ObjectMapper;
import org.codehaus.Hymanson.map.ObjectMapper.DefaultTyping;
import org.codehaus.Hymanson.map.ObjectWriter;

public class HymansonFoo
{
  public static void main(String[] args) throws Exception
  {
    Baz baz = new Baz("BAZ", 42);
    Zab zab = new Zab("ZAB", true);
    List<Bar> bars = new ArrayList<Bar>();
    bars.add(baz);
    bars.add(zab);

    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
    mapper.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "type");

    String json1 = mapper.writeValueAsString(bars);
    System.out.println(json1);
    // output:
    // [
    //   {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42},
    //   {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true}
    // ]

    Foo foo = new Foo(bars);

    String json2 = mapper.writeValueAsString(foo);
    System.out.println(json2);
    // output:
    // {
    //   "bars":
    //   [
    //     "java.util.ArrayList",
    //     [
    //       {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42},
    //       {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true}
    //     ]
    //   ]
    // }

    mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
    mapper.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "type");
    ObjectWriter typedWriter = mapper.writerWithType(mapper.getTypeFactory().constructCollectionType(List.class, Bar.class));

    String json3 = typedWriter.writeValueAsString(bars);
    System.out.println(json3);
    // output:
    // [
    //   "java.util.ArrayList",
    //   [
    //     {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42},
    //     {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true}
    //   ]
    // ]
  }
}