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

WRITE_DATES_WITH_ZONE_ID feature not working when applied on @JsonFormat annotation #83

Closed
mandyWW opened this issue Mar 30, 2016 · 2 comments
Milestone

Comments

@mandyWW
Copy link

mandyWW commented Mar 30, 2016

Copied over from FasterXML/jackson-databind#1175 as was assigned to wrong module.

Firstly thank you for a fantastic library :-) Just one small issue:-

I am using Jackson 2.7.3 and I would like to specify the date format pattern and whether or not to display timezone information on a per property basis. Therefore I have specified

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss", with = JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID)

on my date property - see example class below:-

private static class DummyClassWithDate {
        @JsonProperty("name")
        private String name;
        @JsonProperty("age")
        private int age;
        @JsonProperty("date")
        @JsonFormat
                (shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss Z", with = JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID)
        private DateTime date;

        public DummyClassWithDate() {

        }

        public DummyClassWithDate(String name, int age, DateTime date) {
            this.name = name;
            this.age = age;
            this.date = date;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public DateTime getDate() {
            return date;
        }

        public void setName(String aName) {
            name = aName;
        }

        public void setAge(int anAge) {
            age = anAge;
        }

        public void setDate(DateTime aDate) {
            date = aDate;
        }

    }

However, although the pattern is being used, the timezone is not being preserved when serialized. Here is a unit test which shows the problem:

    private final DummyClassWithDate dummyClassWithDateIncludingTimezone = new DummyClassWithDate("Test", 50, new DateTime(2015, 11, 23, 22, 06, 39, DateTimeZone.forID("Asia/Krasnoyarsk")));

    @Test
    public void testJacksonAnnotatedPOJOWithDateWithTimezoneToJson() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JodaModule());

        assertEquals("{\"name\":\"Test\",\"age\":50,\"date\":\"23-11-2015 10:06:39 +0800[Asia/Krasnoyarsk]\"}",mapper.writeValueAsString(dummyClassWithDateIncludingTimezone));
    }

and the results of running the test:

junit.framework.ComparisonFailure: 
Expected :{"name":"Test","age":50,"date":"23-11-2015 10:06:39 +0800[Asia/Krasnoyarsk]"}
Actual   :{"name":"Test","age":50,"date":"23-11-2015 02:06:39 +0000"}

If I change my test to add in:

mapper.configure(SerializationFeature.WRITE_DATES_WITH_ZONE_ID, true);
this fixes the issue however this is applying the setting to dates on ALL classes and I really only want it applied to specific properties.

@cowtowncoder
Copy link
Member

Thanks! Sounds like some piece of configuration handling is missing from Joda deserializer classes. It might be easiest to compare jsr310 module's implementation wrt @JsonFormat since I think latter has slightly more up-to-date support, and it is possible some improvements have not been merged.

@cowtowncoder cowtowncoder modified the milestones: 2.3.1, 2.8.0 Jul 1, 2016
@cowtowncoder
Copy link
Member

Implemented, will be in 2.8.0 to be released soon.

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