Search in sources :

Example 11 with BaseMarshaller

use of org.infinispan.protostream.BaseMarshaller in project protostream by infinispan.

the class SerializationContextImplTest method testMarshallerProvider.

@Test
public void testMarshallerProvider() throws Exception {
    SerializationContext ctx = createContext();
    String file = "package test;\n" + "message X {\n" + "   optional int32 f = 1;\n" + "}";
    class X {

        Integer f;

        private X(Integer f) {
            this.f = f;
        }
    }
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource().addProtoFile("file.proto", file);
    ctx.registerProtoFiles(fileDescriptorSource);
    ctx.registerMarshallerProvider(new SerializationContext.MarshallerProvider() {

        @Override
        public BaseMarshaller<?> getMarshaller(String typeName) {
            if (typeName.equals("test.X")) {
                return makeMarshaller();
            }
            return null;
        }

        @Override
        public BaseMarshaller<?> getMarshaller(Class<?> javaClass) {
            if (javaClass == X.class) {
                return makeMarshaller();
            }
            return null;
        }

        private BaseMarshaller<?> makeMarshaller() {
            return new ProtobufTagMarshaller<X>() {

                @Override
                public X read(ReadContext ctx) throws IOException {
                    Integer f = null;
                    TagReader in = ctx.getReader();
                    if (in.readTag() == WireType.makeTag(1, WireType.WIRETYPE_VARINT)) {
                        f = in.readInt32();
                    }
                    return new X(f);
                }

                @Override
                public void write(WriteContext ctx, X x) throws IOException {
                    ctx.getWriter().writeInt32(1, x.f);
                }

                @Override
                public Class<X> getJavaClass() {
                    return X.class;
                }

                @Override
                public String getTypeName() {
                    return "test.X";
                }
            };
        }
    });
    byte[] bytes = ProtobufUtil.toWrappedByteArray(ctx, new X(1234));
    Object out = ProtobufUtil.fromWrappedByteArray(ctx, bytes);
    assertTrue(out instanceof X);
    assertNotNull(((X) out).f);
    assertEquals(1234, ((X) out).f.intValue());
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) IOException(java.io.IOException) TagReader(org.infinispan.protostream.TagReader) BaseMarshaller(org.infinispan.protostream.BaseMarshaller) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Aggregations

BaseMarshaller (org.infinispan.protostream.BaseMarshaller)11 SerializationContext (org.infinispan.protostream.SerializationContext)4 ArrayList (java.util.ArrayList)3 ApplicationScoped (javax.enterprise.context.ApplicationScoped)3 Produces (javax.enterprise.inject.Produces)3 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)3 IOException (java.io.IOException)2 MetadataKeyMarshaller (org.commonjava.indy.pkg.maven.content.marshaller.MetadataKeyMarshaller)2 MetadataMarshaller (org.commonjava.indy.pkg.maven.content.marshaller.MetadataMarshaller)2 StoreKeyMarshaller (org.commonjava.indy.pkg.maven.content.marshaller.StoreKeyMarshaller)2 StoreTypeMarshaller (org.commonjava.indy.pkg.maven.content.marshaller.StoreTypeMarshaller)2 SerializationContextInitializer (org.infinispan.protostream.SerializationContextInitializer)2 MapSession (org.springframework.session.MapSession)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 Properties (java.util.Properties)1