0

I'm in a scenario where I'm converting from type A to type B. Type A has a date field which is of type YearMonth, and type B's date field is a String. I don't want to reinvent the wheel, so it'd be great if I could use The Jackson DataType JSR310 library for this conversion.

However, I'm quite confused how I can use the YearMonthSerializer's public serialize method in a standalone way; same goes for YearMonthDeserializer's public deserialize method.

serialize takes in a YearMonth (fine) alonside a JsonGenerator and SerializationProvider (what?) which I'm not sure how to retrieve, whereas derialize doesn't even have a String as an argument, just a JsonParser and DeserializationContext.

I don't want to use this library in the typical way @JsonSerialize(using = YearMonthSerializer.class) because I'm not converting an entire POJO to a JSON sting, just one YearMonth field to a String.

Based on these APIs though it almost looks like I am definitely not intended to use the library in this desired way.

Here's a link to the javadoc.

Ozymandias
  • 2,533
  • 29
  • 33

1 Answers1

0

why not you use the @JsonSerialize(using = YearMonthSerializer.class) above the date field instead of above the class. because you don't want to use it for the entire POJO

  • Adding the annotation doesn't serialize a value, it tells the Jackson serializer how to serialize that field when trying to serialize the entire POJO. The thing is though, I'm not trying to serialize the entire POJO, just the one field. – Ozymandias Oct 20 '19 at 03:28
  • Anyway, you are going to convert from type A to type B, in that convertor why not you use a convertor for that field. or you gonna do it using a predefined one. If so YearMonth.toString() will give you the string – Mohamed Ismail M Oct 20 '19 at 04:40
  • Yeah I should probably just make a predefined converter, since it's just the year and month. Literally two integers. – Ozymandias Oct 20 '19 at 04:57