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

Serialization of the field with deserialization config #323

Closed
metanet opened this issue Oct 12, 2013 · 9 comments
Closed

Serialization of the field with deserialization config #323

metanet opened this issue Oct 12, 2013 · 9 comments
Milestone

Comments

@metanet
Copy link

metanet commented Oct 12, 2013

Hello,

(I am copying the issue from FasterXML/jackson-core#112 with @cowtowncoder 's suggestion)

I am talking about jackson version 1.9.13.

Here is the demonstration of the bug.

public class TestClass { 

    private final int a;

    public TestClass (final int a )
    {
        this.a = a;
    }

    @JsonProperty("b")
    private int getA ()
    {
        return a;
    }
}

When I serialize new TestClass( 5 ), I get this json: {"b":5}

Now I am changing my code to this version:

public class TestClass { 

    private final int a;

    public TestClass (@JsonProperty("a") final int a )
    {
        this.a = a;
    }

    @JsonProperty("b")
    private int getA ()
    {
        return a;
    }
}

When I serialize new TestClass( 5 ), I get this json: {"a":5,"b":5}
Here, I am not expecting the field "a" in the serialized json, I only expect it during the deserialization.

Because of the legacy data, I needed to serialize and deserialize a field with different names. My code was working fine with jackson 1.8 version. When I updated to jackson 1.9, this bug appeared and possibly screwed up various parts of my dataset (which is in size of a couple of hundred million records).

By the way, this bug does not exist in Jackson 2.2.

@cowtowncoder
Copy link
Member

Interesting. One thing I am wondering is whether missing @JsonCreator from constructor matters here -- it certainly makes it likely that constructor is never used (and deserializer instead tries to modify field: JVM allows this, with some constraints!).

@cowtowncoder
Copy link
Member

Hmmh. I actually get an exception with 2.2.3.

@mtyurt
Copy link

mtyurt commented Jan 29, 2014

Any improvements on this issue?

@cowtowncoder
Copy link
Member

No. If anyone has time feel free to work on it.

@cowtowncoder
Copy link
Member

Interesting: looks like test would work as expected for 2.4 (master) -- I added test case, it is passing.

@cowtowncoder
Copy link
Member

Checked with 2.3: still failing with 2.3.2. So this is due to a recent fix, although indirect one: most likely one for #369.

@cowtowncoder
Copy link
Member

Status with 2.4 is bit unclear; only now fully understanding where the problem comes from -- difficulties in differentiating between implicit (from underlying accessor name) and explicit (annotation) names, and how conflict resolution deals with the two.

cowtowncoder added a commit that referenced this issue Apr 15, 2014
@cowtowncoder
Copy link
Member

Ok. After fixes to handling of implicit names, at this point I realize that there is a real conflict: field a could either be renamed along with setter or constructor. So it is necessary to add explicit @JsonProperty for it; or, to use @JsonIgnore to remove it from consideration: both approaches should work.

I will modify test accordingly to verify that above-mentioned approaches work.

@cowtowncoder cowtowncoder added this to the 2.4.0 milestone May 6, 2014
@cowtowncoder
Copy link
Member

Tests pass, as long as field a is explicitly named (for example as either a or b), or marked as to be ignored. This with 2.4.0, soon to be released.

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

3 participants