-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Serializer and Deserializer for RangeSet (#50)
- Loading branch information
1 parent
7caed4c
commit b6ecd7e
Showing
5 changed files
with
124 additions
and
0 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
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
45 changes: 45 additions & 0 deletions
45
guava/src/main/java/com/fasterxml/jackson/datatype/guava/deser/RangeSetDeserializer.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 com.fasterxml.jackson.datatype.guava.deser; | ||
|
||
import com.fasterxml.jackson.core.JsonParseException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.BeanProperty; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.google.common.collect.ImmutableRangeSet; | ||
import com.google.common.collect.Range; | ||
import com.google.common.collect.RangeSet; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class RangeSetDeserializer extends JsonDeserializer<RangeSet<Comparable<?>>> { | ||
private JavaType genericRangeListType; | ||
|
||
@Override | ||
public RangeSet<Comparable<?>> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
if (genericRangeListType == null) { | ||
throw new JsonParseException(p, "RangeSetJsonSerializer was not contextualized (no deserialize target type). " + | ||
"You need to specify the generic type down to the generic parameter of RangeSet."); | ||
} else { | ||
@SuppressWarnings("unchecked") final Iterable<Range<Comparable<?>>> ranges | ||
= (Iterable<Range<Comparable<?>>>) ctxt | ||
.findContextualValueDeserializer(genericRangeListType, null).deserialize(p, ctxt); | ||
ImmutableRangeSet.Builder<Comparable<?>> builder = ImmutableRangeSet.builder(); | ||
for (Range<Comparable<?>> range : ranges) { | ||
builder.add(range); | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) { | ||
final JavaType genericType = ctxt.getContextualType().containedType(0); | ||
if (genericType == null) return this; | ||
final RangeSetDeserializer deserializer = new RangeSetDeserializer(); | ||
deserializer.genericRangeListType = ctxt.getTypeFactory().constructCollectionType(List.class, | ||
ctxt.getTypeFactory().constructParametricType(Range.class, genericType)); | ||
return deserializer; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
guava/src/main/java/com/fasterxml/jackson/datatype/guava/ser/RangeSetSerializer.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,36 @@ | ||
package com.fasterxml.jackson.datatype.guava.ser; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.BeanProperty; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.google.common.collect.Range; | ||
import com.google.common.collect.RangeSet; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class RangeSetSerializer extends JsonSerializer<RangeSet<Comparable<?>>> { | ||
private JavaType genericRangeListType; | ||
|
||
@Override | ||
public void serialize(RangeSet<Comparable<?>> value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
if (genericRangeListType == null) { | ||
serializers.findValueSerializer(List.class).serialize(value.asRanges(), gen, serializers); | ||
} else { | ||
serializers.findValueSerializer(genericRangeListType).serialize(value.asRanges(), gen, serializers); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) { | ||
if (property == null) return this; | ||
final RangeSetSerializer serializer = new RangeSetSerializer(); | ||
serializer.genericRangeListType = prov.getTypeFactory() | ||
.constructCollectionType(List.class, | ||
prov.getTypeFactory().constructParametricType( | ||
Range.class, property.getType().containedType(0))); | ||
return serializer; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
guava/src/test/java/com/fasterxml/jackson/datatype/guava/TestRangeSet.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,35 @@ | ||
package com.fasterxml.jackson.datatype.guava; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
import com.fasterxml.jackson.databind.type.TypeFactory; | ||
import com.google.common.collect.Range; | ||
import com.google.common.collect.RangeSet; | ||
import com.google.common.collect.TreeRangeSet; | ||
|
||
import java.io.IOException; | ||
|
||
public class TestRangeSet extends ModuleTestBase { | ||
|
||
private final ObjectMapper MAPPER = mapperWithModule(); | ||
|
||
public void testSerializeDeserialize() throws IOException { | ||
|
||
final RangeSet<Integer> rangeSet = TreeRangeSet.create(); | ||
rangeSet.add(Range.closedOpen(1, 2)); | ||
rangeSet.add(Range.openClosed(3, 4)); | ||
|
||
final String json = MAPPER.writeValueAsString(rangeSet); | ||
|
||
final TypeFactory tf = MAPPER.getTypeFactory(); | ||
final JavaType type = tf.constructParametricType(RangeSet.class, Integer.class); | ||
final ObjectReader reader = MAPPER.readerFor(type); | ||
|
||
final RangeSet<Integer> deserialized = reader.readValue(json); | ||
|
||
assertEquals(rangeSet, deserialized); | ||
|
||
} | ||
|
||
} |