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 #1175

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

Comments

@mandyWW
Copy link

mandyWW commented Mar 30, 2016

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

Sounds like missing handling here. Since this is related to (and most likely caused by ) Joda module, could you re-create the issue over here:

https://github.com/FasterXML/jackson-datatype-joda/issues

so it's easier to track?

Thank you for reporting this.

@mandyWW
Copy link
Author

mandyWW commented Mar 30, 2016

Thanks for the fast response! Yes of course - have raised FasterXML/jackson-datatype-joda#83.

@mandyWW mandyWW closed this as completed Mar 30, 2016
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