public GensonBuilder withDeserializers(Deserializer<?>... deserializer) { for (Deserializer<?> d : deserializer) { Type typeOfConverter = TypeUtil.typeOf(0, TypeUtil.lookupGenericType(Deserializer.class, d.getClass())); typeOfConverter = TypeUtil.expandType(typeOfConverter, d.getClass()); registerDeserializer(d, typeOfConverter); } return this; }
private void addDefaultDeserializers(List<? extends Deserializer<?>> deserializers) { if (deserializers != null) { for (Deserializer<?> deserializer : deserializers) { Type typeOfConverter = TypeUtil.typeOf( 0, TypeUtil.lookupGenericType(Deserializer.class, deserializer.getClass())); typeOfConverter = TypeUtil.expandType(typeOfConverter, deserializer.getClass()); if (!deserializersMap.containsKey(typeOfConverter)) deserializersMap.put(typeOfConverter, deserializer); } } }
private <T> void registerDeserializer(Deserializer<T> deserializer, Type type) { if (deserializersMap.containsKey(type)) throw new IllegalStateException( "Can not register deserializer " + deserializer.getClass() + ". A custom deserializer is already registered for type " + type); deserializersMap.put(type, deserializer); }
/** * Deserializes a transaction. * * @param options The deserialization options. * @param deserializer The deserializer. * @return The deserialized transaction. */ private static Transaction deserialize( final VerifiableEntity.DeserializationOptions options, final Deserializer deserializer) { final int type = deserializer.readInt("type"); final BiFunction<VerifiableEntity.DeserializationOptions, Deserializer, Transaction> constructor = TYPE_TO_CONSTRUCTOR_MAP.getOrDefault(type, null); if (null == constructor) { throw new IllegalArgumentException("Unknown transaction type: " + type); } return constructor.apply(options, deserializer); }
public static <T> T parse(Object object, Type type) { Deserializer deserializer = getDeserializer(type); return deserializer.deserialize(object, type); }