Jackson Series Sort List

I am experimenting with serializing / deserializing Jackson. For example, I have a class like this:

class Base{ String baseId; } 

And I want to serialize List objs; To do this using jackson, I need to specify a list item of type real, due to erasure of type java. This code will work:

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

Now I want to serialize a more complex class:

 class Result{ List<Base> data; } 

How should I say that Jackson serialized this class correctly?

+6
source share
1 answer

Simply

 new ObjectMapper().writeValueAsString(myResult); 

The type of the list will not be lost due to erasing the type in the same way as in the first example.


Please note that for valid serialization of a list or general list, there is no need to specify the types of list components, as shown in the example in the original question. All three of the following serialization examples are List<Bar> with the same JSON.

 import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; import org.codehaus.jackson.annotate.JsonMethod; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectWriter; public class JacksonFoo { 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 typical writer is useful when serializing with additional type information. Please note that the outputs of json1 and json3 below are different.

 import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; import org.codehaus.jackson.annotate.JsonMethod; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper.DefaultTyping; import org.codehaus.jackson.map.ObjectWriter; public class JacksonFoo { 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} // ] // ] } } 
+14
source

Source: https://habr.com/ru/post/903296/


All Articles