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

@JsonUnwrapped is not treated as assuming @JsonProperty("") #1013

Closed
david-bakin opened this issue Nov 20, 2015 · 2 comments
Closed

@JsonUnwrapped is not treated as assuming @JsonProperty("") #1013

david-bakin opened this issue Nov 20, 2015 · 2 comments
Milestone

Comments

@david-bakin
Copy link

See discussion here but basically @JsonUnwrapped on a private field by itself does not cause that field to be serialized, currently, You need to add an explicit @JsonProperty. You shouldn't have to do that. (Following test fails currently, should pass, though you can make it pass by commenting out the line with @JsonProperty. Uses TestNG and AssertJ.)

package com.bakins_bits;

import static org.assertj.core.api.Assertions.assertThat;

import org.testng.annotations.Test;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJsonUnwrappedShouldMakePrivateFieldsSerializable
{
    public static class Inner
    {
        public String animal;
    }

    public static class Outer
    {
        // @JsonProperty
        @JsonUnwrapped
        private Inner inner;
    }

    @Test
    public void jsonUnwrapped_should_make_private_fields_serializable() throws JsonProcessingException {
        // ARRANGE
        Inner inner = new Inner();
        inner.animal = "Zebra";

        Outer outer = new Outer();
        outer.inner = inner;

        ObjectMapper sut = new ObjectMapper();

        // ACT
        String actual = sut.writeValueAsString(outer);

        // ASSERT
        assertThat(actual).contains("animal");
        assertThat(actual).contains("Zebra");
        assertThat(actual).doesNotContain("inner");
    }
}
@cowtowncoder cowtowncoder added this to the 2.6.4 milestone Nov 20, 2015
@david-bakin
Copy link
Author

Should @JsonIgnore have been included too?

@cowtowncoder
Copy link
Member

@david-bakin didn't think so, as it drops definition? Including that may have some side-effects.

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