Class 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 use 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);
     
    Author:
    Eugen Cepoi
    See Also:
    ObjectWriter.metadata(key, value), ObjectReader.metadata("class"), Genson.aliasFor(Class)