java Jackson:自定义集合序列化为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3954265/
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
Hymanson : custom collection serialization to JSON
提问by Guido
I am trying to json-serialize a class MyRootClass with a property that is a collection of elements of a second class MyClass:
我正在尝试使用作为第二个类 MyClass 的元素集合的属性对类 MyRootClass 进行 json 序列化:
public class MyRootClass {
private List<MyInterface> list = new ArrayList<MyInterface>();
// getter / setter
}
public class MyClass implements MyInterface {
private String value = "test";
// getter / setter
}
The following code:
以下代码:
MyRootClass root = new MyRootClass();
root.getList().add(new MyClass());
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(System.out, root);
Generates this JSON output:
生成此 JSON 输出:
{"list": [ {"value":"test"} ] }
instead of what I need, every object in the collection serialized with a name:
而不是我需要的,集合中的每个对象都用一个名称序列化:
{"list": [ {"myclass": {"value":"test"}} ] }
Is there any way to achieve it using Hymanson? I thought about writing a custom serializer, but I've not found anything related to a collection of objects.
有没有办法使用Hyman逊来实现它?我想写一个自定义序列化程序,但我没有找到任何与对象集合相关的东西。
采纳答案by StaxMan
It depends on what exactly you want to achieve with name; but yes, this can be done if you want to include 'myclass' here is type information (or can act as if it was used; if you do not use Hymanson to deserialize it does not really matter).
这取决于你想用名字实现什么;但是,是的,如果您想在此处包含类型信息的“myclass”,则可以这样做(或者可以像使用它一样;如果您不使用 Hymanson 进行反序列化,这并不重要)。
If so, you would annotate MyInterface:
如果是这样,您将注释 MyInterface:
@JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
and MyClass with:
和 MyClass:
@JsonTypeName("myclass")
(if you don't define that, default name would be unqualified name of the class)
(如果你没有定义,默认名称将是类的非限定名称)
@JsonTypeInfo
above defines that type name is to be used (instead of Java class name, or custom method), and inclusion is done by using a wrapper object (alternatives are wrapper array and as-property)
@JsonTypeInfo
上面定义了要使用的类型名称(而不是 Java 类名称或自定义方法),并且包含是通过使用包装器对象完成的(替代方案是包装器数组和作为属性)
So you should then see expected output.
所以你应该会看到预期的输出。
回答by Joscha
You can use a helper object like this:
您可以像这样使用辅助对象:
public static class MyObject {
public int i;
public MyObject(int i) { this.i = i; }
public MyObject() {}
}
@JsonDeserialize(contentAs=MyObject.class)
public static class MyHelperClass extends ArrayList<MyObject> {
}
@Test
public void testCollection() throws JsonGenerationException, JsonMappingException, IOException {
final Collection<MyObject> l = new ArrayList<MyObject>();
l.add(new MyObject(1));
l.add(new MyObject(2));
l.add(new MyObject(3));
final ObjectMapper mapper = new ObjectMapper();
final String s = mapper.writeValueAsString(l);
final Collection<MyObject> back = mapper.readValue(s, MyHelperClass.class);
}
回答by Bozho
What you want is to include the name of the class in the output. This is not how json serializers behave - they include only field names.
您想要的是在输出中包含类的名称。这不是 json 序列化程序的行为方式 - 它们仅包含字段名称。
What you can do is to introduce another class.
你能做的就是介绍另一个班级。
class MyClass implements MyInterface {
private MyOtherClass myclass;
}
class MyOtherClass {
private String value = "test";
}