jackson-databind
jackson-databind copied to clipboard
jackson should provides a method which can deserialize a collection that contains different type items
Is your feature request related to a problem? Please describe.
String collectionJson = "[ {\"school" : \"xxx\", ... }, {\"student" : \"yyy\", ...} ]";
List<JavaType> javaTypeList = new ArrayList<>();
javaTypeList.add(schoolJavaType);
javaTypeList.add(studentJavaType);
// like fastjson#parseArray(collectionJsonString, arrayOfJavaLangReflectType);
List<Object> objectList = objectMapper.parseListByType(collectionJson, javaTypeList);
It is already possible to have polymorphic values but you do have to use @JsonTypeInfo to indicate polymorphic nature (and inclusion of type information on serialization, reading on deserialization).
That annotation may be added to base type of values or on List property.
It is already possible to have polymorphic values but you do have to use
@JsonTypeInfoto indicate polymorphic nature (and inclusion of type information on serialization, reading on deserialization). That annotation may be added to base type of values or onListproperty.
- collections items are not polymorphic values but different java types such as
String、List<Integer>、SomeGeneric<T>; - the collectionJson is provided by others, there are no
"className"in it.
@zrlw Ok I don't understand how this is supposed to work; how should types of elements be detected?
One other thing to note is that you can use
List<?> list = mapper.readValue(json, List.class); // or 'new TypeReference<List<Object>>() { }`
which does allow mapping content into Strings, Longs, Doubles, Booleans and further Lists and Maps. That won't auto-detect any other types and there is no way to configure.
@zrlw Ok I don't understand how this is supposed to work; how should types of elements be detected?
the types of elements are not be detected but be configured in application config.
Ok, let me retry: is the idea that you pass in a List of types (like JavaType) which must match exactly elements of the incoming JSON Array? If so I guess I can see that, although not sure how common a need this is.
A link to Javadocs of the fastjson class would be useful too, I assume that explains the logic.
is the idea that you pass in a
Listof types (likeJavaType) which must match exactly elements of the incoming JSON Array?
yes. it's useful for api gateway generalized calling. service SPI receives all kinds of RPC requests, deserializes paramters and calls the real methods based on the calling type field of the incoming request.
we don't find any useful javadoc but a simple test of fastjson#parseArray: https://github.com/alibaba/fastjson/blob/14a95cc5b07e76ab8c0c72192c1e739cb157c7fc/src/test/java/com/alibaba/json/bvt/parser/DefaultExtJSONParser_parseArray.java#L65
I'm not at all sure about this. I don't think changing jackson-databind is the way to go about this.
It may be possible to use Scala's Tuple classes in Java. It will not be as tidy to use classes like scala.Tuple2, scala.Tuple3 in Java but it should be possible to write stuff like new scala.Tuple2<X, Y>(x, y). Adding the DefaultScalaModule from jackson-module-scala to your ObjectMapper will allow you to serialize/deserialize them.
There are Java tuple implementations out there too but you may need to write code to serialize/deserialize them.
- https://projectreactor.io/docs/core/release/api/reactor/util/function/package-summary.html (https://projectreactor.io)
- Some specific
Pairimplementations - https://stackabuse.com/definitive-guide-to-java-pairs-working-with-tuples-in-java/ - https://www.javatuples.org/