After updating the Jackson library, the response changed, previously Json.toJSON(LocalDateTime.now()) was used, which calls ObjectMapper().valueToTree(data);
I want the view to be the same as before :
"date": {
"year": 2022,
"month": "SEPTEMBER",
"dayOfMonth": 8,
"dayOfWeek": "THURSDAY",
"dayOfYear": 251,
"monthValue": 9,
"hour": 16,
"minute": 4,
"second": 30,
"nano": 0,
"chronology": {
"calendarType": "iso8601",
"id": "ISO"
}
}
now the answer looks like this:
[2022,9,20,11,28,9,598000000]
Before updating the libraries, serialization of the LocalDateTime object in JsonNode was enough to get the right answer, now I tried in different ways -
final String jsonNode = new ObjectMapper().findAndRegisterModules().writeValueAsString(LocalDateTime.now());
return ok(Json.toJson(jsonNode));
or this without Json.toJson(jsonNode), just :
final String jsonNode = new ObjectMapper().findAndRegisterModules().writeValueAsString(LocalDateTime.now());
return ok(jsonNode);
or:
final JsonNode jsonNode = new ObjectMapper().registerModule(new JSR310Module()).valueToTree(LocalDateTime.now());
return ok(Json.toJson(jsonNode));
and various other options, which, unfortunately, do not give the desired effect even if I do not use Json.toJSON();
Json.toJson() (code):
public static JsonNode toJson(final Object data) {
try {
return mapper().valueToTree(data);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
I use Play and sbt for compilation, it is important for me not to change the code of the entire project just because of updating the libraries, but it should be noted that I needed to update them because some new modules that needed to be added are not compatible with the old version of Jackson. Dependencies look like this
libraryDependencies += "com.fasterxml.jackson.dataformat" % "jackson-dataformat-cbor" % "2.12.6"
libraryDependencies += "com.fasterxml.jackson.datatype" % "jackson-datatype-jdk8" % "2.12.6"
libraryDependencies += "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % "2.12.6"
libraryDependencies += "com.fasterxml.jackson.core" % "jackson-annotations" % "2.12.6"
libraryDependencies += "com.fasterxml.jackson.core" % "jackson-core" % "2.12.6"here
I will be very grateful for the answer, if it is possible to do this
UPD
I solved this problem like this, maybe it will be useful: first of all, I created the custom serializer:
public class LocalDateTimeKeySerializer extends StdSerializer<LocalDateTime> {
public static final LocalDateTimeKeySerializer INSTANCE = new LocalDateTimeKeySerializer();
public LocalDateTimeKeySerializer() {
super(LocalDateTime.class);
}
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeNumberField("year", value.getYear());
gen.writeNumberField("monthValue", value.getMonthValue());
gen.writeStringField("month", value.getMonth().name());
gen.writeNumberField("dayOfMonth", value.getDayOfMonth());
gen.writeNumberField("hour", value.getHour());
gen.writeNumberField("minute", value.getMinute());
gen.writeNumberField("second", value.getSecond());
gen.writeNumberField("nano", value.getNano());
gen.writeStringField("dayOfWeek", value.getDayOfWeek().name());
gen.writeNumberField("dayOfYear", value.getDayOfYear());
gen.writeEndObject();
}}
after that added this serializer in custom module:
public class JsonCustomModule extends SimpleModule {
public JsonCustomModule() {
super(PackageVersion.VERSION);
addSerializer(LocalDateTime.class, LocalDateTimeKeySerializer.INSTANCE);
}}
hope it could be useful ^^
You need to create a custom JsonSerializer
that converts the LocalDateTime
to a Calendar
, then serializes that. Untested:
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
Calendar calendar = GregorianCalendar.from(value.atZone(ZoneId.systemDefault()));
JsonSerializer<Object> calendarSerializer = serializers.findValueSerializer(calendar.getClass());
calendarSerializer.serialize(calendar, gen, serializers);
}