Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added svenson JSON databind #8

Merged
merged 1 commit into from Jun 17, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions tpc/.classpath
Expand Up @@ -5,6 +5,7 @@
<classpathentry kind="lib" path="lib/libthrift.jar"/>
<classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>
<classpathentry kind="lib" path="lib/stax-api-1.0.1.jar"/>
<classpathentry kind="lib" path="lib/commons-beanutils-1.8.3.jar"/>
<classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
Expand Down Expand Up @@ -50,5 +51,6 @@
<classpathentry kind="lib" path="lib/protostuff-json-1.0.0.M7.jar"/>
<classpathentry kind="lib" path="lib/protostuff-runtime-1.0.0.M7.jar"/>
<classpathentry kind="lib" path="lib/protostuff-xml-1.0.0.M7.jar"/>
<classpathentry kind="lib" path="lib/svenson-1.4.0.jar"/>
<classpathentry kind="output" path="build"/>
</classpath>
Binary file added tpc/lib/commons-beanutils-1.8.3.jar
Binary file not shown.
Binary file added tpc/lib/svenson-1.4.0.jar
Binary file not shown.
1 change: 1 addition & 0 deletions tpc/serializers.txt
Expand Up @@ -22,6 +22,7 @@ json/protostuff-manual
json/protostuff-runtime
json/protobuf
json/google-gson
json/svenson-databind
bson/jackson-manual
bson/jackson-databind
bson/mongodb
Expand Down
58 changes: 58 additions & 0 deletions tpc/src/serializers/SvensonJsonDatabind.java
@@ -0,0 +1,58 @@
package serializers;

import java.io.*;

import data.media.Image;
import data.media.MediaContent;

/**
* This serializer uses svenson for JSON data binding.
*/
public class SvensonJsonDatabind
{
public static void register(TestGroups groups)
{
groups.media.add(JavaBuiltIn.MediaTransformer,
new GenericSerializer<MediaContent>("json/svenson-databind", MediaContent.class));
}

static class GenericSerializer<T> extends Serializer<T>
{
private final org.svenson.JSONParser _jsonParser;
private final org.svenson.JSON _jsonWriter;
private final String name;
private final Class<T> type;

public GenericSerializer(String name, Class<T> clazz)
{
this.name = name;
type = clazz;

_jsonParser = org.svenson.JSONParser.defaultJSONParser();
_jsonParser.addTypeHint(".images[]", Image.class);
_jsonWriter = org.svenson.JSON.defaultJSON();
}

public String getName()
{
return name;
}

public T deserialize(byte[] array) throws Exception
{
org.svenson.tokenize.JSONCharacterSource source =
new org.svenson.tokenize.InputStreamSource(new ByteArrayInputStream(array), true);
T result = _jsonParser.parse(type, source);
return result;
}

public byte[] serialize(T data) throws IOException
{
ByteArrayOutputStream baos = outputStream(data);
OutputStreamWriter w = new OutputStreamWriter(baos, "UTF-8");
_jsonWriter.writeJSONToWriter(data, w);
w.close();
return baos.toByteArray();
}
}
}