Class ClassMetadataConverter<T>
- java.lang.Object
-
- com.oracle.coherence.io.json.genson.Wrapper<Converter<T>>
-
- com.oracle.coherence.io.json.genson.convert.ClassMetadataConverter<T>
-
- Type Parameters:
T-
- All Implemented Interfaces:
Converter<T>,Deserializer<T>,Serializer<T>,AnnotatedElement
public class ClassMetadataConverter<T> extends Wrapper<Converter<T>> implements Converter<T>
Converter responsible of writing and reading @class metadata. This is useful if you want to be able to deserialize all serialized objects without knowing their concrete type. Metadata is written only in objects (never in arrays or literals) and is always the first element in the object. Most default converters are annotated with @HandleClassMetada indicating that they will not have class metadata written nor use it during deserialization. This feature is disabled by default, to enable it useGensonBuilder.useClassMetadata(boolean). Genson provides also a aliases mechanism that will replace the class name with the value of your alias in the generated stream. You should use it as it is more "secure" and provides you more flexibility. Indeed if you change the name or package of your class you will still be able to deserialize to it. An example allowing to serialize a object and then deserialize it back without knowing its type would be:class Foo { } Genson genson = new GensonBuilder().useClassMetadata(true).addAlias("foo", Foo.class).create(); String json = genson.serialize(new Foo()); // json value will be {"@class":"Foo"} Foo foo = (Foo) genson.deserialize(json, Object.class);- Author:
- Eugen Cepoi
- See Also:
ObjectWriter.metadata(key, value),ObjectReader.metadata("class"),Genson.aliasFor(Class)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classClassMetadataConverter.ClassMetadataConverterFactory
-
Constructor Summary
Constructors Constructor Description ClassMetadataConverter(Class<T> tClass, Converter<T> delegate, boolean classMetadataWithStaticType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tdeserialize(ObjectReader reader, Context ctx)voidserialize(T obj, ObjectWriter writer, Context ctx)-
Methods inherited from class com.oracle.coherence.io.json.genson.Wrapper
decorate, getAnnotation, getAnnotations, getDeclaredAnnotations, isAnnotationPresent, isOfType, isWrapped, toAnnotatedElement, unwrap
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.reflect.AnnotatedElement
getAnnotationsByType, getDeclaredAnnotation, getDeclaredAnnotationsByType
-
-
-
-
Method Detail
-
serialize
public void serialize(T obj, ObjectWriter writer, Context ctx) throws Exception
- Specified by:
serializein interfaceConverter<T>- Specified by:
serializein interfaceSerializer<T>- Parameters:
obj- we want to serialize. The object is of type T or a subclass (if this serializer has been registered for subclasses).writer- to use to write data to the output stream.ctx- the current context.- Throws:
JsonBindingExceptionJsonStreamExceptionException
-
deserialize
public T deserialize(ObjectReader reader, Context ctx) throws Exception
- Specified by:
deserializein interfaceConverter<T>- Specified by:
deserializein interfaceDeserializer<T>- Parameters:
reader- used to read data from.ctx- the current context.- Returns:
- an instance of T or a subclass of T.
- Throws:
JsonBindingExceptionJsonStreamExceptionException
-
-