-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from pavanpvpk/ZoneIdConverter
Add ZoneId converter
- Loading branch information
Showing
6 changed files
with
244 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ target | |
.idea/libraries/ | ||
.idea/scala_settings.xml | ||
.idea/dictionaries/ | ||
*.ipr | ||
*.iws | ||
|
||
# Mac | ||
.DS_Store |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/com/fatboyindustrial/gsonjavatime/ZoneIdConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2014 Greg Kopff | ||
* All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
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.ZoneId; | ||
|
||
/** | ||
* GSON serialiser/deserialiser for converting {@link ZoneId} objects. | ||
*/ | ||
public class ZoneIdConverter implements JsonSerializer<ZoneId>, JsonDeserializer<ZoneId> { | ||
|
||
/** | ||
* 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(final ZoneId src, final Type typeOfSrc, final JsonSerializationContext context) | ||
{ | ||
if (src == null) | ||
{ | ||
return null; | ||
} | ||
return new JsonPrimitive(src.getId()); | ||
} | ||
|
||
/** | ||
* 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 | ||
* 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 ZoneId deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws | ||
JsonParseException | ||
{ | ||
if (json == null) | ||
{ | ||
return null; | ||
} | ||
if (json.isJsonNull()) | ||
{ | ||
return null; | ||
} | ||
final String zoneIdentifier = json.getAsString(); | ||
if (zoneIdentifier==null || zoneIdentifier.isEmpty()) | ||
{ | ||
return null; | ||
} | ||
return ZoneId.of(zoneIdentifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/test/java/com/fatboyindustrial/gsonjavatime/ZoneIdConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2014 Greg Kopff | ||
* All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
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 org.junit.Rule; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.lang.reflect.Type; | ||
import java.time.ZoneId; | ||
import java.time.DateTimeException; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* Tests for {@link ZoneIdConverter}. | ||
*/ | ||
public class ZoneIdConverterTest { | ||
|
||
/** The specific genericized type for {@code ZoneId}. */ | ||
private static final Type ZONE_ID_TYPE = new TypeToken<ZoneId>(){}.getType(); | ||
@Rule | ||
public ExpectedException exception = ExpectedException.none(); | ||
|
||
/** | ||
* Tests that serialising to JSON works. | ||
*/ | ||
@Test | ||
public void testSerialisation() throws Exception | ||
{ | ||
final Gson gson = registerZoneId(new GsonBuilder()).create(); | ||
|
||
final ZoneId zoneId = ZoneId.of("Australia/Brisbane"); | ||
final String json= gson.toJson(zoneId, ZoneId.class); | ||
assertThat(json, is("\"Australia/Brisbane\"")); | ||
} | ||
|
||
/** | ||
* Tests that deserialising from JSON works. | ||
*/ | ||
@Test | ||
public void testDeserialisation() throws Exception | ||
{ | ||
final Gson gson = registerZoneId(new GsonBuilder()).create(); | ||
|
||
final String json = "\"Australia/Brisbane\""; | ||
final ZoneId zoneId = gson.fromJson(json, ZoneId.class); | ||
|
||
assertThat(zoneId, is(ZoneId.of("Australia/Brisbane"))); | ||
} | ||
|
||
/** | ||
* Tests that deserialising from JSON works with an empty value. | ||
*/ | ||
@Test | ||
public void testDeserialisationWithEmptyValue() throws Exception | ||
{ | ||
final Gson gson = registerZoneId(new GsonBuilder()).create(); | ||
|
||
final String json = "\"\""; | ||
final ZoneId zoneId = gson.fromJson(json, ZoneId.class); | ||
|
||
assertNull(zoneId); | ||
} | ||
|
||
/** | ||
* Tests that deserialising from JSON works with a whitespace. | ||
*/ | ||
@Test | ||
public void testDeserialisationWithWhitespace() throws Exception | ||
{ | ||
exception.expect(DateTimeException.class); | ||
exception.expectMessage("Invalid ID for ZoneOffset, invalid format: "); | ||
|
||
final Gson gson = registerZoneId(new GsonBuilder()).create(); | ||
|
||
final String json = "\" \""; | ||
final ZoneId zoneId = gson.fromJson(json, ZoneId.class); | ||
} | ||
|
||
/** | ||
* Registers the {@link ZoneIdConverter} converter. | ||
* @param builder The GSON builder to register the converter with. | ||
* @return A reference to {@code builder}. | ||
*/ | ||
private static GsonBuilder registerZoneId(GsonBuilder builder) | ||
{ | ||
builder.registerTypeAdapter(ZONE_ID_TYPE, new ZoneIdConverter()); | ||
|
||
return builder; | ||
} | ||
} |