public class JSONMapper extends Object
public class Person {
private String name;
private String phoneNumber;
private int age;
// Getters and setters omitted.
// ...
}
The mapping code
// Create a person.
Person p = new Person();
p.setName("Mr. Jason Tools");
p.setPhoneNumber("0123456789");
p.setAge(40);
// Map and print.
JSONMapper mapper = new JSONMapper();
JSONValue json = mapper.toJSON(p);
System.out.println(json.render(true));
The resulting JSON text
{
"age" : 40,
"name" : "Mr. Jason Tools",
"phoneNumber" : "0123456789"
}
JSONSerializer
Modifier and Type | Field and Description |
---|---|
static String |
DATEFORMAT_DEFAULT
The default pattern used by the DateMapper to convert String representations into Java dates.
|
static String |
OBJMAPPING_FIELD
The object mapper will use field access to access the contents of an instance.
|
static String |
OBJMAPPING_PROPERTY
The object mapper will use JavaBean getters and setters to access the contents of an instance.
|
static String |
OPT_DATEFORMAT
The mapping option DATAFORMAT indicates which date pattern will be used by the DateMapper to convert String
values to Dates and vice versa.
|
static String |
OPT_OBJMAPPING
The mapping option OBJMAPPING indicates whether property mapping of field mapping will be used.
|
Constructor and Description |
---|
JSONMapper() |
JSONMapper(ClassMapper... helpers) |
Modifier and Type | Method and Description |
---|---|
void |
addHelper(ClassMapper helper)
Add custom helper class.
|
Object |
getMappingOption(String key,
Object defaultValue)
Get the value of a mapping option.
|
boolean |
hasMappingOption(String key)
Check to see if an option was set explicitly or not.
|
void |
setMappingOption(String key,
Object value)
Add a mapping option.
|
Object |
toJava(JSONValue json)
Map a JSON representation to a Java object.
|
Object |
toJava(JSONValue aValue,
Class<?> aPojoClass)
Map a JSON representation to a Java object.
|
Object |
toJava(JSONValue json,
ParameterizedType genericType)
Map a JSON representation to a Java object.
|
JSONValue |
toJSON(Object pojo)
Map a POJO to the JSON representation.
|
void |
useJavaBeanAccess()
The objects that fall back on the general object mapper will be mapped by
using their JavaBean properties.
|
void |
usePojoAccess()
The objects that fall back on the general object mapper will be mapped by
using their fields directly.
|
public static final String OPT_OBJMAPPING
OBJMAPPING_FIELD
or OBJMAPPING_PROPERTY
.public static final String OBJMAPPING_FIELD
OPT_OBJMAPPING
,
Constant Field Valuespublic static final String OBJMAPPING_PROPERTY
OPT_OBJMAPPING
,
Constant Field Valuespublic static final String OPT_DATEFORMAT
DATEFORMAT_DEFAULT
,
Constant Field Valuespublic static final String DATEFORMAT_DEFAULT
public JSONMapper(ClassMapper... helpers)
public JSONMapper()
public Object toJava(JSONValue aValue, Class<?> aPojoClass) throws MapperException
aValue
- The JSON value that has to be mapped.aPojoClass
- The class to which the JSON object should be mapped.MapperException
- when an error occurs during mapping.public Object toJava(JSONValue json, ParameterizedType genericType) throws MapperException
json
- The JSON value that has to be mapped.genericType
- A type indication to help the mapper map the JSON text.MapperException
- When the JSON text cannot be mapped to POJO.public Object toJava(JSONValue json) throws MapperException
json
- The JSON value that has to be mapped.MapperException
- When the JSON text cannot be mapped to POJO.public JSONValue toJSON(Object pojo) throws MapperException
pojo
- to be mapped to JSON.MapperException
- If something goes wrong during mapping.public void addHelper(ClassMapper helper)
helper
- the custom helper you want to add to the mapper.public void usePojoAccess()
public void useJavaBeanAccess()
public void setMappingOption(String key, Object value)
key
- The name of the option.value
- The value of the option.OPT_DATEFORMAT
,
OPT_OBJMAPPING
public Object getMappingOption(String key, Object defaultValue)
key
- The option name.defaultValue
- The default value that will be returned if the option is not set.OPT_DATEFORMAT
,
OPT_OBJMAPPING
public boolean hasMappingOption(String key)
key
- The option name.Copyright © 2012. All Rights Reserved.