-
Notifications
You must be signed in to change notification settings - Fork 1
Custom Serialisation
UrielCh edited this page Jul 31, 2015
·
1 revision
You can register Serialisation / DeSerialisation code to Json-smart using JSONValue.registerWriter(Class<?> cls, JsonWriterI<T> writer)
and JSONValue.registerReader(Class<T> type, JsonReaderI<T> mapper)
Overwrite Serialisation is very easy
registerWriter(new JsonWriterI<Boolean>() {
void writeJSONString(Boolean value, Appendable out, JSONStyle compression) throws IOException {
out.append(value.toString());
}
}, Boolean.class);
JsonReaderI<T>
Interface is more complex, and is an Class you will need to extand.
JsonWriterI contains methods for Array, and Objects.
At runtime JsonReaderI methods will be called in this order:
startObject(theParentKey name)
createObject() // can return an non final type named TMP_CLS
getValue(key1) // may be call to check for colision
getType(key1) // must return the key final Class
setValue(TMP_CLS Object, key1, value1);
getValue(key2) // may be call to check for colision
getType(key2) // must return the key final Class
setValue(TMP_CLS Object, key2, value2);
convert(TMP_CLS Object); // convert or cast TMP_CLS to the final class
At runtime JsonReaderI methods will be called in this order:
startArray(theParentKey name)
createObject() // can return an non final type named TMP_CLS
addValue(TMP_CLS Object, value1);
addValue(TMP_CLS Object, value2);
convert(TMP_CLS Object); // convert or cast TMP_CLS to the final class
since all Element in a array should have the same type, getType is not used in array creation