-
Notifications
You must be signed in to change notification settings - Fork 121
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 #78 from robreeves/joda-localtime
Added support for joda LocalTime serialization
- Loading branch information
Showing
3 changed files
with
103 additions
and
1 deletion.
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
55 changes: 55 additions & 0 deletions
55
src/main/java/de/javakaffee/kryoserializers/jodatime/JodaLocalTimeSerializer.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,55 @@ | ||
package de.javakaffee.kryoserializers.jodatime; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
import org.joda.time.Chronology; | ||
import org.joda.time.DateTimeZone; | ||
import org.joda.time.LocalTime; | ||
import org.joda.time.chrono.*; | ||
|
||
/** | ||
* A format for Joda {@link LocalTime}, that stores the milliseconds of the day and chronology | ||
* as separate attributes. | ||
* <p> | ||
* The following chronologies are supported: | ||
* <ul> | ||
* <li>{@link ISOChronology}</li> | ||
* <li>{@link CopticChronology}</li> | ||
* <li>{@link EthiopicChronology}</li> | ||
* <li>{@link GregorianChronology}</li> | ||
* <li>{@link JulianChronology}</li> | ||
* <li>{@link IslamicChronology}</li> | ||
* <li>{@link BuddhistChronology}</li> | ||
* <li>{@link GJChronology}</li> | ||
* </ul> | ||
* </p> | ||
* | ||
* @author <a href="mailto:robertpreeves@gmail.com">Rob Reeves</a> | ||
*/ | ||
public class JodaLocalTimeSerializer extends Serializer<LocalTime> { | ||
@Override | ||
public void write(Kryo kryo, Output output, LocalTime object) { | ||
final int time = object.getMillisOfDay(); | ||
output.writeInt(time, true); | ||
|
||
//LocalTime always converts the internal DateTimeZone to UTC so there is no need to serialize it. | ||
final String chronologyId = IdentifiableChronology.getChronologyId(object.getChronology()); | ||
output.writeString(chronologyId); | ||
} | ||
|
||
@Override | ||
public LocalTime read(Kryo kryo, Input input, Class<LocalTime> type) { | ||
final int time = input.readInt(true); | ||
final Chronology chronology = IdentifiableChronology.readChronology(input); | ||
|
||
//LocalTime always converts the internal DateTimeZone to UTC. | ||
return new LocalTime(time, chronology.withZone(DateTimeZone.UTC)); | ||
} | ||
|
||
@Override | ||
public LocalTime copy(Kryo kryo, LocalTime original) { | ||
return new LocalTime(original); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/de/javakaffee/kryoserializers/jodatime/JodaLocalTimeSerializerTest.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,45 @@ | ||
package de.javakaffee.kryoserializers.jodatime; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import org.joda.time.*; | ||
import org.joda.time.chrono.GregorianChronology; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
import static de.javakaffee.kryoserializers.KryoTest.deserialize; | ||
import static de.javakaffee.kryoserializers.KryoTest.serialize; | ||
|
||
/** | ||
* Tests for {@link JodaLocalTimeSerializer}. | ||
* | ||
* @author <a href="mailto:robertpreeves@gmail.com">Rob Reeves</a> | ||
*/ | ||
public class JodaLocalTimeSerializerTest { | ||
private Kryo _kryo; | ||
|
||
@BeforeTest | ||
protected void beforeTest() { | ||
_kryo = new Kryo(); | ||
_kryo.register(LocalTime.class, new JodaLocalTimeSerializer()); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testJodaLocalTime() { | ||
final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles"); | ||
final Chronology chronology = GregorianChronology.getInstance(tz); | ||
final LocalTime obj = new LocalTime(null, chronology); | ||
final byte[] serialized = serialize(_kryo, obj); | ||
final LocalTime deserialized = deserialize(_kryo, serialized, LocalTime.class); | ||
Assert.assertEquals(deserialized, obj); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testCopyJodaLocalTime() { | ||
final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles"); | ||
final Chronology chronology = GregorianChronology.getInstance(tz); | ||
final LocalTime obj = new LocalTime(52341234, chronology); | ||
final LocalTime copy = _kryo.copy(obj); | ||
Assert.assertEquals(copy, obj); | ||
} | ||
} |