/
ProtobufJsonSerializer.java
35 lines (28 loc) · 1.04 KB
/
ProtobufJsonSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.dyuproject.protostuff.benchmark.serializers;
import java.io.IOException;
import java.nio.charset.Charset;
import com.dyuproject.protostuff.benchmark.V2SpeedMedia;
import com.google.protobuf.JsonFormat;
/**
* Copied from http://thrift-protobuf-compare.googlecode.com/svn/trunk/tpc/src/serializers/
* Tweaked a bit to subclass AbstractSpeedMediaSerializer
*
*/
public class ProtobufJsonSerializer extends AbstractSpeedMediaSerializer
{
private final Charset _charset = Charset.forName("UTF-8");
public V2SpeedMedia.MediaContent deserialize (byte[] array) throws Exception
{
V2SpeedMedia.MediaContent.Builder builder = V2SpeedMedia.MediaContent.newBuilder();
JsonFormat.merge(new String(array, _charset.name()), builder);
return builder.build();
}
public byte[] serialize(V2SpeedMedia.MediaContent content) throws IOException
{
return JsonFormat.printToString(content).getBytes(_charset.name());
}
public String getName ()
{
return "protobuf-json(JsonFormat.java)";
}
}