@Override public byte[] apply(T t) { return SerDeUtils.toBytes(t); } }
/** * Serializes the given Object into bytes. * */ @Override public byte[] apply(Object o) { return toBytes(o); } }
@Override public void write(Kryo kryo, Output output) { int numVariableMappings = variableMappings.isEmpty()?0:variableMappings.size(); output.writeShort(numVariableMappings); for(Map m : variableMappings) { byte[] b = m == null?new byte[]{}:SerDeUtils.toBytes(m); output.writeInt(b.length); if(b.length > 0) { output.writeBytes(b); } } }
/** * Ensure that a value can be serialized and deserialized using Kryo. * * <p>When a Stellar function is used in a Storm topology there are cases when the result * needs to be serializable, like when using the Profiler. Storm can use either Kryo or * basic Java serialization. It is highly recommended that all Stellar functions return a * result that is Kryo serializable to allow for the broadest possible use of the function. * * @param value The value to validate. */ private static void ensureKryoSerializable(Object value, String expression) { String msg = String.format("Expression result is not Kryo serializable. It is highly recommended for all " + "functions to return a result that is Kryo serializable to allow for their broadest possible use. " + "expr=%s, value=%s", expression, value); byte[] raw = SerDeUtils.toBytes(value); Object actual = SerDeUtils.fromBytes(raw, Object.class); Assert.assertEquals(msg, value, actual); }
data = (byte[])o; } else { data = SerDeUtils.toBytes(o);
@Test public void testMap() { final Map<String, Object> expected = new HashMap<>(); expected.put("foo", "bar"); expected.put( "bar", 1.0); ; byte[] raw = SerDeUtils.toBytes(expected); Object actual = SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual); }
@Test public void testList() { final List<String> expected = new ArrayList<String>(); expected.add("foo"); expected.add("bar"); byte[] raw = SerDeUtils.toBytes(expected); Object actual = SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual); }
private void assertKryoserializable(ConcatMap c) { byte[] serialized = SerDeUtils.toBytes(c); ConcatMap deserialized = SerDeUtils.fromBytes(serialized, ConcatMap.class); Assert.assertEquals(deserialized, c); }
@Test public void testInteger() { final int expected = 2; byte[] raw = SerDeUtils.toBytes(expected); int actual = SerDeUtils.fromBytes(raw, Integer.class); assertEquals(expected, actual); }
@Test public void testArbitraryPojo() { final ArbitraryPojo expected = new ArbitraryPojo(); byte[] raw = SerDeUtils.toBytes(expected); Object actual = SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual); } }
@Test public void testFloat() { final Float expected = 2.2F; byte[] raw = SerDeUtils.toBytes(expected); { float actual = SerDeUtils.fromBytes(raw, Float.class); assertEquals(expected, actual, 0.01); } { float actual = (float) SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual, 0.01); } }
@Test public void testShort() { final short expected = 2; byte[] raw = SerDeUtils.toBytes(expected); { short actual = SerDeUtils.fromBytes(raw, Short.class); assertEquals(expected, actual); } { short actual = (short) SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual); } }
@Test public void testLong() { final long expected = 2L; byte[] raw = SerDeUtils.toBytes(expected); { long actual = SerDeUtils.fromBytes(raw, Long.class); assertEquals(expected, actual); } { long actual = (Long) SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual); } }
@Test public void testDouble() { final double expected = 2.0; byte[] raw = SerDeUtils.toBytes(expected); { double actual = SerDeUtils.fromBytes(raw, Double.class); assertEquals(expected, actual, 0.01); } { double actual = (double) SerDeUtils.fromBytes(raw, Object.class); assertEquals(expected, actual, 0.01); } }
@Test @SuppressWarnings("unchecked") public void testBloomFilter() { final BloomFilter<Object> expected = new BloomFilter<>(new BloomFilter.DefaultSerializer<>(), 10000, 0.01); expected.add("foo"); expected.add("bar"); byte[] raw = SerDeUtils.toBytes(expected); BloomFilter<Object> actual = (BloomFilter) SerDeUtils.fromBytes(raw, Object.class); Assert.assertTrue(actual.mightContain("foo")); Assert.assertFalse(actual.mightContain("timothy")); assertEquals(expected, actual); }