Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zrlw opened this issue Sep 6, 2022 · 7 comments · May be fixed by #3597
Open

Comments

@zrlw
Copy link

zrlw commented Sep 6, 2022

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 zrlw added the to-evaluate Issue that has been received but not yet evaluated label Sep 6, 2022
@cowtowncoder
Copy link
Member

cowtowncoder commented Sep 7, 2022

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 cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Sep 7, 2022
@zrlw
Copy link
Author

zrlw commented Sep 7, 2022

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.

@cowtowncoder
Copy link
Member

@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
Copy link
Author

zrlw commented Sep 8, 2022

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

@cowtowncoder
Copy link
Member

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.

@zrlw
Copy link
Author

zrlw commented Sep 8, 2022

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

@pjfanning
Copy link
Member

pjfanning commented Sep 24, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants