Giter Site home page Giter Site logo

Comments (19)

nmorel avatar nmorel commented on July 29, 2024 1

The properties are mapped correctly. The only thing Jakcson does not see is your JsonTypeInfo annotation. You can create a wrapper like this :

public class ListWrapper extends List<MyEntityWithJsonTypeInfo> {
  public ListWrapper(List<? extends MyEntityWithJsonTypeInfo> list) {
     super(list); // may need a cast
  }
}

Or a little more generic :

public class Wrapper<T> {
  private final List<T> values;

  public Wrapper(List<T> values) {
    this.values=values;
  }

  @JsonTypeInfo(....)
  public List<T> getValues() {
    return values;
  }
}

Jackson will still see Wrapper and List but you annotated the property values with the JsonTypeInfo so it should see it.

And other way is to add JsonTypeInfo to Object via mix in annotation. This way, Jackson will add the type info to any class.

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

I can't implement the solution. It's a problem on server-side.
gwt-jackson does not have this problem because the type is available in generators.
You have to find a workaround on server-side with your REST implementation and jackson.

As the author says in the comment, the problem seems to appear only if you use generics on root type. Use a wrapper and you should be ok.

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

Yeah I am doing that, but I now have about 35 wrapper classes :S Well I'll look into how I can do this. Thanks!

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

Maybe you can reduce to one wrapper by using your super class with the JsonTypeInfo.

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

How could I do this? (If you don't mind explaining to me haha)

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

Thanks for the reply, I tried the generic wrapper and it doesn't seem to work still

public class JacksonList<T> {
    private final List<T> elements;

    public JacksonList(List<T> elements) {
        this.elements=elements;
    }

    @JsonTypeInfo( use = JsonTypeInfo.Id.CLASS )
    public List<T> get() {
        return elements;
    }
}

Causes: com.github.nmorel.gwtjackson.client.exception.JsonDeserializationException: Cannot find the property @class containing the type information

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

That's strange. Do you have the generated json ?
I'll try to do a few test when I find some time.

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024
[
   {
      "@id":1,
      "id":1,
      "owner":null,
      "description":"fghgfhfhf fhfghfghfghfghfgh fhfhf",
      "causesSummary":null,
      "consequencesSummary":null,
      "actionsSummary":null,
      "lessonsSummary":null,
      "conclusion":"ytryuytuuuuuuuuuuu",
      "creationDate":"2014-09-18T06:00:17.167+0000",
      "date":"2014-09-18T06:00:00.000+0000",
      "quantity":null,
      "secondaryQuantity":null,
      "product":null,
      "nptg":false,
      "transportRelated":false,
      "commitOpening":false,
      "commitInvestigation":true,
      "commitReview":false,
      "processState":{
         "@class":"nz.co.doltech.ims.shared.domains.Process",
         "@id":2,
         "id":3,
         "name":"Review",
         "description":"Review and Feedback Process",
         "nametoken":"review",
         "order":3,
         "removed":false
      }
   }

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

how can it return an array when you are giving an object ?
You should have a json object with one property that is an array.

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

Not sure I completely understand what you are meaning

return new JacksonList<Incident>(incidentService.getIncidents(filter, fetchCollections)).get();

That is the call I am making, getIncidents returns a List object.

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

Oh, don't call get(). Return the JacksonList directly and rename the get() method to getValues() or anything else.

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

Oh, so I don't need to call get? Instead I need to add JacksonList as the return type? I understand now :)

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

Yes, if you don't, it's like before, you return a simple list.

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

Right of course, awesome man! That makes sense haha.

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

So I did this:

public class JacksonList<T> {
    private final ArrayList<T> elements;

    public JacksonList(ArrayList<T> elements) {
        this.elements = elements;
    }

    @JsonTypeInfo( use = JsonTypeInfo.Id.CLASS )
    public ArrayList<T> getElements() {
        return elements;
    }
}

getting this result and its still not working:

{
   "elements":[
      {
         "@id":1,
         "id":1,
         "owner":null,
         "description":"fghgfhfhf fhfghfghfghfghfgh fhfhf",
         "causesSummary":null,
         "consequencesSummary":null,
         "actionsSummary":null,
         "lessonsSummary":null,
         "conclusion":"ytryuytuuuuuuuuuuu",
         "creationDate":"2014-09-18T06:00:17.167+0000",
         "date":"2014-09-18T06:00:00.000+0000",
         "quantity":null,
         "secondaryQuantity":null,
         "product":null,
         "nptg":false,
         "transportRelated":false,
         "commitOpening":false,
         "commitInvestigation":true,
         "commitReview":false,
         "processState":{
            "@class":"nz.co.doltech.ims.shared.domains.Process",
            "@id":2,
            "id":3,
            "name":"Review",
            "description":"Review and Feedback Process",
            "nametoken":"review",
            "order":3,
            "removed":false,
            "incidentJoinProcesses":[

            ],
            "sections":[

            ]
      }
}

So sorry I'm spamming this bro, you've been such a great help. Thanks for that.

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

Well, I don't know. I just did the test and it worked for me :

public static class Wrapper<T> {

    private final List<T> values;

    public Wrapper( List<T> values ) {this.values = values;}

    @JsonTypeInfo( use = Id.CLASS )
    public List<T> getValues() {
        return values;
    }
}

public static class Toto {

    public String name = "Toto";
}

@Test
public void testTypeInfo() throws IOException {
    Wrapper<?> wrapper = new Wrapper<Toto>( Arrays.asList( new Toto() ) );
    objectMapper.writeValue( System.out, wrapper );
}

The test prints :

{"values":[{"@class":"com.github.nmorel.gwtjackson.jackson.options.WriteNullMapValuesOptionJacksonTest$Toto","name":"Toto"}]}

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

Ah well its not exactly the same as mine, I am using objects which super class is this:

@JsonTypeInfo( use = JsonTypeInfo.Id.CLASS )
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class)
public abstract class DomainObject implements Serializable, IsSerializable {

    public abstract int getId();

    public abstract void setId(int id);
}

That is the only difference I can think of here. Try add a super class with those values.

from gwt-jackson.

BenDol avatar BenDol commented on July 29, 2024

Are you able to test this with the super class?

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

Still works for me with the super class

public static class Wrapper<T> {

    private final List<T> values;

    public Wrapper( List<T> values ) {this.values = values;}

    @JsonTypeInfo( use = Id.CLASS )
    public List<T> getValues() {
        return values;
    }
}

@JsonTypeInfo( use = JsonTypeInfo.Id.CLASS )
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class)
public static abstract class DomainObject implements Serializable, IsSerializable {

    public abstract int getId();

    public abstract void setId(int id);
}

public static class Toto extends DomainObject {

    public String name = "Toto";

    @Override
    public int getId() {
        return 10;
    }

    @Override
    public void setId( int id ) {

    }
}

@Test
public void testTypeInfo() throws IOException {
    Wrapper<?> wrapper = new Wrapper<Toto>( Arrays.asList( new Toto() ) );
    objectMapper.writeValue( System.out, wrapper );
}

The output :

{
  "values" : [ {
    "@class" : "com.github.nmorel.gwtjackson.jackson.options.WriteNullMapValuesOptionJacksonTest$Toto",
    "@id" : 1,
    "name" : "Toto",
    "id" : 10
  } ]
}

from gwt-jackson.

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.