@Override public <T> BeanDescriptor<T> provide(Class<T> ofClass, Type type, Genson genson) { BeanDescriptor<T> desc = (BeanDescriptor<T>) cache.get(type); if (desc == null) { for (BeanDescriptorProvider provider : providers) { desc = provider.provide(ofClass, type, genson); if (desc != null) break; } cache.putIfAbsent(type, desc); } return desc; } }
@Override public <T> BeanDescriptor<T> provide(Class<T> ofClass, Type type, Genson genson) { BeanDescriptor<T> desc = (BeanDescriptor<T>) cache.get(type); if (desc == null) { for (BeanDescriptorProvider provider : providers) { desc = provider.provide(ofClass, type, genson); if (desc != null) break; } cache.putIfAbsent(type, desc); } return desc; } }
BeanDescriptorProvider provider = new Genson().getBeanDescriptorFactory(); BeanDescriptor<MyClass> descriptor = provider.provide(MyClass.class, genson); ObjectReader reader = new JsonReader(jsonString); MyClass existingObject = descriptor.deserialize(existingObject, reader, new Context(genson));
/** * Deserializes the stream in the existing object. Note however that this works only for Pojos * and doesn't handle nested objects (will be overridden by the values from the stream). * * @return the object enriched with the properties from the stream. */ public <T> T deserializeInto(ObjectReader reader, T object, Context ctx) { BeanDescriptor<T> bd = (BeanDescriptor<T>) getBeanDescriptorProvider().provide(object.getClass(), this); bd.deserialize(object, reader, ctx); return object; }
/** * Deserializes the stream in the existing object. Note however that this works only for Pojos * and doesn't handle nested objects (will be overridden by the values from the stream). * * @return the object enriched with the properties from the stream. */ public <T> T deserializeInto(ObjectReader reader, T object, Context ctx) { BeanDescriptor<T> bd = (BeanDescriptor<T>) getBeanDescriptorProvider().provide(object.getClass(), this); bd.deserialize(object, reader, ctx); return object; }
@SuppressWarnings("unchecked") protected <T> T provide(Class<T> forClass, Type withParameterType, Map<Type, ? extends T> fromTypeMap, Genson genson) { if (fromTypeMap.containsKey(withParameterType)) return fromTypeMap.get(withParameterType); Type wrappedParameterType = withParameterType; if (withParameterType instanceof Class<?> && ((Class<?>) withParameterType).isPrimitive()) wrappedParameterType = TypeUtil.wrap((Class<?>) withParameterType); for (Iterator<Factory<?>> it = factories.iterator(); it.hasNext(); ) { Factory<?> factory = it.next(); Object object; Type factoryType = TypeUtil.lookupGenericType(Factory.class, factory.getClass()); factoryType = TypeUtil.expandType(factoryType, factory.getClass()); // it is a parameterized type and we want the parameter corresponding to Serializer from // Factory<Serializer<?>> factoryType = TypeUtil.typeOf(0, factoryType); Type factoryParameter = TypeUtil.typeOf(0, factoryType); if (forClass.isAssignableFrom(TypeUtil.getRawClass(factoryType)) && TypeUtil.match(wrappedParameterType, factoryParameter, false) && (object = factory.create(withParameterType, genson)) != null) { return forClass.cast(object); } } return (T) beanDescriptorProvider.provide(TypeUtil.getRawClass(withParameterType), withParameterType, genson); }
@SuppressWarnings("unchecked") protected <T> T provide(Class<T> forClass, Type withParameterType, Map<Type, ? extends T> fromTypeMap, Genson genson) { if (fromTypeMap.containsKey(withParameterType)) return fromTypeMap.get(withParameterType); Type wrappedParameterType = withParameterType; if (withParameterType instanceof Class<?> && ((Class<?>) withParameterType).isPrimitive()) wrappedParameterType = TypeUtil.wrap((Class<?>) withParameterType); for (Iterator<Factory<?>> it = factories.iterator(); it.hasNext(); ) { Factory<?> factory = it.next(); Object object; Type factoryType = TypeUtil.lookupGenericType(Factory.class, factory.getClass()); factoryType = TypeUtil.expandType(factoryType, factory.getClass()); // it is a parameterized type and we want the parameter corresponding to Serializer from // Factory<Serializer<?>> factoryType = TypeUtil.typeOf(0, factoryType); Type factoryParameter = TypeUtil.typeOf(0, factoryType); if (forClass.isAssignableFrom(TypeUtil.getRawClass(factoryType)) && TypeUtil.match(wrappedParameterType, factoryParameter, false) && (object = factory.create(withParameterType, genson)) != null) { return forClass.cast(object); } } return (T) beanDescriptorProvider.provide(TypeUtil.getRawClass(withParameterType), withParameterType, genson); }