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

Date format customisation is ignored if custom instantiation is used #43

Closed
m0mus opened this Issue Sep 12, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@m0mus
Member

m0mus commented Sep 12, 2017

Use case:

import java.time.LocalDate;
import java.util.Locale;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.json.bind.annotation.JsonbCreator;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.json.bind.annotation.JsonbProperty;

public class CustomDateFormatSample {
    public static final String JSON_DATE = "{\"localDate\":\"05-09-2017\"}";

    public static final class DateProperty {
        @JsonbDateFormat(value = "dd-MM-yyyy", locale = "nl-NL")
        public LocalDate localDate = LocalDate.now();

        @Override
        public String toString() {
            return String.format("DateProperty[%s]", localDate);
        }
    }

    public static final class DateConstructor {
        @JsonbDateFormat(value = "dd-MM-yyyy", locale = "nl-NL")
        public LocalDate localDate;

        @JsonbCreator
        public DateConstructor(@JsonbProperty("localDate") LocalDate localDate) {
            this.localDate = localDate;
        }

        /*- This has the same error as the constructor without @JsonbDateFormat
        @JsonbCreator
        public DateConstructor(@JsonbProperty("localDate") @JsonbDateFormat(value = "dd-MM-yyyy", locale = "nl-NL") LocalDate localDate) {
            this.localDate = localDate;
        }
        */

        @Override
        public String toString() {
            return String.format("DateConstructor[%s]", localDate);
        }
    }

    public static final class DateFactory {
        public LocalDate localDate;

        private DateFactory(LocalDate localDate) {
            this.localDate = localDate;
        }

        @JsonbCreator
        public static DateFactory withLocalDate(@JsonbProperty("localDate") LocalDate localDate) {
            return new DateFactory(localDate);
        }

        @Override
        public String toString() {
            return String.format("DateFactory[%s]", localDate);
        }
    }

    public static void main(String[] args) {

        try {
            System.out.println("Handle class with plain date field with custom format");
            System.out.println();
            handleClassWithPlainDateField();
            System.out.println();
        } catch (Exception e) {
            System.out.println();
            System.out.println("Exception handling class with plain date field with custom format:");
            e.printStackTrace();
            System.out.println();
        }

        try {
            System.out.println(
                    "Handle class with plain date field with custom format set through JsonbCreator constructor");
            System.out.println();
            handleClassWithPlainDateFieldSetThroughJsonbCreatorConstructor();
            System.out.println();
        } catch (Exception e) {
            System.out.println();
            System.out.println(
                    "Exception handling class with plain date field with custom format set through JsonbCreator constructor:");
            e.printStackTrace();
            System.out.println();
        }

        try {
            System.out.println(
                    "Handle class with plain date field with custom format set through JsonbCreator factory method");
            System.out.println();
            handleClassWithPlainDateFieldSetThroughJsonbCreatorFactoryMethod();
            System.out.println();
        } catch (Exception e) {
            System.out.println();
            System.out.println(
                    "Exception handling class with plain date field with custom format set through JsonbCreator factory method:");
            e.printStackTrace();
            System.out.println();
        }
    }

    private static void handleClassWithPlainDateField() {
        Jsonb jsonb = JsonbBuilder.create();

        DateProperty dateProperty = new DateProperty();
        System.out.println(jsonb.toJson(dateProperty));
        DateProperty datePropertyFromJson = jsonb.fromJson(JSON_DATE, DateProperty.class);
        System.out.println(datePropertyFromJson);
        System.out.println();
    }

    private static void handleClassWithPlainDateFieldSetThroughJsonbCreatorConstructor() {
        Jsonb jsonb = JsonbBuilder.create();

        DateConstructor dateConstructor = new DateConstructor(LocalDate.now());
        System.out.println(jsonb.toJson(dateConstructor));
        /*
         * Exception on the next line. The AbstractDateTimeDeserializer does not use a
         * custom date formatter, and tries to parse JSON_DATE with
         * LocalDateTypeDeserializer.parseDefault as ISO_LOCAL_DATE.
         */
        DateConstructor dateConstructorFromJson = jsonb.fromJson(JSON_DATE, DateConstructor.class);
        System.out.println(dateConstructorFromJson);
        System.out.println();
    }

    private static void handleClassWithPlainDateFieldSetThroughJsonbCreatorFactoryMethod() {
        /*
         * With the custom date format through an annotation on the field fails like in
         * the previous examples. Check here with the custom date format set through the
         * JsonbConfig.
         */
        JsonbConfig config = new JsonbConfig().withDateFormat("dd-MM-yyyy", new Locale("nl", "NL"));
        Jsonb jsonb = JsonbBuilder.create(config);

        DateFactory dateFactory = DateFactory.withLocalDate(LocalDate.now());
        System.out.println(jsonb.toJson(dateFactory));
        /*
         * Exception on the next line. The AbstractDateTimeDeserializer does not use a
         * custom date formatter, and tries to parse JSON_DATE with
         * LocalDateTypeDeserializer.parseDefault as ISO_LOCAL_DATE.
         */
        DateFactory dateFactoryFromJson = jsonb.fromJson(JSON_DATE, DateFactory.class);
        System.out.println(dateFactoryFromJson);
        System.out.println();
    }
}
@bravehorsie

This comment has been minimized.

Show comment
Hide comment
@bravehorsie

bravehorsie Sep 14, 2017

Contributor

Fixing introspecting creator parameters in attached pull request, I am not sure though we should also merge customization from property field / getter / setter.

Contributor

bravehorsie commented Sep 14, 2017

Fixing introspecting creator parameters in attached pull request, I am not sure though we should also merge customization from property field / getter / setter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment