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 support #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

6 changes: 2 additions & 4 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions .idea/modules.xml

This file was deleted.

18 changes: 0 additions & 18 deletions gson-javatime-serialisers.iml

This file was deleted.

26 changes: 17 additions & 9 deletions src/main/java/com/fatboyindustrial/gsonjavatime/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.*;

/**
* The {@code Converters} class contains static methods for registering Java Time converters.
Expand All @@ -52,6 +44,9 @@ public class Converters
/** The specific genericized type for {@code LocalTime}. */
public static final Type LOCAL_TIME_TYPE = new TypeToken<LocalTime>(){}.getType();

/** The specific genericized type for {@code YearMonth}. */
private static final Type YEAR_MONTH_TYPE = TypeToken.get(YearMonth.class).getType();

/** The specific genericized type for {@code OffsetDateTime}. */
public static final Type OFFSET_DATE_TIME_TYPE = new TypeToken<OffsetDateTime>(){}.getType();

Expand Down Expand Up @@ -82,6 +77,7 @@ public static GsonBuilder registerAll(GsonBuilder builder)
registerLocalDate(builder);
registerLocalDateTime(builder);
registerLocalTime(builder);
registerYearMonth(builder);
registerOffsetDateTime(builder);
registerOffsetTime(builder);
registerZonedDateTime(builder);
Expand Down Expand Up @@ -116,6 +112,18 @@ public static GsonBuilder registerLocalDateTime(GsonBuilder builder)
return builder;
}

/**
* Registers the {@link YearMonthConverter} converter.
* @param builder The GSON builder to register the converter with.
* @return A reference to {@code builder}.
*/
public static GsonBuilder registerYearMonth(GsonBuilder builder)
{
builder.registerTypeAdapter(YEAR_MONTH_TYPE, new YearMonthConverter());

return builder;
}

/**
* Registers the {@link LocalTimeConverter} converter.
* @param builder The GSON builder to register the converter with.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.fatboyindustrial.gsonjavatime;

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 java.lang.reflect.Type;
import java.time.YearMonth;

/**
* GSON serialiser/deserialiser for converting {@link YearMonth} objects.
*/
public class YearMonthConverter implements JsonSerializer<YearMonth>, JsonDeserializer<YearMonth> {

/**
* Gson invokes this call-back method during serialization when it encounters a field of the
* specified type. <p>
*
* In the implementation of this call-back method, you should consider invoking
* {@link JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
* non-trivial field of the {@code src} object. However, you should never invoke it on the
* {@code src} object itself since that will cause an infinite loop (Gson will call your
* call-back method again).
*
* @param src the object that needs to be converted to Json.
* @param typeOfSrc the actual type (fully genericized version) of the source object.
* @return a JsonElement corresponding to the specified object.
*/
@Override
public JsonElement serialize(YearMonth src, Type typeOfSrc, JsonSerializationContext jsonSerializationContext) {
return new JsonPrimitive(src.toString());
}

/**
* Gson invokes this call-back method during deserialization when it encounters a field of the
* specified type. <p>
*
* In the implementation of this call-back method, you should consider invoking
* {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects
* for any non-trivial field of the returned object. However, you should never invoke it on the
* same type passing {@code json} since that will cause an infinite loop (Gson will call your
* call-back method again).
*
* @param json The Json data being deserialized
* @param typeOfT The type of the Object to deserialize to
* @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
* @throws JsonParseException if json is not in the expected format of {@code typeOfT}
*/
@Override
public YearMonth deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
return YearMonth.parse(json.getAsString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@
import com.google.gson.JsonPrimitive;
import org.junit.Test;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.*;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
Expand All @@ -59,6 +51,7 @@ public void testSerialisation() throws Exception
container.ld = LocalDate.of(1969, 7, 21);
container.lt = LocalTime.of(12, 56, 0);
container.ldt = LocalDateTime.of(container.ld, container.lt);
container.ym = YearMonth.of(2020, 11);
container.odt = OffsetDateTime.of(container.ld, container.lt, ZoneOffset.ofHours(10));
container.ot = OffsetTime.of(container.lt, ZoneOffset.ofHours(10));
container.zdt = ZonedDateTime.of(container.ld, container.lt, ZoneId.of("Australia/Brisbane"));
Expand All @@ -71,6 +64,7 @@ public void testSerialisation() throws Exception
assertThat(json.get("ld").getAsString(), is("1969-07-21"));
assertThat(json.get("lt").getAsString(), is("12:56:00"));
assertThat(json.get("ldt").getAsString(), is("1969-07-21T12:56:00"));
assertThat(json.get("ym").getAsString(), is("2020-11"));
assertThat(json.get("odt").getAsString(), is("1969-07-21T12:56:00+10:00"));
assertThat(json.get("ot").getAsString(), is("12:56:00+10:00"));
assertThat(json.get("zdt").getAsString(), is("1969-07-21T12:56:00+10:00[Australia/Brisbane]"));
Expand All @@ -90,6 +84,7 @@ public void testDeserialisation() throws Exception
container.ld = LocalDate.of(1969, 7, 21);
container.lt = LocalTime.of(12, 56, 0);
container.ldt = LocalDateTime.of(container.ld, container.lt);
container.ym = YearMonth.of(2020, 11);
container.odt = OffsetDateTime.of(container.ld, container.lt, ZoneOffset.ofHours(10));
container.ot = OffsetTime.of(container.lt, ZoneOffset.ofHours(10));
container.zdt = ZonedDateTime.of(container.ld, container.lt, ZoneId.of("Australia/Brisbane"));
Expand All @@ -100,6 +95,7 @@ public void testDeserialisation() throws Exception
serialized.add("ld", new JsonPrimitive("1969-07-21"));
serialized.add("lt", new JsonPrimitive("12:56:00"));
serialized.add("ldt", new JsonPrimitive("1969-07-21T12:56:00"));
serialized.add("ym", new JsonPrimitive("2020-11"));
serialized.add("odt", new JsonPrimitive("1969-07-21T12:56:00+10:00"));
serialized.add("ot", new JsonPrimitive("12:56:00+10:00"));
serialized.add("zdt", new JsonPrimitive("1969-07-21T12:56:00+10:00[Australia/Brisbane]"));
Expand All @@ -112,6 +108,7 @@ public void testDeserialisation() throws Exception
assertThat(deserialised.ld, is(container.ld));
assertThat(deserialised.ldt, is(container.ldt));
assertThat(deserialised.lt, is(container.lt));
assertThat(deserialised.ym, is(container.ym));
assertThat(deserialised.odt, is(container.odt));
assertThat(deserialised.ot, is(container.ot));
assertThat(deserialised.zdt, is(container.zdt));
Expand All @@ -127,6 +124,7 @@ private static class Container
private LocalDate ld;
private LocalDateTime ldt;
private LocalTime lt;
private YearMonth ym;
private OffsetDateTime odt;
private OffsetTime ot;
private ZonedDateTime zdt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.fatboyindustrial.gsonjavatime;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.junit.Test;

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

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class YearMonthConverterTest {

/** The specific genericized type for {@code YearMonth}. */
private static final Type YEAR_MONTH_TYPE = TypeToken.get(YearMonth.class).getType();

/**
* Tests that serialising to JSON works.
*/
@Test
public void testSerialisation(){
final Gson gson = registerYearMonth(new GsonBuilder()).create();

final YearMonth yearMonth = YearMonth.of(2020, 11);
final String json = gson.toJson(yearMonth, YearMonth.class);

assertThat(json, is("\"2020-11\""));
}

/**
* Tests that deserialising from JSON works.
*/
@Test
public void testDeserialisation(){
final Gson gson = registerYearMonth(new GsonBuilder()).create();

final String json = "\"2020-11\"";
final YearMonth yearMonth = gson.fromJson(json, YearMonth.class);

assertThat(yearMonth, is(YearMonth.of(2020, 11)));
}

/**
* Registers the {@link YearMonthConverter} converter.
* @param builder The GSON builder to register the converter with.
* @return A reference to {@code builder}.
*/
private static GsonBuilder registerYearMonth(GsonBuilder builder){
builder.registerTypeAdapter(YEAR_MONTH_TYPE, new YearMonthConverter());

return builder;
}
}