redis / redis-om-spring

Spring Data Redis extensions for better search, documents models, and more
MIT License
609 stars 94 forks source link

Support for java.time.YearMonth Gson (TypeAdapter) #440

Closed jkranigx closed 6 months ago

jkranigx commented 6 months ago

Good Afternoon,

As of release 0.9.1 Redis OM for Spring (Java) supports JSON serialization/deserialization for both the java.time.LocalDate and java.time.LocalDateTime types. Would it be possible to add support for java.time.YearMonth? I have written my own TypeAdapter (see below), but for others it would be a time saver. Thanks for your time :)

public final class YearMonthGsonTypeAdapter extends TypeAdapter {

 private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

 @Override
 public void write(JsonWriter writer, YearMonth yearMonth) throws IOException {

     if (yearMonth == null) {

         writer.nullValue();
         return;
     }

     writer.value(yearMonth.format(formatter));
 }

 @Override
 public YearMonth read(JsonReader reader) throws IOException {

     if (reader.peek() == JsonToken.NULL) {
         reader.nextNull();
         return null;
     }

     final String yearMonthStr = reader.nextString();
     return YearMonth.parse(yearMonthStr, formatter);
 }

}

bsbodden commented 6 months ago

@jkranigx Added a YearMonth adapter based on your code. Cheers

jkranigx commented 6 months ago

Thanks! Much appreciated.

On Wed, May 22, 2024 at 8:36 AM Brian Sam-Bodden @.***> wrote:

@jkranigx https://github.com/jkranigx Added a YearMonth adapter based on your code. Cheers

— Reply to this email directly, view it on GitHub https://github.com/redis/redis-om-spring/issues/440#issuecomment-2124686154, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACS2ZZ7GZ3OZ3ILNNGK3UGTZDSGNDAVCNFSM6AAAAABH4Q4JC6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRUGY4DMMJVGQ . You are receiving this because you were mentioned.Message ID: @.***>