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

Add handling of collections with polymorphic types #11

Open
sockeqwe opened this issue Feb 18, 2015 · 14 comments
Open

Add handling of collections with polymorphic types #11

sockeqwe opened this issue Feb 18, 2015 · 14 comments

Comments

@sockeqwe
Copy link

As far as I have seen there is no support for inheritance. I have worked on a very similar project (annotation procession based json parser) with features like you implemented (you have more features though, but the basic features are the same). However, I have implemented Inheritance resolution similar like Jackson did. Since my annotation processor is in alpha and I consider your parser as more advanced, I consider to switch over to your parser and I would like to ask you if you would accept pull requests for inheritance or are you currently working on that? Or does your roadmap specifies inheritance for a much later version like 2.0.0?

If you are interested in we can specify together the annotations.

@EricKuck
Copy link
Member

Inheritance is actually fully supported already. Just annotate the parent and child as @JsonObjects and LoganSquare will parse and serialize everything like you'd expect it to. Do you have a specific use case that it doesn't handle correctly?

@sockeqwe
Copy link
Author

sorry i described things unclear. I mean if the json provides type information, for example:

@JsonObject
class Animal {
    @JsonField
     String name;
}

@JsonObject
class Cat extends Animal {

     @JsonField
      String coatColor;
}

@JsonObject
class Dog extends Animal {

     @JsonField
      String breed;
}

The json could look like this:

[
   {
     "type" : "dog",
     "breed": "German shepherd"
   },
   {
     "type" : "cat",
     "coatColor": "black"
   }
]

So the parser should be able to distinguish the type from json. In my json parser you can do that like this:

@JsonObject
class Animal {

    @JsonField
     String name;

      @JsonFiled( subTypes = {
            @JsonSubType( key = "dog", value = Dog.class),
            @JsonSubType( key = "cat", value = Cat.class),
          }
      )
      String type;
}

The parser should be able to parse List<Animal> (containing a Dog and a Cat) from json that contains the type information

@EricKuck
Copy link
Member

This is an interesting use case. I guess I haven't encountered an API that can spit out different types of data within the same array (even if they are all of the same superclass), but there's nothing in the JSON standard saying that can't happen.

I'll have to think through what a good annotation schema would be and what the annotation processing would look like to be able to handle something like this. Off the top of my head, I can't think of a way to do this without a big performance penalty.

PS - Just to keep the issue tracker clean and easy to read, could you change the title of this issue to something like "Add handling of collections with polymorphic types"? Right now it looks like you're requesting something that's already there.

@sockeqwe sockeqwe changed the title Inheritance Add handling of collections with polymorphic types Feb 18, 2015
@zoltish
Copy link

zoltish commented Feb 19, 2015

Is it possible to manually override this behaviour - e.g. with a TypeAdapter?

For example adding a field when serializing that indicates which class each object maps to, and similarly creating the correct class when deserializing?

@h6ah4i
Copy link

h6ah4i commented Mar 20, 2015

Hi. I am also facing this issue and I've just made an example code which handles polymorphic object parsing/serialization.

https://github.com/h6ah4i/logansquare_polymorphicparser

@zoltish
Copy link

zoltish commented Mar 20, 2015

@h6ah4i Thanks, how is it performance wise?

@h6ah4i
Copy link

h6ah4i commented Mar 21, 2015

@zoltish I have not measured the performance, but I think it is the optimal implementation. I'll compare the performance with another libraries :)

@lkraider
Copy link

I have a field that is sometimes a String and sometimes a JSON Object, ex:

"schedule_type":{
               "type":"Cycle",
               "value":{
                  "cycle":"21+7"
               }
            },
"schedule_type":{
               "type":"Items",
               "value":"1,2,3"
            }

@atabouraya
Copy link

I have this case where the acted_upon_type determines how acted_upon will be, acted_upon could be Post,Comment, Answer,Question
{ "acted_upon": {} "acted_upon_type": "Post" }
Is there a way to handle such case

@Aditya94A
Copy link

Just spent the entire day trying to pin this down, exactly the use case @sockeqwe describes. Is anyone aware of any alternative means of generating polymorphic types from a list of the common parent class?

I guess I'll have to go back to gson :/

@maccam04
Copy link

maccam04 commented Jul 24, 2017

@AdityaAnand1 @lkraider i've solve this by using this:
"schedule_type":{ value: { 1, 2,3} } & "schedule_type":{ value:"1"} }

public class Response {
@JsonField(name = "value")
public Object data;
}
then you can use instanceof depends on your needs :) like this
data instanceof List or data instanceof String.

@ursusursus
Copy link

any update on this? this could be handled by a typeconverter right?

@lhalegria
Copy link

three years passed since this issue was opened and yet no solution to the problem

@Aditya94A
Copy link

Time to get on the moshi train @lhalegria 😄

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

No branches or pull requests

10 participants