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

The following examples show how to use io.protostuff.runtime.RuntimeSchema#createFrom() . 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: PBTest.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) {
	Bean1 bean1 = new Bean1("haha1", new BigDecimal("1.00"));
	Bean2 bean2 = new Bean2("haha2", new BigDecimal("2.00"));

	LinkedBuffer buffer1 = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
	Schema schema1 = RuntimeSchema.createFrom(bean1.getClass());
	byte[] bytes1 = ProtostuffIOUtil.toByteArray(bean1, schema1, buffer1);

	Bean1 bean11 = new Bean1();
	ProtostuffIOUtil.mergeFrom(bytes1, bean11, schema1);
	System.out.println(bean11.toString());

	LinkedBuffer buffer2 = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
	Schema schema2 = RuntimeSchema.createFrom(bean2.getClass());
	byte[] bytes2 = ProtostuffIOUtil.toByteArray(bean2, schema2, buffer2);

	Bean2 bean22 = new Bean2();
	ProtostuffIOUtil.mergeFrom(bytes2, bean22, schema2);
	System.out.println(bean22.toString());

}
 
Example 2
Source File: ProtoBufSerializer.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T deSerialize(byte[] bytes, Class<T> clazz) {
    RuntimeSchema<T> runtimeSchema = RuntimeSchema.createFrom (clazz);
    T object = runtimeSchema.newMessage ();
    ProtobufIOUtil.mergeFrom (bytes, object, runtimeSchema);
    return object;
}
 
Example 3
Source File: ProtoStuffSerializer.java    From litchi with Apache License 2.0 5 votes vote down vote up
private static Schema<?> getSchema(Class<?> clazz) {
	Schema<?> schema = cachedSchema.get(clazz);
	if (schema == null) {
		schema = RuntimeSchema.createFrom(clazz);
		if (schema != null) {
			cachedSchema.put(clazz, schema);
		}
	}
	return schema;
}
 
Example 4
Source File: ProtobufUtils.java    From spring-boot-starter-grpc with MIT License 5 votes vote down vote up
/**
 * 根据获取相应类型的schema方法
 */
@SuppressWarnings({ "unchecked", "unused" })
private static  <T> RuntimeSchema<T> getSchema(Class<T> clazz) {
    RuntimeSchema<T> schema = (RuntimeSchema<T>) cachedSchema.get(clazz);
    if (schema == null) {
        schema = RuntimeSchema.createFrom(clazz);
        cachedSchema.put(clazz, schema);
    }
    return schema;
}
 
Example 5
Source File: ProtobufUtils.java    From spring-boot-starter-grpc with MIT License 5 votes vote down vote up
/**
 * 反序列化方法,将字节数组反序列化为对象(字节数组 ---> 对象)
 */
public static <T> T deserialize(byte[] data, Class<T> clazz) {
    RuntimeSchema<T> schema = RuntimeSchema.createFrom(clazz);
    T message = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(data, message, schema);
    return message;
}
 
Example 6
Source File: ProtostuffUtils.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Getting schema for serialized object types
 *
 * @param cls
 *            Class of serialized objects
 * @param <T>
 *            Types of serialized objects
 * @return Schema of serialized object type
 */
@SuppressWarnings({ "unchecked" })
private static <T> Schema<T> getSchema(Class<T> cls) {
	Schema<T> schema = (Schema<T>) schemaCache.get(cls);
	if (schema == null) {
		schema = RuntimeSchema.createFrom(cls);
		schemaCache.put(cls, schema);
	}
	return schema;
}
 
Example 7
Source File: ProtostuffUtils.java    From RxCache with Apache License 2.0 5 votes vote down vote up
/**
 * 获取序列化对象类型的schema
 *
 * @param cls 序列化对象的class
 * @param <T> 序列化对象的类型
 * @return 序列化对象类型的schema
 */
@SuppressWarnings({"unchecked", "rawtypes"})
private static <T> Schema<T> getSchema(Class<T> cls) {
    Schema<T> schema = (Schema<T>) CACHE_SCHEMA.get(cls);
    if (schema == null) {
        schema = RuntimeSchema.createFrom(cls, idStrategy);
        CACHE_SCHEMA.put(cls, schema);
    }
    return schema;
}
 
Example 8
Source File: ProtoStuffSerializer.java    From Thunder with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> Schema<T> getSchema(Class<T> clazz) {
    Schema<T> schema = (Schema<T>) SCHEMA_MAP.get(clazz);
    if (schema == null) {
        Schema<T> newSchema = RuntimeSchema.createFrom(clazz);
        schema = (Schema<T>) SCHEMA_MAP.putIfAbsent(clazz, newSchema);
        if (schema == null) {
            schema = newSchema;
        }
    }

    return schema;
}
 
Example 9
Source File: LevelDBManager.java    From nuls with MIT License 4 votes vote down vote up
private static void initSchema() {
    RuntimeSchema schema = RuntimeSchema.createFrom(ModelWrapper.class);
    SCHEMA_MAP.put(ModelWrapper.class, schema);
}
 
Example 10
Source File: CacheObjectSerializer.java    From nuls with MIT License 4 votes vote down vote up
public CacheObjectSerializer(Class clazz) {
//        System.out.println("init++++" + clazz);
        schema = RuntimeSchema.createFrom(clazz);
        this.clazz = clazz;
    }
 
Example 11
Source File: MyRuntimeSchema.java    From jseckill with Apache License 2.0 4 votes vote down vote up
private MyRuntimeSchema() {
    RuntimeSchema<Seckill> seckillSchemaVar = RuntimeSchema.createFrom(Seckill.class);
    setGoodsRuntimeSchema(seckillSchemaVar);
}
 
Example 12
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Setup
public void prepare() throws IOException
{
    int1RuntimeSchema = RuntimeSchema.createFrom(Int1.class);
    int10RuntimeSchema = RuntimeSchema.createFrom(Int10.class);
    sparseInt1RuntimeSchema = RuntimeSchema.createFrom(SparseInt1.class);
    sparseInt10RuntimeSchema = RuntimeSchema.createFrom(SparseInt10.class);
    generatedInt1Schema = GeneratedInt1.getSchema();
    generatedInt10Schema = GeneratedInt10.getSchema();
    int1 = new Int1();
    int1.a0 = 1;
    int10 = new Int10();
    int10.a0 = 1;
    int10.a1 = 2;
    int10.a2 = 3;
    int10.a3 = 4;
    int10.a4 = 5;
    int10.a5 = 6;
    int10.a6 = 7;
    int10.a7 = 8;
    int10.a8 = 9;
    int10.a9 = 10;
    sparseInt1 = new SparseInt1();
    sparseInt1.a0 = 1;
    sparseInt10 = new SparseInt10();
    sparseInt10.a0 = 1;
    sparseInt10.a1 = 2;
    sparseInt10.a2 = 3;
    sparseInt10.a3 = 4;
    sparseInt10.a4 = 5;
    sparseInt10.a5 = 6;
    sparseInt10.a6 = 7;
    sparseInt10.a7 = 8;
    sparseInt10.a8 = 9;
    sparseInt10.a9 = 10;
    generatedInt1 = new GeneratedInt1();
    generatedInt1.setA0(1);
    generatedInt10 = new GeneratedInt10();
    generatedInt10.setA0(1);
    generatedInt10.setA1(2);
    generatedInt10.setA2(3);
    generatedInt10.setA3(4);
    generatedInt10.setA4(5);
    generatedInt10.setA5(6);
    generatedInt10.setA6(7);
    generatedInt10.setA7(8);
    generatedInt10.setA8(9);
    generatedInt10.setA9(10);
    buffer = LinkedBuffer.allocate();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ProtobufIOUtil.writeTo(outputStream, int1, int1RuntimeSchema, buffer);
    data_1_int = outputStream.toByteArray();
    outputStream.reset();
    buffer.clear();

    ProtobufIOUtil.writeTo(outputStream, int10, int10RuntimeSchema, buffer);
    data_10_int = outputStream.toByteArray();
    outputStream.reset();
    buffer.clear();
}