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

Object ID Deserialization #1

Closed
miguelcobain opened this issue Aug 28, 2013 · 4 comments
Closed

Object ID Deserialization #1

miguelcobain opened this issue Aug 28, 2013 · 4 comments

Comments

@miguelcobain
Copy link

Hi!

This is a great resource for those interested in using Ember and Java Web Services. Thanks!

I see you're using some clever "tricks" with jackson. It's awesome.
But one thing I couldn't manage to get is the Deserialization of relationships.
Serializations works fine.

For example. A GET to /resources returns:

{
    "resources":[
        {
            "id":1,
            "name":"abc",
            "another_resource_id":1
        },
        //...
    ]
}

The AnotherResource id gets serialized.
But when you POST a resource, with this payload, for example:

{
    "resource":{
         "name":"abc",
         "another_resource_id":5
    }
}

Do you have a successful save? I'm getting an error in Jackson. I can't deserialize using only the id. There's already a related issue with this, and I reported my problem here: FasterXML/jackson-databind#176 (comment)

How are you doing this?

@brunorg
Copy link
Owner

brunorg commented Sep 12, 2013

Hi,

Take a look on this http://wiki.fasterxml.com/JacksonFeatureObjectIdentity

I'm declaring that annotation in the POJO's classes

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class AnotherResource {
...
}

The annotation is used when deserializing to create a new object that will have the id property filled with the value posted with the json snippet.

The Resource class need some annotations in the relationship too, to make this works.

public class Resource {

    @JsonIdentityReference(alwaysAsId = true)
    @JsonProperty("another_resource_id")
    private AnotherResource anotherResource;

...
}

@miguelcobain
Copy link
Author

Yes, I understand that this works with serialization, i.e the json the server outputs is correct.
But what about the other way around? (Deserialization)
I couldn't get a successful serialization (POST request) with your code. Jackson can't resolve the id references back.

@brunorg
Copy link
Owner

brunorg commented Sep 14, 2013

You need add a method in Resource class to create a AnotherResource object from the id too.

@JsonSetter
public void setAnotherResource(Long id) {
    System.out.println("Creating AnotherResource " + id);
    this.anotherResource = new AnotherResource(id);
}

@miguelcobain
Copy link
Author

Good idea!
However I don't think this is very elegant. You must write this all over your project's entities.
A much more cleaner way would be something like what I'm trying to achieve in this SO question: http://stackoverflow.com/questions/18659835/how-to-deserialize-field-from-object-based-on-annotation-using-jackson

Thank you for your help!

@brunorg brunorg closed this as completed Oct 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants