Java源码示例:com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper
示例1
@SuppressWarnings({ "unchecked", "rawtypes" })
public ProtobufMapperWrapper(ProtobufConfig protobufConfig) {
_mapper = new ProtobufMapper(new CustomProtobufFactory());
_mapper.setSerializerFactory(new CustomBeanSerializerFactory(null));
typeCustomizationFactories = new TypeCustomizationFactory[] {
new ByteCustomizationFactory(),
new ShortCustomizationFactory(),
new BooleanCustomizationFactory(),
new BigDecimalCustomizationFactory(_mapper),
new XMLGregorianCalendarCustomizationFactory(_mapper),
new BigIntegerCustomizationFactory(),
new EnumCustomizationFactory(protobufConfig),
new CalendarCustomizationFactory(_mapper),
new CharacterCustomizationFactory(),
new MapCustomizationFactory(_mapper),
};
ArrayProvider arrayProvider = arrayProvider();
SimpleModule module = new SimpleModule();
module.setSerializers(new CustomSimpleSerializers(arrayProvider));
module.setDeserializers(new CustomSimpleDeserializers(arrayProvider));
for (TypeCustomizationFactory factory : typeCustomizationFactories) {
for (Class<?> clazz : factory.getTargetClasses()) {
module.addSerializer(clazz, factory.createSerializer());
module.addDeserializer(clazz, factory.createDeserializer());
if (factory.getTargetProtobufDataType() != null) {
customizedDataTypes.put(clazz, factory.getTargetProtobufDataType());
}
}
}
_mapper.registerModule(module);
}
示例2
@org.junit.Test
public void test3() throws Exception {
ProtobufMapper mapper = new ProtobufMapper();
ProtobufSchemaGenerator gen = new ProtobufSchemaGenerator();
mapper.acceptJsonFormatVisitor(BigDecimalPojo.class, gen);
System.out.println(gen.getGeneratedSchema().getSource());
}
示例3
@Test
public void stringTest() throws Exception {
Pojo13 p = new Pojo13();
p.setValue1(makeString(7995));
p.setValue2(makeString(7995));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ProtobufMapper mapper = new ProtobufMapper();
mapper.writer(getSchema(mapper, p.getClass())).writeValue(bout, p);
}
示例4
@Test
@Ignore
public void testProtobufParser() throws Exception {
JsonPathListener mockListener = mock(JsonPathListener.class);
ObjectMapper mapper = new ProtobufMapper();
String protobuf_str = "message Employee {\n"
+ " required string name = 1;\n"
+ " required int32 age = 2;\n"
+ " repeated string emails = 3;\n"
+ " optional Employee boss = 4;\n"
+ "}\n";
final ProtobufSchema schema = ProtobufSchemaLoader.std.parse(protobuf_str);
Employee empl = new Employee();
empl.age = 30;
empl.emails = new String[]{"[email protected]"};
empl.name = "foo";
byte[] protobufData = mapper.writer(schema)
.writeValueAsBytes(empl);
// TODO Jackson's bug to be fixed in 2.9.6
JsonSurfer protobufSurfer = new JsonSurfer(new JacksonParser(new ProtobufFactory(), schema), provider);
SurfingConfiguration config = protobufSurfer.configBuilder().bind("$.name", mockListener).build();
protobufSurfer.surf(new ByteArrayInputStream(protobufData), config);
verify(mockListener).onValue(eq(provider.primitive("foo")), any(ParsingContext.class));
}
示例5
protected ProtobufMapper mapper() {
return _mapper;
}
示例6
@Test
public void test() throws Exception {
ProtobufMapper mapper = new ProtobufMapper();
ProtobufSchema schema = getSchema(mapper, Level1.class);
System.out.println(schema.getSource());
Level1 level1 = new Level1();
Level2 level2 = new Level2();
Level3 level3a = new Level3();
Level3 level3b = new Level3();
// level1.setValue(1);
level2.setValue(2);
level3a.setValue(3);
level3b.setValue(4);
List<Level3> level3s = Arrays.asList(level3a, level3b);
level1.setLevel2(level2);
level2.setLevel3s(level3s);
ByteArrayOutputStream bout = new ByteArrayOutputStream() {
@Override
public synchronized void write(byte[] b, int off, int len) {
for (int i = 0; i < len; i++) {
System.out.print(String.format("%8s", Integer.toHexString(b[off + i])).substring(6, 8).replaceAll(" ", "0") + " ");
}
System.out.println();
super.write(b, off, len);
}
};
mapper.writer(schema).writeValue(bout, level1);
showBytes(bout.toByteArray());
Level1 gotLevel1 = mapper.readerFor(Level1.class).with(schema).readValue(new ByteArrayInputStream(bout.toByteArray()));
// byte[] correct = new byte[]{0x08, 0x01, 0x12, 0x0a, 0x08, 0x02, 0x12, 0x02, 0x08, 0x03, 0x12, 0x02, 0x08, 0x04};
// Level1 gotLevel1 = mapper.readerFor(Level1.class).with(schema).readValue(new ByteArrayInputStream(correct));
// assertEquals(level1.getValue(), gotLevel1.getValue());
assertEquals(level2.getValue(), gotLevel1.getLevel2().getValue());
assertEquals(level3s.size(), gotLevel1.getLevel2().getLevel3s().size());
assertEquals(level3a.getValue(), gotLevel1.getLevel2().getLevel3s().get(0).getValue());
assertEquals(level3b.getValue(), gotLevel1.getLevel2().getLevel3s().get(1).getValue());
// assertEquals(level2.getValue(), gotLevel2.getValue());
// assertEquals(level3s.size(), gotLevel2.getLevel3s().size());
// assertEquals(level3a.getValue(), gotLevel2.getLevel3s().get(0).getValue());
// assertEquals(level3b.getValue(), gotLevel2.getLevel3s().get(1).getValue());
}