T
- class type to be serialisedpublic class Serializer<T extends IQuimpSerialize> extends Object implements ParameterizedType
The Serializer class wraps provided objects and converts it to Gson together with itself.
Serializer adds fields like wrapped class name and versioning data (@link QuimpVersion
)
to JSON.
Restored object is constructed using its constructor. If JSON file does not contain variable
available in class being restored, it will have the value assigned in constructor or null. GSon
overrides variables after they have been created in normal process of object building. Check
fromReader(Reader)
for details.
This serializer accepts only classes derived from IQuimpSerialize interface. Saved class is packed in top level structure that contains version of software and wrapped class name. Exemplary use case: SerializerTest#testLoad_1()
There is option to skip call afterSerialzie() or beforeSerialzie() method on class restoring
or saving To do so set doAfterSerialize
to false or doBeforeSerialize
Serializer supports Since, Until tags from GSon library. User can write his own
converters executed if specified condition is met. Serializer compares version of callee tool
(provided in Serializer constructor) with trigger version returned by converter
IQconfOlderConverter
and executes conversion provided by it.
Important: Until and Since tags are resolved using version of QCONF provided from callee on json saving or by version read from QCONF file on its loading. Version read from JSON is also used to decide whether apply converter or not but on load only.
Modifier and Type | Field and Description |
---|---|
String |
className
Name of wrapped class, decoded from object.
|
String |
createdOn
Date when file has been created.
|
protected boolean |
doAfterSerialize
Indicates if afterSerialze should be called.
|
protected boolean |
doBeforeSerialize
Indicates if
IQuimpSerialize.beforeSerialize() should be called. |
com.google.gson.GsonBuilder |
gsonBuilder
The gson builder.
|
T |
obj
Wrapped object being serialized.
|
QuimpVersion |
timeStamp
Version and other information passed to serializer.
|
Constructor and Description |
---|
Serializer(T obj,
QuimpVersion version)
Constructor used for saving wrapped class.
|
Serializer(Type type,
QuimpVersion version)
Default constructor used for restoring object.
|
Modifier and Type | Method and Description |
---|---|
Serializer<T> |
fromReader(Reader reader)
Restore wrapped object from JSON string.
|
Serializer<T> |
fromString(String json)
Restore wrapped object from JSON string.
|
Type[] |
getActualTypeArguments() |
String |
getCurrentDate()
Get current date included in saved file.
|
Type |
getOwnerType() |
Double |
getQconfVersion(Reader reader)
Read QuimP version from QCONF file.
|
Type |
getRawType() |
Serializer<T> |
load(File filename)
Load wrapped object from JSON file.
|
Serializer<T> |
load(String filename)
Load GSon file.
|
void |
registerConverter(IQconfOlderConverter<T> converter)
This method register format converter on list of converters.
|
void |
registerInstanceCreator(Class<T> type,
Object typeAdapter)
Register constructor for wrapped class.
|
void |
save(String filename)
Save wrapped object passed in constructor as JSON file.
|
void |
setPretty()
Sets pretty JSON formatting on save operation.
|
String |
toString()
Convert wrapped class to JSON representation together with Serializer wrapper
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getTypeName
public transient com.google.gson.GsonBuilder gsonBuilder
protected transient boolean doAfterSerialize
protected transient boolean doBeforeSerialize
IQuimpSerialize.beforeSerialize()
should be called.
Rather for tests as IQuimpSerialize.beforeSerialize()
is always required.
public String className
public QuimpVersion timeStamp
public String createdOn
public T extends IQuimpSerialize obj
public Serializer(Type type, QuimpVersion version)
Template T can not be restored during runtime thus the type of wrapped object is not known for GSon. This is why this type must be passed explicitly to Serializer.
type
- class typeversion
- Version of framework this class is called from.public Serializer(T obj, QuimpVersion version)
obj
- Object being savedversion
- Version of framework this class is called from.public void save(String filename) throws FileNotFoundException
Calls IQuimpSerialize.beforeSerialize()
before save.
filename
- Name of fileFileNotFoundException
- if problem with savingsetPretty()
,
Serializer(IQuimpSerialize, QuimpVersion)
,
toString()
public Serializer<T> load(String filename) throws IOException, com.google.gson.JsonSyntaxException, com.google.gson.JsonIOException, Exception
filename
- to loadIOException
- when file can not be foundcom.google.gson.JsonSyntaxException
- on wrong syntaxcom.google.gson.JsonIOException
- on wrong syntaxException
- any other caseload(File)
public Serializer<T> load(File filename) throws IOException, com.google.gson.JsonSyntaxException, com.google.gson.JsonIOException, Exception
Calls IQuimpSerialize.afterSerialize()
after load. The general steps taken on GSon
load are as follows:
filename
- to loadIOException
- when file can not be foundcom.google.gson.JsonSyntaxException
- on wrong syntaxcom.google.gson.JsonIOException
- on wrong syntaxException
- any other casefromReader(Reader)
public Serializer<T> fromString(String json) throws com.google.gson.JsonSyntaxException, com.google.gson.JsonIOException, Exception
json
- string with jsoncom.google.gson.JsonSyntaxException
- on wrong syntaxcom.google.gson.JsonIOException
- on wrong syntaxException
- any other casefromReader(Reader)
public Serializer<T> fromReader(Reader reader) throws com.google.gson.JsonSyntaxException, com.google.gson.JsonIOException, Exception
reader
- reader that provides JSon stringException
- from afterSerialize() method (specific to wrapped object)IOException
- when file can not be readcom.google.gson.JsonSyntaxException
- on bad file or when class has not been restored correctlycom.google.gson.JsonIOException
- This exception is raised when Gson was unable to read an input stream
or write to onload(File)
public void registerConverter(IQconfOlderConverter<T> converter)
Registered converters are called on every object deserialisation in order that they were registered. Converter is run when version of tool is higher than version of converter.
converter
- converterIQconfOlderConverter
,
convert(Serializer)
public String toString()
Calls com.github.celldynamics.quimp.IQuimpSerialize.beforeSerialize() before conversion
toString
in class Object
setPretty()
public String getCurrentDate()
public void setPretty()
toString()
,
save(String)
public Double getQconfVersion(Reader reader)
It does not deserialize JSON, just plain string reading from file.
reader
- reader that delivers stringcom.google.gson.JsonSyntaxException
- on version read errorpublic void registerInstanceCreator(Class<T> type, Object typeAdapter)
It may be necessary during loading JSON file if wrapped class needs some parameters to restore its state on com.github.celldynamics.quimp.IQuimpSerialize.afterSerialize() call and those parameters are passed in constructor.
type
- Type of classtypeAdapter
- Wrapped object builder that implements InstanceCreator interface.IQuimpSerialize.afterSerialize()
,
https://github.com/google/gson/blob/master/UserGuide.md#TOC-InstanceCreator-for-a-Parameterized-Typepublic Type[] getActualTypeArguments()
getActualTypeArguments
in interface ParameterizedType
public Type getRawType()
getRawType
in interface ParameterizedType
public Type getOwnerType()
getOwnerType
in interface ParameterizedType
Copyright © 2002–2019 Department of Computer Science, Warwick University. All rights reserved.