@Test public void encodeStandard() { Json.JObject expected = jsonOf(Account.Standard.class, BigDecimal.valueOf(123)); Json.JValue json = sumTypeCodec.toJson(new Account.Standard(BigDecimal.valueOf(123L))); assertEquals("Unexpected json", expected, json); }
@Override public Json.JValue toJson(T value) { if (mainType.isInstance(value)) { JsonCodec<? extends T> codec = Optional.ofNullable(subtypes.get(value.getClass())). orElseThrow(() -> new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet()))); @SuppressWarnings("unchecked") JsonCodec<T> downcast = (JsonCodec<T>)codec; return downcast.toJson(value); } else { throw new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet())); } }
SumTypeCodec implements JsonCodec<T> { @Override public Json.JValue toJson(T value) { if (mainType.isInstance(value)) { JsonCodec<? extends T> codec = Optional.ofNullable(subtypes.get(value.getClass())). orElseThrow(() -> new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet()))); @SuppressWarnings("unchecked") JsonCodec<T> downcast = (JsonCodec<T>)codec; return downcast.toJson(value); } else { throw new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet())); } } }
SumTypeCodec implements JsonCodec<T> { @Override public Json.JValue toJson(T value) { if (mainType.isInstance(value)) { JsonCodec<? extends T> codec = Optional.ofNullable(subtypes.get(value.getClass())). orElseThrow(() -> new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet()))); @SuppressWarnings("unchecked") JsonCodec<T> downcast = (JsonCodec<T>)codec; return downcast.toJson(value); } else { throw new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet())); } } 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 Json.JValue toJson(T value) { if (mainType.isInstance(value)) { JsonCodec<? extends T> codec = Optional.ofNullable(subtypes.get(value.getClass())). orElseThrow(() -> new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet()))); @SuppressWarnings("unchecked") JsonCodec<T> downcast = (JsonCodec<T>)codec; return downcast.toJson(value); } else { throw new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet())); } } 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 Json.JValue toJson(T value) { if (mainType.isInstance(value)) { JsonCodec<? extends T> codec = Optional.ofNullable(subtypes.get(value.getClass())). orElseThrow(() -> new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet()))); @SuppressWarnings("unchecked") JsonCodec<T> downcast = (JsonCodec<T>)codec; return downcast.toJson(value); } else { throw new IllegalArgumentException(String.format("Not a valid type, %s, available %s", value.getClass(), subtypes.keySet())); } } 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);  }