jsonapi-converter
jsonapi-converter copied to clipboard
How to deserialize this json, when different object types.
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
@ExtinctAmoeba
Please format the provided JSON so it can be read properly.
@ExtinctAmoeba Please format the provided JSON so it can be read properly.
Updated
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()
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.
@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
@ExtinctAmoeba Hopefully you were able to resolve the issue?
@jasminb Yes, thank You very much for help.
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.
@jasminb I reopened this issue, could You please look at it?
@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, 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.
@jasminb There in .zip archive I placed all related models + resourceConverter. If You need anything else, just say it :)