T - public class ClassMetadataConverter<T> extends Wrapper<Converter<T>> implements Converter<T>
GensonBuilder.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);
ObjectWriter.metadata(key, value),
ObjectReader.metadata("class"),
Genson.aliasFor(Class)| Modifier and Type | Class and Description |
|---|---|
static class |
ClassMetadataConverter.ClassMetadataConverterFactory |
| Constructor and Description |
|---|
ClassMetadataConverter(Class<T> tClass,
Converter<T> delegate,
boolean classMetadataWithStaticType,
boolean writeClassMetadata,
boolean readClassMetadata) |
| Modifier and Type | Method and Description |
|---|---|
T |
deserialize(ObjectReader reader,
Context ctx) |
void |
serialize(T obj,
ObjectWriter writer,
Context ctx) |
decorate, getAnnotation, getAnnotations, getDeclaredAnnotations, isAnnotationPresent, isOfType, isWrapped, toAnnotatedElement, unwrapclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetAnnotationsByType, getDeclaredAnnotation, getDeclaredAnnotationsByTypepublic void serialize(T obj, ObjectWriter writer, Context ctx) throws Exception
serialize in interface Converter<T>serialize in interface Serializer<T>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.JsonBindingExceptionJsonStreamExceptionExceptionpublic T deserialize(ObjectReader reader, Context ctx) throws Exception
deserialize in interface Converter<T>deserialize in interface Deserializer<T>reader - used to read data from.ctx - the current context.JsonBindingExceptionJsonStreamExceptionExceptionCopyright © 2019. All rights reserved.