Java Code Examples for io.protostuff.Pipe#Schema

The following examples show how to use io.protostuff.Pipe#Schema . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testPojo() throws Exception
{
    Schema<Pojo> schema = RuntimeSchema.getSchema(Pojo.class);
    Pipe.Schema<Pojo> pipeSchema = ((RuntimeSchema<Pojo>) schema).getPipeSchema();

    Pojo p = new Pojo().fill();

    byte[] data = toByteArray(p, schema);

    Pojo pFromByteArray = new Pojo();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    Pojo pFromStream = new Pojo();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example 2
Source File: AbstractRuntimeMapTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testInlineKPolymorphicV() throws Exception
{
    Schema<HasMapInlineKPolymorphicV> schema = RuntimeSchema
            .getSchema(HasMapInlineKPolymorphicV.class);
    Pipe.Schema<HasMapInlineKPolymorphicV> pipeSchema = ((RuntimeSchema<HasMapInlineKPolymorphicV>) schema)
            .getPipeSchema();

    HasMapInlineKPolymorphicV p = new HasMapInlineKPolymorphicV().fill();

    byte[] data = toByteArray(p, schema);

    HasMapInlineKPolymorphicV pFromByteArray = new HasMapInlineKPolymorphicV();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    HasMapInlineKPolymorphicV pFromStream = new HasMapInlineKPolymorphicV();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
}
 
Example 3
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Pojo ids start at 1.
 */
@Override
public <T> Registry registerPojo(Schema<T> schema, Pipe.Schema<T> pipeSchema,
        int id)
{
    if (id >= strategy.pojos.size())
        grow(strategy.pojos, id + 1);
    else if (strategy.pojos.get(id) != null)
    {
        throw new IllegalArgumentException("Duplicate id registration: " + id +
                " (" + schema.typeClass().getName() + ")");
    }

    if (strategy.pojoMapping.containsKey(schema.typeClass()))
        throw new IllegalArgumentException("Duplicate registration for: " + schema.typeClass());

    Registered<T> wrapper = new Registered<T>(id, schema, pipeSchema, strategy);
    strategy.pojos.set(id, wrapper);

    strategy.pojoMapping.put(schema.typeClass(), wrapper);

    return this;
}
 
Example 4
Source File: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testPojoWithArray2D() throws Exception
{
    Schema<PojoWithArray2D> schema = RuntimeSchema
            .getSchema(PojoWithArray2D.class);
    Pipe.Schema<PojoWithArray2D> pipeSchema = ((RuntimeSchema<PojoWithArray2D>) schema).getPipeSchema();

    PojoWithArray2D p = new PojoWithArray2D().fill();

    byte[] data = toByteArray(p, schema);

    PojoWithArray2D pFromByteArray = new PojoWithArray2D();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoWithArray2D pFromStream = new PojoWithArray2D();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example 5
Source File: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testBat() throws Exception
{
    Schema<WrapsBat> schema = RuntimeSchema.getSchema(WrapsBat.class);
    Pipe.Schema<WrapsBat> pipeSchema = ((RuntimeSchema<WrapsBat>) schema).getPipeSchema();

    WrapsBat p = new WrapsBat().fill();

    byte[] data = toByteArray(p, schema);

    WrapsBat pFromByteArray = new WrapsBat();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    WrapsBat pFromStream = new WrapsBat();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example 6
Source File: AbstractRuntimeMapTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testEnumKInlineV() throws Exception
{
    Schema<HasMapEnumKInlineV> schema = RuntimeSchema
            .getSchema(HasMapEnumKInlineV.class);
    Pipe.Schema<HasMapEnumKInlineV> pipeSchema = ((RuntimeSchema<HasMapEnumKInlineV>) schema).getPipeSchema();

    HasMapEnumKInlineV p = new HasMapEnumKInlineV().fill();

    byte[] data = toByteArray(p, schema);

    HasMapEnumKInlineV pFromByteArray = new HasMapEnumKInlineV();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    HasMapEnumKInlineV pFromStream = new HasMapEnumKInlineV();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
}
 
Example 7
Source File: PolymorphicThrowableSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy) throws IOException
{
    final int number = input.readFieldNumber(pipeSchema.wrappedSchema);
    if (number != ID_THROWABLE)
        throw new ProtostuffException("Corrupt input.");

    transferObject(pipeSchema, pipe, input, output, strategy, number);
}
 
Example 8
Source File: PolymorphicSerializationTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testPipe() throws Exception
{
    Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class);
    Pipe.Schema<Zoo> pipeSchema = ((RuntimeSchema<Zoo>) schema).getPipeSchema();
    Zoo p = filledZoo();

    ProtostuffPipeTest.roundTrip(p, schema, pipeSchema);
}
 
Example 9
Source File: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testPojoWithImmutableListAsDelegate() throws Exception
{
    if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy)
    {
        if (((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY)
            .registerDelegate(ImmutableList.class.getName(), new ImmutableListAsDelegate<Baz>(new CollectionSchemaForBaz())))
        {
            // couldn't register
            System.err.println("registered delegate: ImmutableListAsDelegate<Baz>");
        }
    }

    Schema<PojoWithImmutableListAsDelegate> schema = RuntimeSchema
        .getSchema(PojoWithImmutableListAsDelegate.class);
    Pipe.Schema<PojoWithImmutableListAsDelegate> pipeSchema = ((RuntimeSchema<PojoWithImmutableListAsDelegate>) schema)
        .getPipeSchema();

    PojoWithImmutableListAsDelegate p = new PojoWithImmutableListAsDelegate().fill();

    byte[] data = toByteArray(p, schema);

    PojoWithImmutableListAsDelegate pFromByteArray = new PojoWithImmutableListAsDelegate();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoWithImmutableListAsDelegate pFromStream = new PojoWithImmutableListAsDelegate();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example 10
Source File: PolymorphicThrowableSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy, int number)
        throws IOException
{
    final Pipe.Schema<Object> derivedPipeSchema = strategy.transferPojoId(
            input, output, number).getPipeSchema();

    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(derivedPipeSchema, pipeSchema);
    }

    Pipe.transferDirect(derivedPipeSchema, pipe, input, output);
}
 
Example 11
Source File: ProtobufNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws IOException
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}
 
Example 12
Source File: PolymorphicPojoMapSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy, int number)
        throws IOException
{
    final Pipe.Schema<Object> derivedPipeSchema = strategy.transferPojoId(
            input, output, number).getPipeSchema();
    
    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(derivedPipeSchema, pipeSchema);
    }
    
    Pipe.transferDirect(derivedPipeSchema, pipe, input, output);
}
 
Example 13
Source File: PolymorphicPojoMapSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy) throws IOException
{
    final int number = input.readFieldNumber(pipeSchema.wrappedSchema);
    if (number != ID_POJO)
        PolymorphicMapSchema.transferObject(pipeSchema, pipe, input, output, strategy, number);
    else
        transferObject(pipeSchema, pipe, input, output, strategy, number);
}
 
Example 14
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public Pipe.Schema<T> getPipeSchema() {
	Pipe.Schema<T> pipeSchema = this.pipeSchema;
	if (pipeSchema == null) {
		synchronized (this) {
			if ((pipeSchema = this.pipeSchema) == null) {
				this.pipeSchema = pipeSchema = RuntimeSchema.resolvePipeSchema(schema, schema.typeClass(),
						true);
			}
		}
	}
	return pipeSchema;
}
 
Example 15
Source File: NullArrayElementTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testNullFirstAndLast() throws IOException
{
    Schema<PojoWithNonPrimitiveArrays> schema =
            getSchema(PojoWithNonPrimitiveArrays.class);

    Pipe.Schema<PojoWithNonPrimitiveArrays> pipeSchema =
            ((RuntimeSchema<PojoWithNonPrimitiveArrays>) schema).getPipeSchema();

    PojoWithNonPrimitiveArrays p = new PojoWithNonPrimitiveArrays(
            new Boolean[] { null, true, false, null },
            new Character[] { null, 'a', 'b', null },
            new Short[] { null, 1, 2, null },
            new Integer[] { null, 1, 2, null },
            new Long[] { null, 1l, 2l, null },
            new Float[] { null, 1.1f, 2.2f, null },
            new Double[] { null, 1.1d, 2.2d, null },
            new String[] { null, "a", "b", null },
            new ByteString[] { null, ByteString.copyFromUtf8("a"), ByteString.copyFromUtf8("b"), null },
            new byte[][] { null, new byte[] { 'a' }, new byte[] { 'b' }, null },
            new BigDecimal[] { null, new BigDecimal(1.1d), new BigDecimal(2.2d), null },
            new BigInteger[] { null, new BigInteger("1"), new BigInteger("2"), null },
            new Date[] { null, new Date(), new Date(), null },
            new Size[] { null, Size.MEDIUM, Size.LARGE, null },
            new SomePojo[] { null, new SomePojo("a"), new SomePojo("b"), null }
            );

    byte[] data = toByteArray(p, schema);

    PojoWithNonPrimitiveArrays pFromByteArray = schema.newMessage();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoWithNonPrimitiveArrays pFromStream = schema.newMessage();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example 16
Source File: ProtobufNullArrayElementTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws IOException
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}
 
Example 17
Source File: NullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public void testNullMid() throws IOException
{
    Schema<PojoWithNonPrimitiveArrays> schema =
            getSchema(PojoWithNonPrimitiveArrays.class);

    Pipe.Schema<PojoWithNonPrimitiveArrays> pipeSchema =
            ((RuntimeSchema<PojoWithNonPrimitiveArrays>) schema).getPipeSchema();

    Object[] objectArray = new Object[] {
            new Boolean[] { true, null, false },
            Arrays.asList(new Boolean[] { true, null, false }),
            
            new Character[] { 'a', null, 'b' },
            Arrays.asList(new Character[] { 'a', null, 'b' }),
            
            new Short[] { 1, null, 2 },
            Arrays.asList(new Short[] { 1, null, 2 }),
            
            new Integer[] { 1, null, 2 },
            Arrays.asList(new Integer[] { 1, null, 2 }),
            
            new Long[] { 1l, null, 2l },
            Arrays.asList(new Long[] { 1l, null, 2l }),
            
            new Float[] { 1.1f, null, 2.2f },
            Arrays.asList(new Float[] { 1.1f, null, 2.2f }),
            
            new Double[] { 1.1d, null, 2.2d },
            Arrays.asList(new Double[] { 1.1d, null, 2.2d }),
            
            new String[] { "a", null, "b" },
            Arrays.asList(new String[] { "a", null, "b" }),
            
            new ByteString[] { ByteString.copyFromUtf8("a"), null, ByteString.copyFromUtf8("b") },
            Arrays.asList(new ByteString[] { ByteString.copyFromUtf8("a"), null, ByteString.copyFromUtf8("b") }),
            
            new byte[][] { new byte[] { 'a' }, null, new byte[] { 'b' } },
            Arrays.asList(new byte[][] { new byte[] { 'a' }, null, new byte[] { 'b' } }),
            
            new BigDecimal[] { new BigDecimal(1.1d), null, new BigDecimal(2.2d) },
            Arrays.asList(new BigDecimal[] { new BigDecimal(1.1d), null, new BigDecimal(2.2d) }),
            
            new BigInteger[] { new BigInteger("1"), null, new BigInteger("2") },
            Arrays.asList(new BigInteger[] { new BigInteger("1"), null, new BigInteger("2") }),
            
            new Date[] { new Date(), null, new Date() },
            Arrays.asList(new Date[] { new Date(), null, new Date() }),
            
            new Baz[] { new Baz(0, "0", 0), null, new Baz(1, "1", 1) },
            Arrays.asList(new Baz[] { new Baz(0, "0", 0), null, new Baz(1, "1", 1) })
    };
    PojoWithNonPrimitiveArrays p = new PojoWithNonPrimitiveArrays(objectArray);

    byte[] data = toByteArray(p, schema);

    PojoWithNonPrimitiveArrays pFromByteArray = schema.newMessage();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertArrayObjectEquals(p, pFromByteArray);

    PojoWithNonPrimitiveArrays pFromStream = schema.newMessage();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertArrayObjectEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}
 
Example 18
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
public Pipe.Schema<Object> getPipeSchema()
{
    return pipeSchema;
}
 
Example 19
Source File: PolymorphicMapSchema.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
public Pipe.Schema<Object> getPipeSchema()
{
    return pipeSchema;
}
 
Example 20
Source File: NullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public void testNullLast() throws IOException
{
    Schema<PojoWithNonPrimitiveArrays> schema =
            getSchema(PojoWithNonPrimitiveArrays.class);

    Pipe.Schema<PojoWithNonPrimitiveArrays> pipeSchema =
            ((RuntimeSchema<PojoWithNonPrimitiveArrays>) schema).getPipeSchema();

    Object[] objectArray = new Object[] {
            new Boolean[] { true, false, null },
            Arrays.asList(new Boolean[] { true, false, null }),
            
            new Character[] { 'a', 'b', null },
            Arrays.asList(new Character[] { 'a', 'b', null }),
            
            new Short[] { 1, 2, null },
            Arrays.asList(new Short[] { 1, 2, null }),
            
            new Integer[] { 1, 2, null },
            Arrays.asList(new Integer[] { 1, 2, null }),
            
            new Long[] { 1l, 2l, null },
            Arrays.asList(new Long[] { 1l, 2l, null }),
            
            new Float[] { 1.1f, 2.2f, null },
            Arrays.asList(new Float[] { 1.1f, 2.2f, null }),
            
            new Double[] { 1.1d, 2.2d, null },
            Arrays.asList(new Double[] { 1.1d, 2.2d, null }),
            
            new String[] { "a", "b", null },
            Arrays.asList(new String[] { "a", "b", null }),
            
            new ByteString[] { ByteString.copyFromUtf8("a"), ByteString.copyFromUtf8("b"), null },
            Arrays.asList(new ByteString[] { ByteString.copyFromUtf8("a"), ByteString.copyFromUtf8("b"), null }),
            
            new byte[][] { new byte[] { 'a' }, new byte[] { 'b' }, null },
            Arrays.asList(new byte[][] { new byte[] { 'a' }, new byte[] { 'b' }, null }),
            
            new BigDecimal[] { new BigDecimal(1.1d), new BigDecimal(2.2d), null },
            Arrays.asList(new BigDecimal[] { new BigDecimal(1.1d), new BigDecimal(2.2d), null }),
            
            new BigInteger[] { new BigInteger("1"), new BigInteger("2"), null },
            Arrays.asList(new BigInteger[] { new BigInteger("1"), new BigInteger("2"), null }),
            
            new Date[] { new Date(), new Date(), null },
            Arrays.asList(new Date[] { new Date(), new Date(), null }),
            
            new Baz[] { new Baz(0, "0", 0), new Baz(1, "1", 1), null },
            Arrays.asList(new Baz[] { new Baz(0, "0", 0), new Baz(1, "1", 1), null })
    };
    PojoWithNonPrimitiveArrays p = new PojoWithNonPrimitiveArrays(objectArray);

    byte[] data = toByteArray(p, schema);

    PojoWithNonPrimitiveArrays pFromByteArray = schema.newMessage();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertArrayObjectEquals(p, pFromByteArray);

    PojoWithNonPrimitiveArrays pFromStream = schema.newMessage();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertArrayObjectEquals(p, pFromStream);

    roundTrip(p, schema, pipeSchema);
}