Package com.sdicons.json.mapper
Class JSONMapper
java.lang.Object
com.sdicons.json.mapper.JSONMapper
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addHelper
(SimpleMapperHelper aHelper) Add custom helper class.static HelperRepository
<SimpleMapperHelper> static Object
Map a JSON representation to a Java object.static Object
Map a JSON representation to a Java object.static Object
toJava
(JSONValue aValue, ParameterizedType aGenericType) Map a JSON representation to a Java object.static JSONValue
Map a POJO to the JSON representation.static void
The objects that fall back on the general object mapper will be mapped by using their JavaBean properties.static void
The objects that fall back on the general object mapper will be mapped by using their fields directly.
-
Field Details
-
repo
-
-
Constructor Details
-
JSONMapper
public JSONMapper()
-
-
Method Details
-
toJava
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 Object toJava(JSONValue aValue, 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
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
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
Add custom helper class.- Parameters:
aHelper
- the custom helper you want to add to the mapper.
-
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.
-