Skip to content

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

@mandyWW

Description

@mandyWW

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions