Package org.cakelab.json.codec
Class JSONMapping<JAVAT,JSONT>
- java.lang.Object
-
- org.cakelab.json.codec.JSONMapping<JAVAT,JSONT>
-
- Type Parameters:
JAVAT
- Java typeJSONT
- JSON type
public abstract class JSONMapping<JAVAT,JSONT> extends java.lang.Object
Warning: Work in progress
This is an early prototype of a way to add own type mappings to the json modeller. An implementation of this abstract class can be added to theJSONCodecConfiguration
as 'mapping'. A provided mapping gets priority over generic mappings. Currently, custom mappings can be provided for any combination of Java and JSON type. However JSON types are limited to the value types of the JSON model, such as JSONObject, JSONArray, Long and Double for JSON numbers, String or Boolean. A mapping rule is always selected by its Java type first. However, there can exist multiple mappings for the same type but different JSON types. When encoding to JSON, the modeller will then select one by random (since they are stored in a hash map). When decoding from JSON to Java, the modeller will lookup the mapping with matching tuple of Java and JSON type.- Author:
- homac
-
-
Constructor Summary
Constructors Constructor Description JSONMapping(java.lang.Class<JAVAT> javaType, java.lang.Class<JSONT> jsonType)
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract JAVAT
toJava(JSONModeller modeller, JSONT jsonValue)
Instantiate a new Java object of type JAVAT and initialise it from JSON value of type JSONT.abstract void
toJava(JSONModeller modeller, JSONT jsonAny, JAVAT targetObject)
Initialise given javaValue from give jsonValue.abstract JSONT
toJson(JSONModeller modeller, JAVAT javaValue)
Implementation maps given javaValue to a valid JSON value.
-
-
-
Method Detail
-
toJson
public abstract JSONT toJson(JSONModeller modeller, JAVAT javaValue) throws JSONException
Implementation maps given javaValue to a valid JSON value.- Throws:
JSONException
-
toJava
public abstract JAVAT toJava(JSONModeller modeller, JSONT jsonValue) throws JSONException
Instantiate a new Java object of type JAVAT and initialise it from JSON value of type JSONT. In case the Java value is a constant (such as an enum value) it is suggested to return the matching constant instead.- Throws:
JSONException
-
toJava
public abstract void toJava(JSONModeller modeller, JSONT jsonAny, JAVAT targetObject) throws JSONException
Initialise given javaValue from give jsonValue.- Throws:
JSONException
-
-