@Test public void testCheckingDecode() { DecodeResult<Account> result = sumTypeCodec.fromJson(jsonOf(Account.Checking.class, BigDecimal.valueOf(0))); assertTrue(result.unsafeGet() instanceof Account.Checking); assertEquals(result.unsafeGet().amount, BigDecimal.ZERO); }
@Override public DecodeResult<T> fromJson(Json.JValue value) { Json.JObject object = value.asJsonObjectOrEmpty(); DecodeResult<? extends T> result = subtypes.entrySet().stream().filter(tuple -> discriminator.test(tuple.getKey(), object)).findFirst() .map(t -> t.getValue().fromJson(object)).orElseGet(() -> DecodeResult.fail("Unable to decode any types")); return result.map(it -> it); }
SumTypeCodec implements JsonCodec<T> { @Override public DecodeResult<T> fromJson(Json.JValue value) { Json.JObject object = value.asJsonObjectOrEmpty(); DecodeResult<? extends T> result = subtypes.entrySet().stream().filter(tuple -> discriminator.test(tuple.getKey(), object)).findFirst() .map(t -> t.getValue().fromJson(object)).orElseGet(() -> DecodeResult.fail("Unable to decode any types")); return result.map(it -> it); } }
SumTypeCodec implements JsonCodec<T> { @Override public DecodeResult<T> fromJson(Json.JValue value) { Json.JObject object = value.asJsonObjectOrEmpty(); DecodeResult<? extends T> result = subtypes.entrySet().stream().filter(tuple -> discriminator.test(tuple.getKey(), object)).findFirst() .map(t -> t.getValue().fromJson(object)).orElseGet(() -> DecodeResult.fail("Unable to decode any types")); return result.map(it -> it); } SumTypeCodec(Class<T> mainType, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> mainType, Function<Class<?>, String> nameF, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> mainType, String discriminatorField, Function<Class<?>, String> nameF, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> type, BiPredicate<Class<?>, Json.JObject> discriminator, Map<Class<?>, JsonCodec<? extends T>> subtypes); }
SumTypeCodec implements JsonCodec<T> { @Override public DecodeResult<T> fromJson(Json.JValue value) { Json.JObject object = value.asJsonObjectOrEmpty(); DecodeResult<? extends T> result = subtypes.entrySet().stream().filter(tuple -> discriminator.test(tuple.getKey(), object)).findFirst() .map(t -> t.getValue().fromJson(object)).orElseGet(() -> DecodeResult.fail("Unable to decode any types")); return result.map(it -> it); } SumTypeCodec(Class<T> mainType, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> mainType, Function<Class<?>, String> nameF, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> mainType, String discriminatorField, Function<Class<?>, String> nameF, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> type, BiPredicate<Class<?>, Json.JObject> discriminator, Map<Class<?>, JsonCodec<? extends T>> subtypes); @Override DecodeResult<T> fromJson(Json.JValue value); @Override Json.JValue toJson(T value); }
SumTypeCodec implements JsonCodec<T> { @Override public DecodeResult<T> fromJson(Json.JValue value) { Json.JObject object = value.asJsonObjectOrEmpty(); DecodeResult<? extends T> result = subtypes.entrySet().stream().filter(tuple -> discriminator.test(tuple.getKey(), object)).findFirst() .map(t -> t.getValue().fromJson(object)).orElseGet(() -> DecodeResult.fail("Unable to decode any types")); return result.map(it -> it); } SumTypeCodec(Class<T> mainType, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> mainType, Function<Class<?>, String> nameF, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> mainType, String discriminatorField, Function<Class<?>, String> nameF, Map<Class<?>, JsonCodec<? extends T>> subtypes);  SumTypeCodec(Class<T> type, BiPredicate<Class<?>, Json.JObject> discriminator, Map<Class<?>, JsonCodec<? extends T>> subtypes); @Override DecodeResult<T> fromJson(Json.JValue value); @Override Json.JValue toJson(T value);  }