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

YearMonth parser would be beneficial #26

Open
nbali opened this issue May 2, 2019 · 2 comments
Open

YearMonth parser would be beneficial #26

nbali opened this issue May 2, 2019 · 2 comments

Comments

@nbali
Copy link

nbali commented May 2, 2019

No description provided.

@gkopff
Copy link
Owner

gkopff commented May 3, 2019

@nbali would you like to contribute a PR?

@nbali
Copy link
Author

nbali commented May 3, 2019

I have no time to do it properly with docs, tests and stuff nowadays, but here is the pure code itself for anyone willing to finish it:

import java.lang.reflect.Type;
import java.time.YearMonth;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

...

	private static class YearMonthConverter implements JsonSerializer<YearMonth>, JsonDeserializer<YearMonth> {
		
		private static final Type TYPE = TypeToken.get(YearMonth.class).getType();
		
		@Override
		public JsonElement serialize(YearMonth src, Type typeOfSrc, JsonSerializationContext context) {
			return new JsonPrimitive(src.toString());
		}
		
		@Override
		public YearMonth deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
			return YearMonth.parse(json.getAsString());
		}
	}

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

No branches or pull requests

2 participants