You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importjava.time.LocalDate;
importjava.util.Locale;
importjavax.json.bind.Jsonb;
importjavax.json.bind.JsonbBuilder;
importjavax.json.bind.JsonbConfig;
importjavax.json.bind.annotation.JsonbCreator;
importjavax.json.bind.annotation.JsonbDateFormat;
importjavax.json.bind.annotation.JsonbProperty;
publicclassCustomDateFormatSample {
publicstaticfinalStringJSON_DATE = "{\"localDate\":\"05-09-2017\"}";
publicstaticfinalclassDateProperty {
@JsonbDateFormat(value = "dd-MM-yyyy", locale = "nl-NL")
publicLocalDatelocalDate = LocalDate.now();
@OverridepublicStringtoString() {
returnString.format("DateProperty[%s]", localDate);
}
}
publicstaticfinalclassDateConstructor {
@JsonbDateFormat(value = "dd-MM-yyyy", locale = "nl-NL")
publicLocalDatelocalDate;
@JsonbCreatorpublicDateConstructor(@JsonbProperty("localDate") LocalDatelocalDate) {
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; } */@OverridepublicStringtoString() {
returnString.format("DateConstructor[%s]", localDate);
}
}
publicstaticfinalclassDateFactory {
publicLocalDatelocalDate;
privateDateFactory(LocalDatelocalDate) {
this.localDate = localDate;
}
@JsonbCreatorpublicstaticDateFactorywithLocalDate(@JsonbProperty("localDate") LocalDatelocalDate) {
returnnewDateFactory(localDate);
}
@OverridepublicStringtoString() {
returnString.format("DateFactory[%s]", localDate);
}
}
publicstaticvoidmain(String[] args) {
try {
System.out.println("Handle class with plain date field with custom format");
System.out.println();
handleClassWithPlainDateField();
System.out.println();
} catch (Exceptione) {
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 (Exceptione) {
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 (Exceptione) {
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();
}
}
privatestaticvoidhandleClassWithPlainDateField() {
Jsonbjsonb = JsonbBuilder.create();
DatePropertydateProperty = newDateProperty();
System.out.println(jsonb.toJson(dateProperty));
DatePropertydatePropertyFromJson = jsonb.fromJson(JSON_DATE, DateProperty.class);
System.out.println(datePropertyFromJson);
System.out.println();
}
privatestaticvoidhandleClassWithPlainDateFieldSetThroughJsonbCreatorConstructor() {
Jsonbjsonb = JsonbBuilder.create();
DateConstructordateConstructor = newDateConstructor(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. */DateConstructordateConstructorFromJson = jsonb.fromJson(JSON_DATE, DateConstructor.class);
System.out.println(dateConstructorFromJson);
System.out.println();
}
privatestaticvoidhandleClassWithPlainDateFieldSetThroughJsonbCreatorFactoryMethod() {
/* * 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. */JsonbConfigconfig = newJsonbConfig().withDateFormat("dd-MM-yyyy", newLocale("nl", "NL"));
Jsonbjsonb = JsonbBuilder.create(config);
DateFactorydateFactory = 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. */DateFactorydateFactoryFromJson = jsonb.fromJson(JSON_DATE, DateFactory.class);
System.out.println(dateFactoryFromJson);
System.out.println();
}
}
The text was updated successfully, but these errors were encountered:
Fixing introspecting creator parameters in attached pull request, I am not sure though we should also merge customization from property field / getter / setter.
Use case:
The text was updated successfully, but these errors were encountered: