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

JsonTypeInfo.As.EXISTING_PROPERTY does not seem to work while serializing an object #1669

Closed
shrey-garg opened this issue Jun 17, 2017 · 2 comments

Comments

@shrey-garg
Copy link

Hi,

I am trying to serialize an object into json so that I can send the type info in an existing field of the class. I am using v2.6.2 and also tried using 2.8.x, but the problem still persists.

    B b = new B();
    b.setSomething("Test");
    String o = JSONObjectMapper.getObjectMapper().writeValueAsString(b);
    System.out.println(o);
    String s = "{ \"type\":\"B\", \"something\":\"else\" }";
    A a = JSONObjectMapper.getObjectMapper().readValue(s, A.class);
    System.out.println(a instanceof B);

Class A:

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
    @JsonSubTypes({
            @JsonSubTypes.Type(value = B.class, name = "B")
    })
    public abstract class A {
          public String type;

          public String getType() {
                return type;
          }

          public void setType(String type) {
                this.type = type;
          }
   }

Class B:

    public class B extends A {
            private String something;

            public String getSomething() {
                   return something;
             }

           public void setSomething(String something) {
                   this.something = something;
           }
    }

The output of the above program is:

    {"something":"Test","type":null}
    true

The type field is not being set when serializing but it's being recognized while deserializing the Object.

If I change EXISTING_PROPERTY to PROPERTY, it's working as expected and adding "type" twice, once with the info and once without.

@kulabun
Copy link
Contributor

kulabun commented Jun 17, 2017

B b = new B();
b.setSomething("Test");
String o = JSONObjectMapper.getObjectMapper().writeValueAsString(b);
System.out.println(o);
String s = "{ \"type\":\"B\", \"something\":\"else\" }";
A a = JSONObjectMapper.getObjectMapper().readValue(s, A.class);
System.out.println(a instanceof B);

Where do you set property "type" for b? Nowhere, so it is null. Existing property used, so providing valid value is your responsibility.

The type field is not being set when serializing

Do you mean serializer have to set it for your bean? Change it's state? The one and only responsibility of serializer is extract your data and output it to specified format. That it what it does.

@shrey-garg
Copy link
Author

Where do you set property "type" for b? Nowhere, so it is null. Existing property used, so providing valid value is your responsibility.

Do you mean serializer have to set it for your bean? Change it's state? The one and only responsibility of serializer is extract your data and output it to specified format. That it what it does.

Thanks for the clarification, makes sense.

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