io.protostuff.runtime.RuntimeSchema Java Examples
The following examples show how to use
io.protostuff.runtime.RuntimeSchema.
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: ProtoStuffSerializer.java From Jupiter with Apache License 2.0 | 6 votes |
@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 #2
Source File: SerializationUtil.java From framework with Apache License 2.0 | 6 votes |
/** * 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 #3
Source File: ProtoStuffSerializer.java From sofa-jraft with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public <T> byte[] writeObject(final T obj) { final Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass()); final LinkedBuffer buf = LinkedBuffers.getLinkedBuffer(); final Output output = Outputs.getOutput(buf); try { schema.writeTo(output, obj); return Outputs.toByteArray(output); } catch (final IOException e) { ThrowUtil.throwException(e); } finally { LinkedBuffers.resetBuf(buf); // for reuse } return null; // never get here }
Example #4
Source File: SerializationUtil.java From framework with Apache License 2.0 | 6 votes |
/** * Description: <br> * * @author 王伟<br> * @taskId <br> * @param obj * @param <T> T * @return T * @throws UtilException <br> */ @SuppressWarnings("unchecked") public static <T> byte[] serial(final T obj) throws UtilException { if (obj != null && !(obj instanceof Void)) { try { if (obj instanceof Map) { return jdkSerial(obj); } else { Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(INIT_SIZE); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } } catch (Exception e) { throw new UtilException(e, ErrorCodeDef.SERIALIZE_ERROR, obj); } } return null; }
Example #5
Source File: ProtoStuffSerializeUtil.java From ns4_frame with Apache License 2.0 | 6 votes |
@Deprecated public static <T> byte[] serialize(List<T> lst,Class<T> cl) throws IOException { if (lst == null) { return new byte[0]; } Schema<T> schema = RuntimeSchema.getSchema(cl); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); LinkedBuffer linkedBuffer = LinkedBuffer.allocate(1024); ProtostuffIOUtil.writeListTo(arrayOutputStream, lst, schema, linkedBuffer); byte[] bs = arrayOutputStream.toByteArray(); arrayOutputStream.close(); return bs; }
Example #6
Source File: ObjectEncodingHandlerProtobufImpl.java From alibaba-rsocket-broker with Apache License 2.0 | 6 votes |
@Override @Nullable public Object decodeResult(ByteBuf data, @Nullable Class<?> targetClass) throws EncodingException { if (data.readableBytes() > 0 && targetClass != null) { try { if (GeneratedMessageV3.class.isAssignableFrom(targetClass)) { Method method = parseFromMethodStore.get(targetClass); if (method != null) { return method.invoke(null, data.nioBuffer()); } } else if (ktProtoBuf && KotlinSerializerSupport.isKotlinSerializable(targetClass)) { byte[] bytes = new byte[data.readableBytes()]; data.readBytes(bytes); return KotlinSerializerSupport.decodeFromProtobuf(bytes, targetClass); } else { Schema schema = RuntimeSchema.getSchema(targetClass); Object object = schema.newMessage(); ProtostuffIOUtil.mergeFrom(new ByteBufInputStream(data), object, schema); return object; } } catch (Exception e) { throw new EncodingException(RsocketErrorCode.message("RST-700501", "bytebuf", targetClass.getName()), e); } } return null; }
Example #7
Source File: ProtoStuffSerializer.java From sofa-jraft with Apache License 2.0 | 6 votes |
@Override public <T> T readObject(final InputBuf inputBuf, final Class<T> clazz) { final Schema<T> schema = RuntimeSchema.getSchema(clazz); final T msg = schema.newMessage(); final Input input = Inputs.getInput(inputBuf); try { schema.mergeFrom(input, msg); Inputs.checkLastTagWas(input, 0); } catch (final IOException e) { ThrowUtil.throwException(e); } finally { inputBuf.release(); } return msg; }
Example #8
Source File: PurchaseInfoSerializer.java From ChengFeng1.5 with MIT License | 6 votes |
@Override public byte[] serialize(String topic, PurchaseInfoDto data) { if(data==null) { return null; } Schema schema = RuntimeSchema.getSchema(data.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); byte[] protostuff=null; try { protostuff= ProtostuffIOUtil.toByteArray(data, schema, buffer); }catch (Exception e) { log.error(e.getMessage()); }finally { buffer.clear(); } return protostuff; }
Example #9
Source File: ProtoStuffSerializer.java From Jupiter with Apache License 2.0 | 6 votes |
@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 #10
Source File: JsonXNumericRuntimeTest.java From protostuff with Apache License 2.0 | 6 votes |
public void testBarStreamed() throws Exception { Schema<Bar> schema = RuntimeSchema.getSchema(Bar.class); for (Bar barCompare : new Bar[] { bar, negativeBar }) { Bar dbar = new Bar(); ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer buffer = buf(); try { JsonXIOUtil.writeTo(out, barCompare, schema, true, buffer); } finally { buffer.clear(); } byte[] data = out.toByteArray(); JsonIOUtil.mergeFrom(data, dbar, schema, true); SerializableObjects.assertEquals(barCompare, dbar); } }
Example #11
Source File: JsonXNumericRuntimeTest.java From protostuff with Apache License 2.0 | 6 votes |
public void testFooStreamed() throws Exception { Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class); Foo fooCompare = foo; Foo dfoo = new Foo(); ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer buffer = buf(); try { JsonXIOUtil.writeTo(out, fooCompare, schema, true, buffer); } finally { buffer.clear(); } byte[] data = out.toByteArray(); JsonIOUtil.mergeFrom(data, dfoo, schema, true); SerializableObjects.assertEquals(fooCompare, dfoo); }
Example #12
Source File: ProtostuffRowSerializationSchema.java From alchemy with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(Row row) { if(this.schema == null){ this.schema = RuntimeSchema.getSchema(clazz); } LinkedBuffer buf = THREAD_LOCAL.get(); try { Object object = schema.newMessage(); ConvertRowUtil.convertFromRow(object, ((RowTypeInfo)typeInfo).getFieldNames(), row); return ProtostuffIOUtil.toByteArray(object, schema, buf); } catch (Throwable t) { throw new RuntimeException( "Could not serialize row '" + row + "'. " + "Make sure that the schema matches the input.", t); } finally { buf.clear(); } }
Example #13
Source File: JsonXNumericRuntimeTest.java From protostuff with Apache License 2.0 | 6 votes |
public void testPolymorphicStreamed() throws Exception { Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class); Zoo zooCompare = PolymorphicSerializationTest.filledZoo(); Zoo dzoo = new Zoo(); ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer buffer = buf(); try { JsonXIOUtil.writeTo(out, zooCompare, schema, true, buffer); } finally { buffer.clear(); } byte[] data = out.toByteArray(); JsonIOUtil.mergeFrom(data, dzoo, schema, true); SerializableObjects.assertEquals(zooCompare, dzoo); }
Example #14
Source File: LevelDBManager.java From nuls with MIT License | 6 votes |
public static <T> T getModel(byte[] value, Class<T> clazz) { if (value == null) { return null; } try { RuntimeSchema schema = SCHEMA_MAP.get(ModelWrapper.class); ModelWrapper model = new ModelWrapper(); ProtostuffIOUtil.mergeFrom(value, model, schema); if (clazz != null && model.getT() != null) { return clazz.cast(model.getT()); } return (T) model.getT(); } catch (Exception e) { e.printStackTrace(); return null; } }
Example #15
Source File: LevelDBManager.java From nuls with MIT License | 6 votes |
public static <T> T getModel(String area, byte[] key, Class<T> clazz) { if (!baseCheckArea(area)) { return null; } if (key == null) { return null; } try { DB db = AREAS.get(area); byte[] bytes = db.get(key); if (bytes == null) { return null; } RuntimeSchema schema = SCHEMA_MAP.get(ModelWrapper.class); ModelWrapper model = new ModelWrapper(); ProtostuffIOUtil.mergeFrom(bytes, model, schema); if (clazz != null && model.getT() != null) { return clazz.cast(model.getT()); } return (T) model.getT(); } catch (Exception e) { e.printStackTrace(); return null; } }
Example #16
Source File: SchemaCache.java From voyage with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public static <T> Schema<T> getSchema(Class<T> clazz) { String className = clazz.getName(); Schema<T> schema = (Schema<T>) SCHEMA_CACHE.get(className); if (null != schema) { return schema; } synchronized (SCHEMA_CACHE) { if (null == SCHEMA_CACHE.get(className)) { schema = RuntimeSchema.getSchema(clazz); SCHEMA_CACHE.put(className, schema); return schema; } else { return (Schema<T>) SCHEMA_CACHE.get(className); } } }
Example #17
Source File: ProtostuffSerializer.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public AbstractByteBuf encode(Object object, Map<String, String> context) throws SofaRpcException { if (object == null) { throw buildSerializeError("Unsupported null message!"); } else if (object instanceof SofaRequest) { return encodeSofaRequest((SofaRequest) object, context); } else if (object instanceof SofaResponse) { return encodeSofaResponse((SofaResponse) object, context); } else { Class clazz = object.getClass(); Schema schema = RuntimeSchema.getSchema(clazz); // Re-use (manage) this buffer to avoid allocating on every serialization LinkedBuffer buffer = LinkedBuffer.allocate(512); // ser try { return new ByteArrayWrapperByteBuf(ProtostuffIOUtil.toByteArray(object, schema, buffer)); } finally { buffer.clear(); } } }
Example #18
Source File: JsonXNumericRuntimeTest.java From protostuff with Apache License 2.0 | 6 votes |
public void testBazStreamed() throws Exception { Schema<Baz> schema = RuntimeSchema.getSchema(Baz.class); for (Baz bazCompare : new Baz[] { baz, negativeBaz }) { Baz dbaz = new Baz(); ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer buffer = buf(); try { JsonXIOUtil.writeTo(out, bazCompare, schema, true, buffer); } finally { buffer.clear(); } byte[] data = out.toByteArray(); JsonIOUtil.mergeFrom(data, dbaz, schema, true); SerializableObjects.assertEquals(bazCompare, dbaz); } }
Example #19
Source File: ProtostuffSerializer.java From BigData-In-Practice with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(String topic, Company data) { if (data == null) { return null; } Schema schema = RuntimeSchema.getSchema(data.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); byte[] protostuff = null; try { protostuff = ProtobufIOUtil.toByteArray(data, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } return protostuff; }
Example #20
Source File: JsonNumericRuntimeSerDeserTest.java From protostuff with Apache License 2.0 | 5 votes |
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 = JsonIOUtil.toByteArray(barCompare, schema, true); JsonIOUtil.mergeFrom(data, dbar, schema, true); SerializableObjects.assertEquals(barCompare, dbar); } }
Example #21
Source File: ProtoStuffSerialize.java From Voovan with Apache License 2.0 | 5 votes |
public Schema getSchema(Class clazz) { Schema schema = SCHEMAS.get(clazz); if(schema == null) { schema = RuntimeSchema.getSchema(clazz); } return schema; }
Example #22
Source File: ProtostuffUtil.java From blade-tool with GNU Lesser General Public License v3.0 | 5 votes |
/** * 获取Schema * @param clazz clazz * @param <T> T * @return T */ @SuppressWarnings("unchecked") private static <T> Schema<T> getSchema(Class<T> clazz) { Schema<T> schema = (Schema<T>) schemaCache.get(clazz); if (Objects.isNull(schema)) { //这个schema通过RuntimeSchema进行懒创建并缓存 //所以可以一直调用RuntimeSchema.getSchema(),这个方法是线程安全的 schema = RuntimeSchema.getSchema(clazz); if (Objects.nonNull(schema)) { schemaCache.put(clazz, schema); } } return schema; }
Example #23
Source File: ProtostuffUtils.java From RxCache with Apache License 2.0 | 5 votes |
/** * 获取序列化对象类型的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 #24
Source File: ProtostuffSerializer.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public Object decode(AbstractByteBuf data, Class clazz, Map<String, String> context) throws SofaRpcException { if (clazz == null) { throw buildDeserializeError("class is null!"); } else { Schema schema = RuntimeSchema.getSchema(clazz); Object fooParsed = schema.newMessage(); ProtostuffIOUtil.mergeFrom(data.array(), fooParsed, schema); return fooParsed; } }
Example #25
Source File: JsonXNumericRuntimeTest.java From protostuff with Apache License 2.0 | 5 votes |
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 #26
Source File: PurchaseInfoDeSerializer.java From ChengFeng1.5 with MIT License | 5 votes |
@Override public PurchaseInfoDto deserialize(String topic, byte[] data) { if(data==null) { return null; } Schema<PurchaseInfoDto> schema = RuntimeSchema.getSchema(PurchaseInfoDto.class); PurchaseInfoDto purchaseInfoDto=new PurchaseInfoDto(); ProtostuffIOUtil.mergeFrom(data, purchaseInfoDto, schema); return purchaseInfoDto; }
Example #27
Source File: ProtostuffDeserializer.java From BigData-In-Practice with Apache License 2.0 | 5 votes |
public Company deserialize(String topic, byte[] data) { if (data == null) { return null; } Schema schema = RuntimeSchema.getSchema(Company.class); Company ans = new Company(); ProtostuffIOUtil.mergeFrom(data, ans, schema); return ans; }
Example #28
Source File: XmlRuntimeSerDeserTest.java From protostuff with Apache License 2.0 | 5 votes |
public void testFoo() throws Exception { Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class); Foo fooCompare = foo; Foo dfoo = new Foo(); byte[] data = XmlIOUtil.toByteArray(fooCompare, schema); XmlIOUtil.mergeFrom(data, dfoo, schema); SerializableObjects.assertEquals(fooCompare, dfoo); }
Example #29
Source File: XmlRuntimeSerDeserTest.java From protostuff with Apache License 2.0 | 5 votes |
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 = XmlIOUtil.toByteArray(barCompare, schema); XmlIOUtil.mergeFrom(data, dbar, schema); SerializableObjects.assertEquals(barCompare, dbar); } }
Example #30
Source File: ProtoStuffSerializeUtil.java From jim-framework with Apache License 2.0 | 5 votes |
public static <T> List<T> deserializeList(byte[] bytes, Class<T> targetClass) { if (bytes == null || bytes.length == 0) { return null; } Schema<T> schema = RuntimeSchema.getSchema(targetClass); try { return ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(bytes), schema); } catch (IOException e) { logger.info("ProtoStuffSerialize.deserializeList error:",e); } return null; }