de.hpi.fgis.dude.util.data.json.auto
Class AutoJsonSerialization<T>

java.lang.Object
  extended by de.hpi.fgis.dude.util.data.json.auto.AutoJsonSerialization<T>
Type Parameters:
T - the type to serialize
Direct Known Subclasses:
ArrayJsonSerialization, CollectionJsonSerialization, CompositeJsonSerialization, EnumJsonSerialization

public abstract class AutoJsonSerialization<T>
extends Object

Provides json serialization for a specific BoundType.

Author:
Arvid.Heise

Constructor Summary
protected AutoJsonSerialization(BoundType type)
          Initialization for the given type
 
Method Summary
protected  void finishTypeSerializationRead(DuDeJsonParser<?> parser, Class<T> serializedType)
          Cleanup method used in conjunction with #readSerializedType(DuDeJsonParser, boolean).
 Class<T> getRawType()
          Returns the raw type for which this serialization class was created.
 BoundType getType()
          Returns the BoundType for which this serialization class was created.
protected  boolean isInstantiable(Class<? extends Object> clazz)
          Determines whether the given class has an accessible default constructor and caches the result.
 T read(DuDeJsonParser<?> parser)
          Creates an instance of the wrapped type and initializes the content with the json provided by the given DuDeJsonParser.
abstract  T read(DuDeJsonParser<?> parser, Object currentValue)
          Creates an instance of the wrapped type and initializes the content with the json provided by the given DuDeJsonParser.
 void readFields(DuDeJsonParser<?> parser, T jsonable)
          Reads all non-transient fields of the jsonable to the DuDeJsonParser.
protected  Class<T> readSerializedType(DuDeJsonParser<?> parser, boolean leaveRecordIntact, Class<?> declaredType)
          Parsing equivalent of writeWithType(DuDeJsonGenerator, Object).
Since the type is not written in every case, this method also supports a non-destructive way for testing of type information.
If leaveRecordIntact is set and no type information has been detected, it appears as if the method has never been invoked.
However, if type information has been detected, it shall be removed from the stream and the parsed class shall be returned.
abstract  void write(DuDeJsonGenerator generator, T jsonable)
          Writes the given instance to the specified DuDeJsonGenerator.
 void writeFields(DuDeJsonGenerator generator, Object jsonable)
          Writes all non-transient fields of the jsonable to the DuDeJsonGenerator.
protected  void writeWithType(DuDeJsonGenerator generator, T jsonable)
          Writes the given value with additional type information.
This method should be used when the actual type of a value cannot be inferred from the declaration.
The typical scenario is the usage of polymorphism without secondary information which would allow to infer the type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AutoJsonSerialization

protected AutoJsonSerialization(BoundType type)
Initialization for the given type

Parameters:
type - the wrapped type
Method Detail

getRawType

public Class<T> getRawType()
Returns the raw type for which this serialization class was created.

Returns:
the raw type

getType

public BoundType getType()
Returns the BoundType for which this serialization class was created.

Returns:
the BoundType

isInstantiable

protected boolean isInstantiable(Class<? extends Object> clazz)
Determines whether the given class has an accessible default constructor and caches the result.

Parameters:
clazz - the class to check
Returns:
true if it is instantiable via ReflectUtil.newInstance(Class)

read

public final T read(DuDeJsonParser<?> parser)
             throws org.codehaus.jackson.JsonParseException,
                    IOException
Creates an instance of the wrapped type and initializes the content with the json provided by the given DuDeJsonParser.

Parameters:
parser - the parser to read from
Returns:
an instance of T
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.

read

public abstract T read(DuDeJsonParser<?> parser,
                       Object currentValue)
                throws org.codehaus.jackson.JsonParseException,
                       IOException
Creates an instance of the wrapped type and initializes the content with the json provided by the given DuDeJsonParser. This method might reuse the currentValue but should in most cases use the actual type information of the currentValue to create a new instance.

Parameters:
parser - the parser to read from
currentValue - the current value of the field which should be populated by the result of this method or null if this is the root object
Returns:
an instance of T
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading from the stream.

readFields

public void readFields(DuDeJsonParser<?> parser,
                       T jsonable)
                throws org.codehaus.jackson.JsonParseException,
                       IOException
Reads all non-transient fields of the jsonable to the DuDeJsonParser. It expects the given jsonParser to currently have an opened record and it leaves it open.

Parameters:
parser - the DuDeJsonParser to read from
jsonable - the AutoJsonable to initialize
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading the data.

write

public abstract void write(DuDeJsonGenerator generator,
                           T jsonable)
                    throws org.codehaus.jackson.JsonGenerationException,
                           IOException
Writes the given instance to the specified DuDeJsonGenerator.

Parameters:
generator - the generator to write to
jsonable - the jsonable to write
Throws:
org.codehaus.jackson.JsonGenerationException - If an error occurs while generating the Json syntax.
IOException - If an error occurs while accessing the underlying stream.

writeFields

public void writeFields(DuDeJsonGenerator generator,
                        Object jsonable)
                 throws org.codehaus.jackson.JsonGenerationException,
                        IOException
Writes all non-transient fields of the jsonable to the DuDeJsonGenerator. It expects the given jsonGenerator to currently have an opened record and it leaves it open.

Parameters:
generator - the DuDeJsonGenerator to write to
jsonable - the object, the fields of which to write
Throws:
org.codehaus.jackson.JsonGenerationException - If an error occurs while generating the Json syntax.
IOException - If an error occurs while accessing the underlying stream.

writeWithType

protected void writeWithType(DuDeJsonGenerator generator,
                             T jsonable)
                      throws org.codehaus.jackson.JsonGenerationException,
                             IOException
Writes the given value with additional type information.
This method should be used when the actual type of a value cannot be inferred from the declaration.
The typical scenario is the usage of polymorphism without secondary information which would allow to infer the type.

Parameters:
generator - the DuDeJsonGenerator to write to
jsonable - the object to write
Throws:
org.codehaus.jackson.JsonGenerationException - If an error occurs while generating the Json syntax.
IOException - If an error occurs while accessing the underlying stream.

readSerializedType

protected Class<T> readSerializedType(DuDeJsonParser<?> parser,
                                      boolean leaveRecordIntact,
                                      Class<?> declaredType)
                               throws org.codehaus.jackson.JsonParseException,
                                      IOException
Parsing equivalent of writeWithType(DuDeJsonGenerator, Object).
Since the type is not written in every case, this method also supports a non-destructive way for testing of type information.
If leaveRecordIntact is set and no type information has been detected, it appears as if the method has never been invoked.
However, if type information has been detected, it shall be removed from the stream and the parsed class shall be returned. In that case, the original record remains opened.

In contrast, leaveRecordIntact is not set, an opened record will be returned in both cases.

Parameters:
parser - the DuDeJsonParser to read from
leaveRecordIntact - true if the record should not be opened if no type information is encoded in the json
declaredType - the declared type of the field
Returns:
the encoded type information or null if none was detected
Throws:
org.codehaus.jackson.JsonParseException - If the class cannot be resolved or an error occurred while parsing the data.
IOException - If an error occurred while reading the data.

finishTypeSerializationRead

protected void finishTypeSerializationRead(DuDeJsonParser<?> parser,
                                           Class<T> serializedType)
                                    throws org.codehaus.jackson.JsonParseException,
                                           IOException
Cleanup method used in conjunction with #readSerializedType(DuDeJsonParser, boolean).

Parameters:
parser - the DuDeJsonParser to read from
serializedType - the return value of #readSerializedType(DuDeJsonParser, boolean)
Throws:
org.codehaus.jackson.JsonParseException - If the class cannot be resolved or an error occurred while parsing the data.
IOException - If an error occurred while reading the data.


Copyright © 2011 Hasso Plattner Institute - Chair of Information Systems. All Rights Reserved.