/** * Registers a delegate. Returns true if registration is successful. */ public <T> boolean registerDelegate(Delegate<T> delegate) { return registerDelegate(delegate.typeClass().getName(), delegate); }
/** * Registers a delegate. Returns true if registration is successful. */ public <T> boolean registerDelegate(Delegate<T> delegate) { return registerDelegate(delegate.typeClass().getName(), delegate); }
protected static void initProtobufObjectCodec() { ((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new Delegate<Object>() { @Override public FieldType getFieldType() { return FieldType.BYTES; } @Override public Object readFrom(Input input) throws IOException { return JsonUtils.readValue(input.readByteArray(), Object.class); } @Override public void writeTo(Output output, int number, Object value, boolean repeated) throws IOException { output.writeByteArray(number, JsonUtils.writeValueAsBytes(value), false); } @Override public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) { throw new IllegalStateException("not support."); } @Override public Class<?> typeClass() { return Object.class; } }); }
public void testPojoWithImmutableListAsDelegate() throws Exception { ImmutableListAsDelegate delegate = null; if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy) { if (!((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY) .registerDelegate(ImmutableList.class.getName(), delegate = new ImmutableListAsDelegate(new CollectionSchemaForBaz()))) { // couldn't register delegate = null; } } 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); }
public void testPojoWithSingletonAsDelegate() throws Exception { if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy) { ((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY) .registerDelegate(SINGLETON_DELEGATE); } Schema<PojoWithSingletonAsDelegate> schema = RuntimeSchema .getSchema(PojoWithSingletonAsDelegate.class); Pipe.Schema<PojoWithSingletonAsDelegate> pipeSchema = ((RuntimeSchema<PojoWithSingletonAsDelegate>) schema) .getPipeSchema(); PojoWithSingletonAsDelegate p = new PojoWithSingletonAsDelegate() .fill(); byte[] data = toByteArray(p, schema); PojoWithSingletonAsDelegate pFromByteArray = new PojoWithSingletonAsDelegate(); mergeFrom(data, 0, data.length, pFromByteArray, schema); assertEquals(p, pFromByteArray); PojoWithSingletonAsDelegate pFromStream = new PojoWithSingletonAsDelegate(); ByteArrayInputStream in = new ByteArrayInputStream(data); mergeFrom(in, pFromStream, schema); assertEquals(p, pFromStream); roundTrip(p, schema, pipeSchema); }
.registerDelegate(delegate = new ShortArrayDelegate()))
public void testPojoWithHashMapInnerKeySetAsDelegate() throws Exception { HashMapInnerKeySetDelegate delegate = null; if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy) { if (!((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY) .registerDelegate("java.util.HashMap$KeySet", delegate = new HashMapInnerKeySetDelegate(new CollectionSchemaForString()))) { // couldn't register delegate = null; } } Schema<PojoWithHashMapInnerKeySetAsDelegate> schema = RuntimeSchema .getSchema(PojoWithHashMapInnerKeySetAsDelegate.class); Pipe.Schema<PojoWithHashMapInnerKeySetAsDelegate> pipeSchema = ((RuntimeSchema<PojoWithHashMapInnerKeySetAsDelegate>) schema) .getPipeSchema(); PojoWithHashMapInnerKeySetAsDelegate p = new PojoWithHashMapInnerKeySetAsDelegate().fill(); byte[] data = toByteArray(p, schema); PojoWithHashMapInnerKeySetAsDelegate pFromByteArray = new PojoWithHashMapInnerKeySetAsDelegate(); mergeFrom(data, 0, data.length, pFromByteArray, schema); assertEquals(p, pFromByteArray); PojoWithHashMapInnerKeySetAsDelegate pFromStream = new PojoWithHashMapInnerKeySetAsDelegate(); ByteArrayInputStream in = new ByteArrayInputStream(data); mergeFrom(in, pFromStream, schema); assertEquals(p, pFromStream); roundTrip(p, schema, pipeSchema); }
protected static void initProtobufObjectCodec() { ((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new Delegate<Object>() { @Override public FieldType getFieldType() { return FieldType.BYTES; } @Override public Object readFrom(Input input) throws IOException { return JsonUtils.readValue(input.readByteArray(), Object.class); } @Override public void writeTo(Output output, int number, Object value, boolean repeated) throws IOException { output.writeByteArray(number, JsonUtils.writeValueAsBytes(value), false); } @Override public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) { throw new IllegalStateException("not support."); } @Override public Class<?> typeClass() { return Object.class; } }); }