public static <T> T deserialize(byte[] serialized, Class<T> clazz) { return serializationDelegate.deserialize(serialized, clazz); }
/** * Creates an instance of the pluggable SerializationDelegate or falls back to DefaultSerializationDelegate if something goes wrong. * * @param topoConf The config from which to pull the name of the pluggable class. * @return an instance of the class specified by storm.meta.serialization.delegate */ private static SerializationDelegate getSerializationDelegate(Map<String, Object> topoConf) { String delegateClassName = (String) topoConf.get(Config.STORM_META_SERIALIZATION_DELEGATE); SerializationDelegate delegate; try { Class delegateClass = Class.forName(delegateClassName); delegate = (SerializationDelegate) delegateClass.newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new RuntimeException("Failed to construct serialization delegate class " + delegateClassName, e); } delegate.prepare(topoConf); return delegate; }
public static byte[] serialize(Object obj) { return serializationDelegate.serialize(obj); }
@Test public void testThriftInstance() throws Exception { ErrorInfo errorInfo = new ErrorInfo(); errorInfo.set_error("error"); errorInfo.set_error_time_secs(1); errorInfo.set_host("host"); errorInfo.set_port(1); byte[] serialized = new ThriftSerializationDelegate().serialize(errorInfo); ErrorInfo errorInfo2 = testDelegate.deserialize(serialized, ErrorInfo.class); assertEquals(errorInfo, errorInfo2); serialized = testDelegate.serialize(errorInfo); errorInfo2 = new ThriftSerializationDelegate().deserialize(serialized, ErrorInfo.class); assertEquals(errorInfo, errorInfo2); }
@Test public void testDeserialize_readingFromGzip() throws Exception { GlobalStreamId id = new GlobalStreamId("first", "second"); byte[] serialized = new GzipThriftSerializationDelegate().serialize(id); GlobalStreamId id2 = testDelegate.deserialize(serialized, GlobalStreamId.class); assertEquals(id2.get_componentId(), id.get_componentId()); assertEquals(id2.get_streamId(), id.get_streamId()); }
@Before public void setUp() throws Exception { testDelegate = new ThriftSerializationDelegate(); testDelegate.prepare(null); }
/** * Serialize an object using the configured serialization and then base64 encode it into a string. * * @param obj the object to encode * @return a string with the encoded object in it. */ public static String serializeToString(Object obj) { return Base64.getEncoder().encodeToString(serializationDelegate.serialize(obj)); }
@Test public void testDeserialize_readingFromGzipBridge() throws Exception { GlobalStreamId id = new GlobalStreamId("first", "second"); byte[] serialized = new GzipBridgeThriftSerializationDelegate().serialize(id); GlobalStreamId id2 = testDelegate.deserialize(serialized, GlobalStreamId.class); assertEquals(id2.get_componentId(), id.get_componentId()); assertEquals(id2.get_streamId(), id.get_streamId()); }
/** * Creates an instance of the pluggable SerializationDelegate or falls back to * DefaultSerializationDelegate if something goes wrong. * @param stormConf The config from which to pull the name of the pluggable class. * @return an instance of the class specified by storm.meta.serialization.delegate */ private static SerializationDelegate getSerializationDelegate(Map stormConf) { String delegateClassName = (String)stormConf.get(Config.STORM_META_SERIALIZATION_DELEGATE); SerializationDelegate delegate; try { Class delegateClass = Class.forName(delegateClassName); delegate = (SerializationDelegate) delegateClass.newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { LOG.error("Failed to construct serialization delegate, falling back to default", e); delegate = new DefaultSerializationDelegate(); } delegate.prepare(stormConf); return delegate; }
public static byte[] serialize(Object obj) { return serializationDelegate.serialize(obj); }
@Test public void testDeserialize_readingFromDefault() throws Exception { GlobalStreamId id = new GlobalStreamId("A", "B"); byte[] serialized = new ThriftSerializationDelegate().serialize(id); GlobalStreamId id2 = testDelegate.deserialize(serialized, GlobalStreamId.class); assertEquals(id2.get_componentId(), id.get_componentId()); assertEquals(id2.get_streamId(), id.get_streamId()); } }
public static <T> T deserialize(byte[] serialized, Class<T> clazz) { return serializationDelegate.deserialize(serialized, clazz); }