-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to serialize and deserialize Guava's Immutable{Double,Int…
…,Long}Array (#86)
- Loading branch information
Showing
14 changed files
with
458 additions
and
21 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
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
23 changes: 23 additions & 0 deletions
23
...com/fasterxml/jackson/datatype/guava/deser/primitives/BaseImmutableArrayDeserializer.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,23 @@ | ||
package com.fasterxml.jackson.datatype.guava.deser.primitives; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.datatype.guava.deser.BasePrimitiveCollectionDeserializer; | ||
|
||
public abstract class BaseImmutableArrayDeserializer<ObjectType, ImmutablePrimitiveArray, IntermediateArrayBuilder> | ||
extends BasePrimitiveCollectionDeserializer<ObjectType, ImmutablePrimitiveArray, IntermediateArrayBuilder> { | ||
|
||
protected BaseImmutableArrayDeserializer(Class<? extends ImmutablePrimitiveArray> cls, Class<? super ObjectType> itemType) { | ||
super(cls, itemType); | ||
} | ||
|
||
@Override | ||
protected final void add(IntermediateArrayBuilder intermediateBuilder, JsonParser parser, DeserializationContext context) throws JacksonException { | ||
collect(intermediateBuilder, asPrimitive(parser)); | ||
} | ||
|
||
protected abstract void collect(IntermediateArrayBuilder intermediateBuilder, ObjectType value); | ||
|
||
protected abstract ObjectType asPrimitive(JsonParser parser) throws JacksonException; | ||
} |
33 changes: 33 additions & 0 deletions
33
...m/fasterxml/jackson/datatype/guava/deser/primitives/ImmutableDoubleArrayDeserializer.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,33 @@ | ||
package com.fasterxml.jackson.datatype.guava.deser.primitives; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.datatype.guava.util.ImmutablePrimitiveTypes; | ||
import com.google.common.primitives.ImmutableDoubleArray; | ||
|
||
public class ImmutableDoubleArrayDeserializer | ||
extends BaseImmutableArrayDeserializer<Double, ImmutableDoubleArray, ImmutableDoubleArray.Builder> { | ||
public ImmutableDoubleArrayDeserializer() { | ||
super(ImmutablePrimitiveTypes.ImmutableDoubleArrayType, Double.class); | ||
} | ||
|
||
@Override | ||
protected ImmutableDoubleArray.Builder createIntermediateCollection() { | ||
return ImmutableDoubleArray.builder(); | ||
} | ||
|
||
@Override | ||
protected void collect(ImmutableDoubleArray.Builder intermediateBuilder, Double value) { | ||
intermediateBuilder.add(value); | ||
} | ||
|
||
@Override | ||
protected ImmutableDoubleArray finish(ImmutableDoubleArray.Builder builder) { | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
protected Double asPrimitive(JsonParser parser) throws JacksonException { | ||
return parser.getDoubleValue(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../com/fasterxml/jackson/datatype/guava/deser/primitives/ImmutableIntArrayDeserializer.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,33 @@ | ||
package com.fasterxml.jackson.datatype.guava.deser.primitives; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.datatype.guava.util.ImmutablePrimitiveTypes; | ||
import com.google.common.primitives.ImmutableIntArray; | ||
|
||
public class ImmutableIntArrayDeserializer | ||
extends BaseImmutableArrayDeserializer<Integer, ImmutableIntArray, ImmutableIntArray.Builder> { | ||
public ImmutableIntArrayDeserializer() { | ||
super(ImmutablePrimitiveTypes.ImmutableIntArrayType, Integer.class); | ||
} | ||
|
||
@Override | ||
protected ImmutableIntArray.Builder createIntermediateCollection() { | ||
return ImmutableIntArray.builder(); | ||
} | ||
|
||
@Override | ||
protected void collect(ImmutableIntArray.Builder intermediateBuilder, Integer value) { | ||
intermediateBuilder.add(value); | ||
} | ||
|
||
@Override | ||
protected ImmutableIntArray finish(ImmutableIntArray.Builder builder) { | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
protected Integer asPrimitive(JsonParser parser) throws JacksonException { | ||
return parser.getIntValue(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...com/fasterxml/jackson/datatype/guava/deser/primitives/ImmutableLongArrayDeserializer.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,33 @@ | ||
package com.fasterxml.jackson.datatype.guava.deser.primitives; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.datatype.guava.util.ImmutablePrimitiveTypes; | ||
import com.google.common.primitives.ImmutableLongArray; | ||
|
||
public class ImmutableLongArrayDeserializer | ||
extends BaseImmutableArrayDeserializer<Long, ImmutableLongArray, ImmutableLongArray.Builder> { | ||
public ImmutableLongArrayDeserializer() { | ||
super(ImmutablePrimitiveTypes.ImmutableLongArrayType, Long.class); | ||
} | ||
|
||
@Override | ||
protected ImmutableLongArray.Builder createIntermediateCollection() { | ||
return ImmutableLongArray.builder(); | ||
} | ||
|
||
@Override | ||
protected void collect(ImmutableLongArray.Builder intermediateBuilder, Long value) { | ||
intermediateBuilder.add(value); | ||
} | ||
|
||
@Override | ||
protected ImmutableLongArray finish(ImmutableLongArray.Builder builder) { | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
protected Long asPrimitive(JsonParser parser) throws JacksonException { | ||
return parser.getLongValue(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ava/com/fasterxml/jackson/datatype/guava/ser/primitives/BaseImmutableArraySerializer.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,24 @@ | ||
package com.fasterxml.jackson.datatype.guava.ser.primitives; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import com.fasterxml.jackson.datatype.guava.util.ImmutablePrimitiveTypes; | ||
|
||
public abstract class BaseImmutableArraySerializer<ImmutableArray> extends StdSerializer<ImmutableArray> { | ||
protected BaseImmutableArraySerializer(ImmutablePrimitiveTypes.ImmutablePrimitiveArrays immutableArrayType) { | ||
super(immutableArrayType.type()); | ||
} | ||
|
||
@Override | ||
public final void serialize(ImmutableArray immutableArray, JsonGenerator gen, SerializerProvider provider) throws JacksonException { | ||
if (immutableArray == null) { | ||
provider.defaultSerializeNullValue(gen); | ||
} else { | ||
writeArray(immutableArray, gen); | ||
} | ||
} | ||
|
||
protected abstract void writeArray(ImmutableArray immutableArray, JsonGenerator gen); | ||
} |
20 changes: 20 additions & 0 deletions
20
...a/com/fasterxml/jackson/datatype/guava/ser/primitives/ImmutableDoubleArraySerializer.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,20 @@ | ||
package com.fasterxml.jackson.datatype.guava.ser.primitives; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.datatype.guava.util.ImmutablePrimitiveTypes; | ||
import com.google.common.primitives.ImmutableDoubleArray; | ||
|
||
public class ImmutableDoubleArraySerializer extends BaseImmutableArraySerializer<ImmutableDoubleArray> { | ||
|
||
public ImmutableDoubleArraySerializer() { | ||
super(ImmutablePrimitiveTypes.ImmutablePrimitiveArrays.DOUBLE); | ||
} | ||
|
||
@Override | ||
protected void writeArray(ImmutableDoubleArray immutableArray, JsonGenerator gen) { | ||
if (!immutableArray.isEmpty()) { | ||
gen.writeArray(immutableArray.toArray(), 0, immutableArray.length()); | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...java/com/fasterxml/jackson/datatype/guava/ser/primitives/ImmutableIntArraySerializer.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,20 @@ | ||
package com.fasterxml.jackson.datatype.guava.ser.primitives; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.datatype.guava.util.ImmutablePrimitiveTypes; | ||
import com.google.common.primitives.ImmutableIntArray; | ||
|
||
public class ImmutableIntArraySerializer extends BaseImmutableArraySerializer<ImmutableIntArray> { | ||
|
||
public ImmutableIntArraySerializer() { | ||
super(ImmutablePrimitiveTypes.ImmutablePrimitiveArrays.INT); | ||
} | ||
|
||
@Override | ||
protected void writeArray(ImmutableIntArray immutableArray, JsonGenerator gen) { | ||
if (!immutableArray.isEmpty()) { | ||
gen.writeArray(immutableArray.toArray(), 0, immutableArray.length()); | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...ava/com/fasterxml/jackson/datatype/guava/ser/primitives/ImmutableLongArraySerializer.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,20 @@ | ||
package com.fasterxml.jackson.datatype.guava.ser.primitives; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.datatype.guava.util.ImmutablePrimitiveTypes; | ||
import com.google.common.primitives.ImmutableLongArray; | ||
|
||
public class ImmutableLongArraySerializer extends BaseImmutableArraySerializer<ImmutableLongArray> { | ||
|
||
public ImmutableLongArraySerializer() { | ||
super(ImmutablePrimitiveTypes.ImmutablePrimitiveArrays.LONG); | ||
} | ||
|
||
@Override | ||
protected void writeArray(ImmutableLongArray immutableArray, JsonGenerator gen) { | ||
if (!immutableArray.isEmpty()) { | ||
gen.writeArray(immutableArray.toArray(), 0, immutableArray.length()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.