Skip to content

Instantly share code, notes, and snippets.

@knoguchi
Created May 14, 2017 19:32
Show Gist options
  • Select an option

  • Save knoguchi/1ac2c621f27beeb3711fa397d06c31b1 to your computer and use it in GitHub Desktop.

Select an option

Save knoguchi/1ac2c621f27beeb3711fa397d06c31b1 to your computer and use it in GitHub Desktop.
package com.fasterxml.jackson.dataformat.protobuf.schema;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.protobuf.ProtobufFactory;
import com.fasterxml.jackson.dataformat.protobuf.ProtobufTestBase;
import java.io.IOException;
import java.io.StringReader;
public class MessageResolutionTest extends ProtobufTestBase
{
static String proto1 =
"syntax = \"proto2\";\n"
+ "package mypackage;\n"
+ "\n"
+ "message Main {\n"
+ " required Other o = 1;\n"
+ "}\n"
+ "\n"
+ "message Other {\n"
+ " required int32 f = 1;\n"
+ "}\n";
static String proto2 =
"syntax = \"proto2\";\n"
+ "package mypackage;\n"
+ "\n"
+ "message Other {\n"
+ " required int32 f = 1;\n"
+ "}\n"
+ "\n"
+ "message Main {\n"
+ " required Other o = 1;\n"
+ "}\n";
static class Main
{
public Other o;
protected Main() { }
public Main(Other o)
{
this.o = o;
}
}
static class Other
{
public int f;
protected Other() { }
public Other(int f)
{
this.f = f;
}
}
private final ObjectMapper MAPPER = new ObjectMapper(new ProtobufFactory());
public void testParsing1() throws Exception
{
byte[] bytes = serializeTest(proto1);
assertNotNull(bytes);
}
public void testParsing2() throws Exception
{
byte[] bytes = serializeTest(proto2);
assertNotNull(bytes);
}
private byte[] serializeTest(String proto) throws IOException
{
// create PB binary from known .proto schema
ProtobufSchema schema = ProtobufSchemaLoader.std.load(new StringReader(proto));
System.out.println(schema.toString());
final ObjectWriter w = MAPPER.writerFor(Main.class).with(schema);
Other o = new Other(123);
Main m = new Main(o);
return w.writeValueAsBytes(m);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment