@Test public void serialize() throws Exception { ZonedDateTime now = ZonedDateTime.now(); Binder res = DefaultBiMapper.serialize( Binder.of( "time", now, "hello", "world" ) ); assertEquals("world", res.get("hello")); assertEquals("unixtime", res.getStringOrThrow("time", "__type")); Binder restored = DefaultBiMapper.deserialize(res); assertEquals(now.truncatedTo(ChronoUnit.SECONDS), restored.get("time")); assertEquals(now.truncatedTo(ChronoUnit.SECONDS), DefaultBiMapper.deserialize(DefaultBiMapper.serialize(now))); }
public @NonNull <T> T serialize(Object x, BiSerializer serializer) { if (x instanceof String || x instanceof Number || x instanceof Boolean || x == null) return (T) x; Class<?> klass = x.getClass(); if (klass.isArray() && !(klass.getComponentType() == byte.class)) { x = Arrays.asList((Object[]) x); } if (x instanceof Collection) { return (T) ((Collection) x).stream() .map(i -> serialize(i, serializer)) .collect(Collectors.toList()); } String canonicalName = klass.getCanonicalName(); BiAdapter adapter = adapters.get(canonicalName); if (adapter == null) { if (x instanceof Map) { Binder serialized = new Binder(); ((Map)x).forEach((k,v) -> serialized.put(serialize(k),serialize(v))); return (T)serialized; } return (T) x; } Binder result = adapter.serialize(x, serializer); String tn = adapter.typeName(); result.put("__type", tn != null ? tn : canonicalName); return (T) result; }
BiMapper { public @NonNull <T> T serialize(Object x, BiSerializer serializer) { if (x instanceof String || x instanceof Number || x instanceof Boolean || x == null) return (T) x; Class<?> klass = x.getClass(); if (klass.isArray() && !(klass.getComponentType() == byte.class)) { x = Arrays.asList((Object[]) x); } if (x instanceof Collection) { return (T) ((Collection) x).stream() .map(i -> serialize(i, serializer)) .collect(Collectors.toList()); } String canonicalName = klass.getCanonicalName(); BiAdapter adapter = adapters.get(canonicalName); if (adapter == null) { if (x instanceof Map) { Binder serialized = new Binder(); ((Map)x).forEach((k,v) -> serialized.put(serialize(k),serialize(v))); return (T)serialized; } return (T) x; } Binder result = adapter.serialize(x, serializer); String tn = adapter.typeName(); result.put("__type", tn != null ? tn : canonicalName); return (T) result; } }
BiMapper { public @NonNull <T> T serialize(Object x, BiSerializer serializer) { if (x instanceof String || x instanceof Number || x instanceof Boolean || x == null) return (T) x; Class<?> klass = x.getClass(); if (klass.isArray() && !(klass.getComponentType() == byte.class)) { x = Arrays.asList((Object[]) x); } if (x instanceof Collection) { return (T) ((Collection) x).stream() .map(i -> serialize(i, serializer)) .collect(Collectors.toList()); } String canonicalName = klass.getCanonicalName(); BiAdapter adapter = adapters.get(canonicalName); if (adapter == null) { if (x instanceof Map) { Binder serialized = new Binder(); ((Map)x).forEach((k,v) -> serialized.put(serialize(k),serialize(v))); return (T)serialized; } return (T) x; } Binder result = adapter.serialize(x, serializer); String tn = adapter.typeName(); result.put("__type", tn != null ? tn : canonicalName); return (T) result; } BiMapper();  BiMapper(BiMapper parent); }
BiMapper { public @NonNull <T> T serialize(Object x, BiSerializer serializer) { if (x instanceof String || x instanceof Number || x instanceof Boolean || x == null) return (T) x; Class<?> klass = x.getClass(); if (klass.isArray() && !(klass.getComponentType() == byte.class)) { x = Arrays.asList((Object[]) x); } if (x instanceof Collection) { return (T) ((Collection) x).stream() .map(i -> serialize(i, serializer)) .collect(Collectors.toList()); } String canonicalName = klass.getCanonicalName(); BiAdapter adapter = adapters.get(canonicalName); if (adapter == null) { if (x instanceof Map) { Binder serialized = new Binder(); ((Map)x).forEach((k,v) -> serialized.put(serialize(k),serialize(v))); return (T)serialized; } return (T) x; } Binder result = adapter.serialize(x, serializer); String tn = adapter.typeName(); result.put("__type", tn != null ? tn : canonicalName); return (T) result; } BiMapper();  BiMapper(BiMapper parent); void deserializeInPlace(Map map, BiDeserializer deserializer); T deserialize(Map map, BiDeserializer deserializer); T deserialize(Map map); T deserializeObject(Object x); T deserializeObject(Object obj, BiDeserializer deserializer); @NonNull T serialize(Object x, BiSerializer serializer); @NonNull T serialize(Object x); void registerAdapter(Class<T> klass, BiAdapter adapter); boolean unregister(Class klass); void registerClass(Class<? extends BiSerializable> klass); BiSerializer newSerializer(); BiDeserializer newDeserializer(); }
BiMapper { public @NonNull <T> T serialize(Object x, BiSerializer serializer) { if (x instanceof String || x instanceof Number || x instanceof Boolean || x == null) return (T) x; Class<?> klass = x.getClass(); if (klass.isArray() && !(klass.getComponentType() == byte.class)) { x = Arrays.asList((Object[]) x); } if (x instanceof Collection) { return (T) ((Collection) x).stream() .map(i -> serialize(i, serializer)) .collect(Collectors.toList()); } String canonicalName = klass.getCanonicalName(); BiAdapter adapter = adapters.get(canonicalName); if (adapter == null) { if (x instanceof Map) { Binder serialized = new Binder(); ((Map)x).forEach((k,v) -> serialized.put(serialize(k),serialize(v))); return (T)serialized; } return (T) x; } Binder result = adapter.serialize(x, serializer); String tn = adapter.typeName(); result.put("__type", tn != null ? tn : canonicalName); return (T) result; } BiMapper();  BiMapper(BiMapper parent); void deserializeInPlace(Map map, BiDeserializer deserializer); T deserialize(Map map, BiDeserializer deserializer); T deserialize(Map map); T deserializeObject(Object x); T deserializeObject(Object obj, BiDeserializer deserializer); @NonNull T serialize(Object x, BiSerializer serializer); @NonNull T serialize(Object x); void registerAdapter(Class<T> klass, BiAdapter adapter); boolean unregister(Class klass); void registerClass(Class<? extends BiSerializable> klass); BiSerializer newSerializer(); BiDeserializer newDeserializer();  }