jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

jackson should provides a method which can deserialize a collection that contains different type items

Open zrlw opened this issue 3 years ago • 7 comments

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);

zrlw avatar Sep 06 '22 23:09 zrlw

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.

cowtowncoder avatar Sep 07 '22 00:09 cowtowncoder

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.

  1. collections items are not polymorphic values but different java types such as String、List<Integer>、SomeGeneric<T>;
  2. the collectionJson is provided by others, there are no "className" in it.

zrlw avatar Sep 07 '22 02:09 zrlw

@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.

cowtowncoder avatar Sep 07 '22 18:09 cowtowncoder

@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.

zrlw avatar Sep 08 '22 01:09 zrlw

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.

cowtowncoder avatar Sep 08 '22 03:09 cowtowncoder

is the idea that you pass in a List of types (like JavaType) 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

zrlw avatar Sep 08 '22 05:09 zrlw

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 Pair implementations - https://stackabuse.com/definitive-guide-to-java-pairs-working-with-tuples-in-java/
  • https://www.javatuples.org/

pjfanning avatar Sep 24 '22 14:09 pjfanning