Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jkranigx opened this issue May 17, 2024 · 2 comments · Fixed by #441
Closed

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

jkranigx opened this issue May 17, 2024 · 2 comments · Fixed by #441
Assignees
Labels
enhancement New feature or request

Comments

@jkranigx
Copy link

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
Copy link
Contributor

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

@jkranigx
Copy link
Author

jkranigx commented May 22, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants