Package com.sdicons.json.mapper
Class JSONMapper
- java.lang.Object
-
- com.sdicons.json.mapper.JSONMapper
-
public class JSONMapper extends java.lang.Object
The mapper class is able to convert a JSON representation to/from a Java representation. The mapper's goal is to produce a nice and clean JSON output which can easily be used in e.g. Javascript context. As a consequence, not all Java constructs are preserved during the conversion to/from JSON. The mapper is the best choice in an application where the clean JSON format is central. If the emphasis is on exact Java serialization where types are preserved, take a look at the Serializer tool. The main difference between the serializer and the mapper is that the serializer keeps as much type information and structure information in the JSON data where the mapper uses the type information in the provided Java classes to interprete the JSON data.
-
-
Field Summary
Fields Modifier and Type Field Description private static HelperRepository<SimpleMapperHelper>
repo
-
Constructor Summary
Constructors Constructor Description JSONMapper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addHelper(SimpleMapperHelper aHelper)
Add custom helper class.static HelperRepository<SimpleMapperHelper>
getRepository()
static java.lang.Object
toJava(JSONValue aValue)
Map a JSON representation to a Java object.static java.lang.Object
toJava(JSONValue aValue, java.lang.Class aPojoClass)
Map a JSON representation to a Java object.static java.lang.Object
toJava(JSONValue aValue, java.lang.reflect.ParameterizedType aGenericType)
Map a JSON representation to a Java object.static JSONValue
toJSON(java.lang.Object aPojo)
Map a POJO to the JSON representation.static void
useJavaBeanAccess()
The objects that fall back on the general object mapper will be mapped by using their JavaBean properties.static void
usePojoAccess()
The objects that fall back on the general object mapper will be mapped by using their fields directly.
-
-
-
Field Detail
-
repo
private static HelperRepository<SimpleMapperHelper> repo
-
-
Method Detail
-
toJava
public static java.lang.Object toJava(JSONValue aValue, java.lang.Class aPojoClass) throws MapperException
Map a JSON representation to a Java object.- Parameters:
aValue
- The JSON value that has to be mapped.aPojoClass
- The class to which the JSON object should be mapped.- Returns:
- The resulting Java object, the POJO representation.
- Throws:
MapperException
- when an error occurs during mapping.
-
toJava
public static java.lang.Object toJava(JSONValue aValue, java.lang.reflect.ParameterizedType aGenericType) throws MapperException
Map a JSON representation to a Java object.- Parameters:
aValue
- The JSON value that has to be mapped.aGenericType
- A type indication to help the mapper map the JSON text.- Returns:
- The resulting Java POJO.
- Throws:
MapperException
- When the JSON text cannot be mapped to POJO.
-
toJava
public static java.lang.Object toJava(JSONValue aValue) throws MapperException
Map a JSON representation to a Java object. Since no class nor type hint is passed to the mapper, this method can only handle the most basic mappings.- Parameters:
aValue
- The JSON value that has to be mapped.- Returns:
- he resulting Java POJO.
- Throws:
MapperException
- When the JSON text cannot be mapped to POJO.
-
toJSON
public static JSONValue toJSON(java.lang.Object aPojo) throws MapperException
Map a POJO to the JSON representation.- Parameters:
aPojo
- to be mapped to JSON.- Returns:
- The JSON representation.
- Throws:
MapperException
- If something goes wrong during mapping.
-
addHelper
public static void addHelper(SimpleMapperHelper aHelper)
Add custom helper class.- Parameters:
aHelper
- the custom helper you want to add to the mapper.
-
getRepository
public static HelperRepository<SimpleMapperHelper> getRepository()
-
usePojoAccess
public static void usePojoAccess()
The objects that fall back on the general object mapper will be mapped by using their fields directly. Without further annotations, the default constructor without arguments will be used in the POJO. If this is not sufficient, the @JSONConstruct and @JSONMap annotations can be used as well in the mapped POJO to indicate which constructor has to be used.
-
useJavaBeanAccess
public static void useJavaBeanAccess()
The objects that fall back on the general object mapper will be mapped by using their JavaBean properties. The mapped JavaBean always needs a default constructor without arguments.
-
-