Giter Site home page Giter Site logo

Comments (10)

jasminb avatar jasminb commented on August 16, 2024 1

Hey @joaofyz, if the API does not make a type difference between Car and Vehicle than your model should not do it.

What you can do is the following:

  • have one class that has all the attributes
  • create a factory that will create specific subclass depending on the attributes

Now I understand that these are design choices that you made for a reason, sadly it is not possible to create different object having same type attribute.

from jsonapi-converter.

jasminb avatar jasminb commented on August 16, 2024

Hello,

This is not related to the JSONAPI spec but rather to jackson library.

http://wiki.fasterxml.com/JacksonPolymorphicDeserialization

Regards

from jsonapi-converter.

joaofyz avatar joaofyz commented on August 16, 2024

Thanks jasminb for your answer.

I was able to solve my problem with the @JsonTypeInfo and @JsonSubTypes annotations.

The only issue I identified was the need to have always de @id annotation and field in every class even if it already exists in the Super class.

from jsonapi-converter.

jasminb avatar jasminb commented on August 16, 2024

Hey,

I've pushed a change today that solves the inheritance issue you are referring to.
If you use SNAPSHOT version of the library, it will work with inherited @id field.

Regards

from jsonapi-converter.

joaofyz avatar joaofyz commented on August 16, 2024

I do not use the SNAPSHOT version but I think you can close this issue.

Thanks for your help

from jsonapi-converter.

joaofyz avatar joaofyz commented on August 16, 2024

Hi,

I'm having another problem with the hierarchical classes, now serializing the objects.

All my objects in JSONAPI are from the same type @type("object_type"), but I can not have the same @type annotation in the main and child classes. How can I do this?

Thanks

from jsonapi-converter.

jasminb avatar jasminb commented on August 16, 2024

Hey @joaofyz,

You cant inherit type (have multiple classes of the same type), however you can have a base class that has most of your attributes and than extend it and specify the type and additional attributes that you need.

When you deserialise you can do the following:

MyBaseClass object = resourceConverter.readDocument(data, MyBaseClass.class);

object will be instance of the correct subtype depending on the type attribute present in the data.

As for serialisation, no problem there, just writeDocument as usual.

from jsonapi-converter.

joaofyz avatar joaofyz commented on August 16, 2024

I don't think this will solve my issue. Let me try to explain what I want to archive:

What I have now is the following:

One class Vehicle
@Type("vehicle") public class Vehicle { ... }

And a subclass Car
@Type("car") public class Car { ... }

I would like to have the @type("vehicle") in the car also.

Thanks for the help again ;)

from jsonapi-converter.

jasminb avatar jasminb commented on August 16, 2024

Can you give more background on why would you want to have same type on multiple classes?

from jsonapi-converter.

joaofyz avatar joaofyz commented on August 16, 2024

Hi @jasminb

In the business prespective they are all "vehicles", so they are all mapped has vehicles.

Do you think I will be able to do this?

from jsonapi-converter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.