Java Code Examples for io.protostuff.runtime.RuntimeSchema#getSchema()

The following examples show how to use io.protostuff.runtime.RuntimeSchema#getSchema() . 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: SerializationUtil.java    From framework with Apache License 2.0 6 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param clazz
 * @param data
 * @param <T> T
 * @return T
 * @throws UtilException <br>
 */
@SuppressWarnings("unchecked")
public static <T> T unserial(final Class<T> clazz, final byte[] data) throws UtilException {
    T result = null;
    if (data != null && data.length > 0) {
        try {
            if (Map.class.isAssignableFrom(clazz)) {
                result = (T) jdkUnserial(data);
            }
            else {
                Schema<T> schema = RuntimeSchema.getSchema(clazz);
                result = clazz.newInstance();
                ProtostuffIOUtil.mergeFrom(data, result, schema);
            }
        }
        catch (Exception e) {
            throw new UtilException(e, ErrorCodeDef.UNSERIALIZE_ERROR, DataUtil.byte2HexStr(data));
        }
    }
    return result;
}
 
Example 2
Source File: ProtoStuffSerializer.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T readObject(InputBuf inputBuf, Class<T> clazz) {
    Schema<T> schema = RuntimeSchema.getSchema(clazz);
    T msg = schema.newMessage();

    Input input = Inputs.getInput(inputBuf);
    try {
        schema.mergeFrom(input, msg);
        Inputs.checkLastTagWas(input, 0);
    } catch (IOException e) {
        ThrowUtil.throwException(e);
    } finally {
        inputBuf.release();
    }

    return msg;
}
 
Example 3
Source File: ProtoStuffSerializer.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> byte[] writeObject(T obj) {
    Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass());

    LinkedBuffer buf = LinkedBuffers.getLinkedBuffer();
    Output output = Outputs.getOutput(buf);
    try {
        schema.writeTo(output, obj);
        return Outputs.toByteArray(output);
    } catch (IOException e) {
        ThrowUtil.throwException(e);
    } finally {
        LinkedBuffers.resetBuf(buf); // for reuse
    }

    return null; // never get here
}
 
Example 4
Source File: JsonNumericRuntimeSerDeserTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testFoo() throws Exception
{
    Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);

    Foo fooCompare = foo;
    Foo dfoo = new Foo();

    byte[] data = JsonIOUtil.toByteArray(fooCompare, schema, true);
    JsonIOUtil.mergeFrom(data, dfoo, schema, true);
    SerializableObjects.assertEquals(fooCompare, dfoo);
}
 
Example 5
Source File: JsonRuntimeSerDeserTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testBaz() throws Exception
{
    Schema<Baz> schema = RuntimeSchema.getSchema(Baz.class);

    for (Baz bazCompare : new Baz[] { baz, negativeBaz })
    {
        Baz dbaz = new Baz();

        byte[] data = JsonIOUtil.toByteArray(bazCompare, schema, false);
        JsonIOUtil.mergeFrom(data, dbaz, schema, false);
        SerializableObjects.assertEquals(bazCompare, dbaz);
    }
}
 
Example 6
Source File: ProtoStuffSerializeUtil.java    From jim-framework with Apache License 2.0 5 votes vote down vote up
public static <T> T deserialize(byte[] bytes, Class<T> beanClass) {
    if (bytes == null || bytes.length == 0) {
        return null;
    }

    T instance = null;
    try {
        instance = beanClass.newInstance();
    } catch (Exception e) {
        throw new RpcException(e);
    }
    Schema<T> schema = RuntimeSchema.getSchema(beanClass);
    ProtostuffIOUtil.mergeFrom(bytes, instance, schema);
    return instance;
}
 
Example 7
Source File: JsonXNumericRuntimeTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testBar() throws Exception
{
    Schema<Bar> schema = RuntimeSchema.getSchema(Bar.class);

    for (Bar barCompare : new Bar[] { bar, negativeBar })
    {
        Bar dbar = new Bar();

        byte[] data = JsonXIOUtil.toByteArray(barCompare, schema, true, buf());
        JsonIOUtil.mergeFrom(data, dbar, schema, true);
        SerializableObjects.assertEquals(barCompare, dbar);
    }
}
 
Example 8
Source File: ProtoMarshaller.java    From faster-framework-project with Apache License 2.0 5 votes vote down vote up
public ProtoMarshaller(Type type) {
    if (type != null) {
        Class clazz;
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            clazz = ((Class) pt.getRawType());
        } else {
            clazz = (Class) type;
        }
        this.responseSchema = RuntimeSchema.getSchema(clazz);
    }
}
 
Example 9
Source File: MessageCrypto.java    From jeesupport with MIT License 5 votes vote down vote up
/**
 * 获取某个对象的实例构建器 
 * @param _cls 实例化对象的类
 * @return 对应类的实例构建器
 */
private static < T > Schema< T > _get_schema( Class< T > _cls ) {
	@SuppressWarnings( "unchecked" )
	Schema< T > schema = ( Schema< T > ) cachedSchema.get( _cls );
	if ( schema == null ) {
		schema = RuntimeSchema.getSchema( _cls );
		if ( schema != null ) {
			cachedSchema.put( _cls , schema );
		}
	}
	return schema;
}
 
Example 10
Source File: BusProtoStuffMessageConverter.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private static Object readContent(final EventType eventType, final byte[] content) {
    final Class<?> targetClass = eventType.getTargetClass();
    if (targetClass == null) {
        LOG.error("Cannot read clazz header for given EventType value {}, missing mapping", eventType.getValue());
        throw new MessageConversionException("Missing mapping of EventType for value " + eventType.getValue());
    }
    @SuppressWarnings("unchecked")
    final Schema<Object> schema = (Schema<Object>) RuntimeSchema.getSchema(targetClass);
    final Object deserializeEvent = schema.newMessage();
    ProtobufIOUtil.mergeFrom(content, deserializeEvent, schema);
    return deserializeEvent;
}
 
Example 11
Source File: JsonRuntimePipeTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testPolymorphic() throws Exception
{
    Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class);
    Pipe.Schema<Zoo> pipeSchema = ((RuntimeSchema<Zoo>) schema).getPipeSchema();

    Zoo zoo = PolymorphicSerializationTest.filledZoo();

    // numeric only
    protobufRoundTrip(zoo, schema, pipeSchema, true);
    protostuffRoundTrip(zoo, schema, pipeSchema, true);
}
 
Example 12
Source File: BusProtoStuffMessageConverter.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private static byte[] writeClassHeader(final Class<?> clazz) {
    final EventType clazzEventType = EventType.from(clazz);
    if (clazzEventType == null) {
        LOG.error("There is no mapping to EventType for the given class {}", clazz);
        throw new MessageConversionException("Missing EventType for given class : " + clazz);
    }
    @SuppressWarnings("unchecked")
    final Schema<Object> schema = (Schema<Object>) RuntimeSchema
            .getSchema((Class<?>) EventType.class);
    final LinkedBuffer buffer = LinkedBuffer.allocate();
    return ProtobufIOUtil.toByteArray(clazzEventType, schema, buffer);
}
 
Example 13
Source File: JsonXNumericRuntimeTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testPolymorphic() throws Exception
{
    Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class);
    Zoo zooCompare = PolymorphicSerializationTest.filledZoo();

    Zoo dzoo = new Zoo();

    byte[] data = JsonXIOUtil.toByteArray(zooCompare, schema, true, buf());

    JsonIOUtil.mergeFrom(data, dzoo, schema, true);
    SerializableObjects.assertEquals(zooCompare, dzoo);
}
 
Example 14
Source File: JsonRuntimePipeTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testBaz() throws Exception
{
    Schema<Baz> schema = RuntimeSchema.getSchema(Baz.class);
    Pipe.Schema<Baz> pipeSchema = ((RuntimeSchema<Baz>) schema).getPipeSchema();

    Baz baz = SerializableObjects.baz;

    protobufRoundTrip(baz, schema, pipeSchema, false);
    protostuffRoundTrip(baz, schema, pipeSchema, false);

    // numeric
    protobufRoundTrip(baz, schema, pipeSchema, true);
    protostuffRoundTrip(baz, schema, pipeSchema, true);
}
 
Example 15
Source File: JsonXNumericRuntimeTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testBaz() throws Exception
{
    Schema<Baz> schema = RuntimeSchema.getSchema(Baz.class);

    for (Baz bazCompare : new Baz[] { baz, negativeBaz })
    {
        Baz dbaz = new Baz();

        byte[] data = JsonXIOUtil.toByteArray(bazCompare, schema, true, buf());
        JsonIOUtil.mergeFrom(data, dbaz, schema, true);
        SerializableObjects.assertEquals(bazCompare, dbaz);
    }
}
 
Example 16
Source File: QueryUtil.java    From datawave with Apache License 2.0 5 votes vote down vote up
public static <T extends Query> T deserialize(String queryImplClassName, Text columnVisibility, Value value) throws InvalidProtocolBufferException,
                ClassNotFoundException {
    @SuppressWarnings("unchecked")
    Class<T> queryClass = (Class<T>) Class.forName(queryImplClassName);
    byte[] b = value.get();
    Schema<T> schema = RuntimeSchema.getSchema(queryClass);
    T queryImpl = schema.newMessage();
    ProtobufIOUtil.mergeFrom(b, queryImpl, schema);
    queryImpl.setColumnVisibility(columnVisibility.toString());
    return queryImpl;
}
 
Example 17
Source File: SerializerUtil.java    From ServerCore with Apache License 2.0 4 votes vote down vote up
public static <T> byte[] encode(T object, Class<T> clazz) {
    Schema<T> schema = RuntimeSchema.getSchema(clazz);
    LinkedBuffer buffer = LinkedBuffer.allocate();
    return ProtostuffIOUtil.toByteArray(object, schema, buffer);
}
 
Example 18
Source File: BusProtoStuffMessageConverter.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private static EventType readClassHeader(final byte[] typeInformation) {
    final Schema<EventType> schema = RuntimeSchema.getSchema(EventType.class);
    final EventType deserializedType = schema.newMessage();
    ProtobufIOUtil.mergeFrom(typeInformation, deserializedType, schema);
    return deserializedType;
}
 
Example 19
Source File: DubboProtostuffSerialization.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
protected ObjectReader createReader(final InputStream is, final Class clazz) throws IOException {
    return new DubboProtostuffReader(RuntimeSchema.getSchema(clazz, STRATEGY), is);
}
 
Example 20
Source File: ConnectionSchema.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static <T> ConnectionSchema<T> getSchema(Class<T> typeClass) {
  final Schema<T> schema = RuntimeSchema.getSchema(typeClass);

  final Instantiator<T> instantiator = findInstantiator(typeClass, schema);
  return new ConnectionSchema<>(schema, instantiator);
}