Permalink
Browse files

Add Jackson/YAML backend for sake of completeness; minor cleanup for …

…jackson
  • Loading branch information...
1 parent 58e6f16 commit 58a1cc5b28f29450d1dfb32b2239c43721de37d9 @cowtowncoder cowtowncoder committed Jun 28, 2012
Binary file not shown.
@@ -10,22 +10,7 @@
import serializers.cks.CksBinary;
import serializers.cks.CksText;
import serializers.jackson.*;
-import serializers.json.JsonGsonDatabind;
-import serializers.json.JsonArgoTree;
-import serializers.json.FastJSONDatabind;
-import serializers.json.FlexjsonDatabind;
-import serializers.json.JsonGsonManual;
-import serializers.json.JsonGsonTree;
-import serializers.json.JsonDotOrgManualTree;
-import serializers.json.JsonLibJsonDatabind;
-import serializers.json.JsonPathDeserializerOnly;
-import serializers.json.JsonSimpleManualTree;
-import serializers.json.JsonSimpleWithContentHandler;
-import serializers.json.JsonSmartManualTree;
-import serializers.json.JsonTwoLattes;
-import serializers.json.JsonijJpath;
-import serializers.json.JsonijManualTree;
-import serializers.json.JsonSvensonDatabind;
+import serializers.json.*;
import serializers.protobuf.ActiveMQProtobuf;
import serializers.protobuf.Protobuf;
import serializers.protostuff.Protostuff;
@@ -110,6 +95,9 @@ protected void addTests(TestGroups groups)
JacksonBsonDatabind.register(groups);
MongoDB.register(groups);
+ // YAML (using Jackson module built on SnakeYAML)
+ JacksonYAMLDatabind.register(groups);
+
// XML-based formats.
XmlStax.register(groups, true, true, true); // woodstox/aalto/fast-infoset
XmlXStream.register(groups);
@@ -5,17 +5,23 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectReader;
+import com.fasterxml.jackson.databind.ObjectWriter;
public abstract class BaseJacksonDataBind<T> extends BaseJacksonDriver<T>
{
protected final JavaType type;
protected final ObjectMapper mapper;
+ protected final ObjectReader reader;
+ protected final ObjectWriter writer;
protected BaseJacksonDataBind(String name, Class<T> clazz, ObjectMapper mapper)
{
super(name);
type = mapper.getTypeFactory().constructType(clazz);
this.mapper = mapper;
+ reader = mapper.reader(type);
+ writer = mapper.writerWithType(type);
}
protected final JsonParser constructParser(byte[] data) throws IOException {
@@ -0,0 +1,20 @@
+package serializers.jackson;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+
+import serializers.JavaBuiltIn;
+import serializers.TestGroups;
+
+import data.media.MediaContent;
+
+public class JacksonYAMLDatabind
+{
+ public static void register(TestGroups groups)
+ {
+ ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
+ groups.media.add(JavaBuiltIn.mediaTransformer,
+ new StdJacksonDataBind<MediaContent>("yaml/jackson",
+ MediaContent.class, mapper));
+ }
+}
@@ -16,14 +16,14 @@ public StdJacksonDataBind(String name, Class<T> clazz, ObjectMapper mapper) {
@Override
public byte[] serialize(T data) throws IOException
{
- return mapper.writeValueAsBytes(data);
+ return writer.writeValueAsBytes(data);
}
@Override
@SuppressWarnings("unchecked")
public T deserialize(byte[] array) throws IOException
{
- return (T) mapper.readValue(array, 0, array.length, type);
+ return (T) reader.readValue(array, 0, array.length);
}
// // Future extensions for testing performance for item sequences
@@ -34,7 +34,7 @@ public void serializeItems(T[] items, OutputStream out) throws IOException
JsonGenerator generator = constructGenerator(out);
// JSON allows simple sequences, so:
for (int i = 0, len = items.length; i < len; ++i) {
- mapper.writeValue(generator, items[i]);
+ writer.writeValue(generator, items[i]);
}
generator.close();
}
@@ -46,7 +46,7 @@ public void serializeItems(T[] items, OutputStream out) throws IOException
T[] result = (T[]) new Object[numberOfItems];
JsonParser parser = constructParser(in);
for (int i = 0; i < numberOfItems; ++i) {
- result[i] = (T) mapper.readValue(parser, type);
+ result[i] = (T) reader.readValue(parser, type);
}
parser.close();
return result;

0 comments on commit 58a1cc5

Please sign in to comment.