public class JSONSerializer 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.
JSONSerializer serializer = new JSONSerializer();
JSONValue json = serializer.marshal(p);
System.out.println(json.render(true));
The resulting JSON text
{
">" : "O",
"&" : "id0",
"c" : "com.sdicons.json.examples.Person",
"=" :
{
"age" :
{
">" : "O",
"&" : "id1",
"c" : "java.lang.Integer",
"=" : "40"
},
"name" :
{
">" : "O",
"&" : "id2",
"c" : "java.lang.String",
"=" : "Mr. Jason Tools"
},
"phoneNumber" :
{
">" : "O",
"&" : "id3",
"c" : "java.lang.String",
"=" : "0123456789"
}
}
}
JSONMapper
Modifier and Type | Field and Description |
---|---|
static String |
DATESERIALIZE_DEFAULT |
static String |
ERR_MISSINGATTR |
static String |
ERR_MISSINGATTRVAL |
static String |
ERR_MISSINGSTRING |
static String |
OBJSERIALIZE_FIELD |
static String |
OBJSERIALIZE_PROPERTY |
static String |
OPT_DATESERIALIZE |
static String |
OPT_OBJSERIALIZE |
static String |
RNDR_ARR |
static String |
RNDR_ATTR_CLASS |
static String |
RNDR_ATTR_ID |
static String |
RNDR_ATTR_KIND |
static String |
RNDR_ATTR_REF |
static String |
RNDR_ATTR_TYPE |
static String |
RNDR_ATTR_VALUE |
static String |
RNDR_NULL |
static String |
RNDR_OBJ |
static String |
RNDR_OBJREF |
static String |
RNDR_PRIM |
static String |
RNDR_PRTITYP_BOOLEAN |
static String |
RNDR_PRTITYP_BYTE |
static String |
RNDR_PRTITYP_CHAR |
static String |
RNDR_PRTITYP_DOUBLE |
static String |
RNDR_PRTITYP_FLOAT |
static String |
RNDR_PRTITYP_INT |
static String |
RNDR_PRTITYP_LONG |
static String |
RNDR_PRTITYP_SHORT |
Constructor and Description |
---|
JSONSerializer() |
JSONSerializer(ClassSerializer... serializers) |
Modifier and Type | Method and Description |
---|---|
void |
addHelper(ClassSerializer aHelper)
Add custom helper class.
|
Object |
getSerializeOption(String key,
Object defaultValue) |
boolean |
hasSerializeOption(String key) |
JSONObject |
marshal(boolean aValue)
Convert a boolean primitive to JSON.
|
JSONObject |
marshal(byte aValue)
Convert a byte primitive to JSON.
|
JSONObject |
marshal(char aValue)
Convert a char primitive to JSON.
|
JSONObject |
marshal(double aValue)
Convert a double primitive to JSON.
|
JSONObject |
marshal(float aValue)
Convert a float primitive to JSON.
|
JSONObject |
marshal(int aValue)
Convert an int primitive to JSON.
|
JSONObject |
marshal(long aValue)
Convert a long primitive to JSON.
|
JSONObject |
marshal(Object aObj)
Convert a Java object to JSON.
|
JSONObject |
marshal(short aValue)
Convert a short primitive to JSON.
|
JSONObject |
marshalImpl(Object aObj,
HashMap<Object,Object> aPool) |
static void |
requireStringAttribute(JSONObject aElement,
String anAttribute) |
void |
setSerializeOption(String key,
Object value) |
SerializerValue |
unmarshal(JSONObject aElement)
Convert a JSON representation to the Java primitive or reference.
|
Object |
unmarshalImpl(JSONObject aElement,
HashMap<Object,Object> aObjectPool) |
void |
useJavaBeanAccess()
The objects that fall back on the general object helper will be serialized by
using their JavaBean properties.
|
void |
usePojoAccess()
The objects that fall back on the general object helper will be serialized by
using their fields directly.
|
public static final String OPT_OBJSERIALIZE
public static final String OBJSERIALIZE_FIELD
public static final String OBJSERIALIZE_PROPERTY
public static final String OPT_DATESERIALIZE
public static final String DATESERIALIZE_DEFAULT
public static final String RNDR_NULL
public static final String RNDR_OBJ
public static final String RNDR_OBJREF
public static final String RNDR_PRIM
public static final String RNDR_ARR
public static final String RNDR_ATTR_ID
public static final String RNDR_ATTR_KIND
public static final String RNDR_ATTR_TYPE
public static final String RNDR_ATTR_VALUE
public static final String RNDR_ATTR_CLASS
public static final String RNDR_ATTR_REF
public static final String RNDR_PRTITYP_BOOLEAN
public static final String RNDR_PRTITYP_BYTE
public static final String RNDR_PRTITYP_CHAR
public static final String RNDR_PRTITYP_SHORT
public static final String RNDR_PRTITYP_INT
public static final String RNDR_PRTITYP_LONG
public static final String RNDR_PRTITYP_FLOAT
public static final String RNDR_PRTITYP_DOUBLE
public static final String ERR_MISSINGATTR
public static final String ERR_MISSINGATTRVAL
public static final String ERR_MISSINGSTRING
public JSONSerializer()
public JSONSerializer(ClassSerializer... serializers)
public JSONObject marshal(boolean aValue)
aValue
- public JSONObject marshal(byte aValue)
aValue
- public JSONObject marshal(short aValue)
aValue
- public JSONObject marshal(char aValue)
aValue
- public JSONObject marshal(int aValue)
aValue
- public JSONObject marshal(long aValue)
aValue
- public JSONObject marshal(float aValue)
aValue
- public JSONObject marshal(double aValue)
aValue
- public JSONObject marshal(Object aObj) throws SerializerException
aObj
- SerializerException
- An error occurred while converting the Java object to JSON.public JSONObject marshalImpl(Object aObj, HashMap<Object,Object> aPool) throws SerializerException
SerializerException
public SerializerValue unmarshal(JSONObject aElement) throws SerializerException
aElement
- SerializerException
- An error occured while trying to convert the JSON representation into a
Java representation.public Object unmarshalImpl(JSONObject aElement, HashMap<Object,Object> aObjectPool) throws SerializerException
SerializerException
public static void requireStringAttribute(JSONObject aElement, String anAttribute) throws SerializerException
SerializerException
public void addHelper(ClassSerializer aHelper)
aHelper
- the custom helper you want to add to the serializer.public void usePojoAccess()
public void useJavaBeanAccess()
public boolean hasSerializeOption(String key)
Copyright © 2012. All Rights Reserved.