jasminb / jsonapi-converter

JSONAPI-Converter is a Java/Android library that provides support for working with JSONAPI spec
Apache License 2.0
273 stars 81 forks source link

How to deserialize this json, when different object types. #208

Open ExtinctAmoeba opened 5 years ago

ExtinctAmoeba commented 5 years ago

Is there any deserialization options for this case? Any examples, solutions? If json contains one object, everything works as expected.

Example online: https://jsoneditoronline.org/?id=7f036c2b6b9f46a5b8c033863dd520f3

jasminb commented 5 years ago

@ExtinctAmoeba

Please format the provided JSON so it can be read properly.

ExtinctAmoeba commented 5 years ago

@ExtinctAmoeba Please format the provided JSON so it can be read properly.

Updated

jasminb commented 5 years ago

Hello @ExtinctAmoeba

To handle this case, create an interface Section and then implement it for different section types.

Use the Section interface in as a type in the class that declares relationship and you are done.

For example of this see: ResourceConverterTest#testReadPolymorphicRelationships()

ExtinctAmoeba commented 5 years ago

Hi @jasminb

I tried Your solution, based on this model Dealership.java And created Interface Sections like in your example model: Driveable.java

@Relationship("sections") private Collection<Sections> sections;

And getting error: No class was registered for type 'TitleSection'. But I have it registered in my ApiClient. Like this: .addConverterFactory(new JSONAPIConverterFactory(objectMapper, BaseResource.class, Page.class, AlgorithmSection.class, ….

I guess problem is that I'm not using right converter.

So I tried:

ObjectMapper objectMapper = new ObjectMapper().configure (DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false).configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ResourceConverter converter = new ResourceConverter(objectMapper, TitleSection.class, AlgorithmSection.class, Page.class); ….

Definitely I'm doing something wrong, because both things doesn't work.

jasminb commented 5 years ago

@ExtinctAmoeba

Ive updated README.md with an example on how to setup polymorphic relationship use-case: https://github.com/jasminb/jsonapi-converter/blob/develop/README.md#polymorphic-relationships

jasminb commented 5 years ago

@ExtinctAmoeba Hopefully you were able to resolve the issue?

ExtinctAmoeba commented 5 years ago

@jasminb Yes, thank You very much for help.

ExtinctAmoeba commented 5 years ago

Hi, again. After inserting one more relationship, parser "ReadPolymorphicRelationships" ignores it. For example: https://jsoneditoronline.org/?id=0fcc4f3912654feca9d759016e7e56bb

I'm getting nullpointer on getTags.

I tried this:

@Relationship(value = "tags", resolve = true, path = "/relationships/tags", relatedPath = "tags") private List<Tag> tags;

@Relationship(value = "sections", resolve = true, path = "/relationships/sections", relatedPath = "sections") private List<Sections> sections;

But it didn't work.

ExtinctAmoeba commented 5 years ago

@jasminb I reopened this issue, could You please look at it?

jasminb commented 5 years ago

@ExtinctAmoeba, I will take a look and update you. It would be helpful if you could provide model classes so I don't have to do it myself.

ExtinctAmoeba commented 5 years ago

@ExtinctAmoeba, I will take a look and update you. It would be helpful if you could provide model classes so I don't have to do it myself.

Okay, give me some time.

ExtinctAmoeba commented 5 years ago

@jasminb There in .zip archive I placed all related models + resourceConverter. If You need anything else, just say it :)

Models.zip