/** * Deserializes the given bytes. * * @param bytes the function argument * @return the function result */ @Override public T apply(byte[] bytes) { return fromBytes(bytes, clazz); } }
@Override public void write(byte[] obj, Optional<String> output, Configuration hadoopConfig) throws IOException { System.out.println(SerDeUtils.fromBytes(obj, Object.class)); } }
@Override public Object load(String s) throws Exception { if(StringUtils.isEmpty(s)) { return null; } Path p = new Path(s); if(fs.exists(p)) { try(InputStream is = new BufferedInputStream(fs.open(p))) { byte[] serialized = IOUtils.toByteArray(is); if(serialized.length > 0) { Object ret = SerDeUtils.fromBytes(serialized, Object.class); return ret; } } } return null; } }
@Override public SyncPolicy create(String sensor, WriterConfiguration config) { try { //we do a deep clone of the SyncPolicy via kryo serialization. This gives us a fresh policy //to work with. The reason we need a fresh policy is that we want to ensure each handler //(one handler per task & sensor type and one handler per file) has its own sync policy. // Reusing a sync policy is a bad idea, so we need to clone it here. Unfortunately the // SyncPolicy object does not implement Cloneable, so we'll need to clone it via serialization //to get a fresh policy object. Note: this would be expensive if it was in the critical path, // but should be called infrequently (once per sync). byte[] serializedForm = SerDeUtils.toBytes(syncPolicy); return SerDeUtils.fromBytes(serializedForm, SyncPolicy.class); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } } }
measurement.withProfileValue(SerDeUtils.fromBytes(value, clazz)); values.add(measurement);
@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); }
@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 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); } }
public ProfileMeasurement toProfileMeasurement() { ProfilePeriod period = ProfilePeriod.fromPeriodId(periodId, durationMillis, TimeUnit.MILLISECONDS); ProfileMeasurement measurement = new ProfileMeasurement() .withProfileName(profileName) .withEntity(entity) .withPeriod(period) .withProfileValue(SerDeUtils.fromBytes(profileValue, Object.class)); return measurement; }
@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 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); }