1

I am trying to Json serialize and deserialize LocalDate array in my Java class but when i generate json schema for the web service, the parameter still shows up as LocalDate rather than String.

Following is the code :

@JsonSerialize(
        contentUsing = ToStringSerializer.class
    )
    @JsonDeserialize(
        contentUsing = LocalDateFromJSON.class
    )
    private LocalDate[] amortizationDates;

and in Json schema this appears as :

amortizationDates":{"type":"array","items":{"$ref":"#/definitions/LocalDate"}}

which is wrong because it should appear as String when serialized.

Any ideas on how to serialize it as String.

Edit:

I am suing Jackson for serialization and following are serializer details :

com.fasterxml.jackson.databind.ser.std.ToStringSerializer- Jackson inbuilt

LocalDateFromJSON ->

public static class LocalDateFromJSON extends JsonDeserializer<LocalDate> {
        public LocalDateFromJSON() {
        }

        public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            return LocalDate.parse(((TextNode)jsonParser.readValueAsTree()).asText());
        }
    }
Lokesh
  • 7,810
  • 6
  • 48
  • 78
  • Do you need custom serialization format for LacalDate? Because you can just add this lib `com.fasterxml.jackson.datatype:jackson-datatype-jsr310` to make jakson support java 8 time – Kirill Liubun Jan 03 '19 at 11:09
  • check it out https://stackoverflow.com/questions/28802544/java-8-localdate-jackson-format – kj007 Jan 03 '19 at 11:21

0 Answers0