public class DefaultJsonMapper extends Object implements JsonMapper
Constructor and Description |
---|
DefaultJsonMapper() |
Modifier and Type | Method and Description |
---|---|
protected <T> T |
createInstance(Class<T> type)
Creates a new instance of the given
type . |
protected Set<String> |
facebookFieldNamesWithMultipleMappings(List<ReflectionUtils.FieldWithAnnotation<Facebook>> fieldsWithAnnotation)
Finds any Facebook JSON fields that are mapped to more than 1 Java field.
|
protected String |
getFacebookFieldName(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation)
For a Java field annotated with the
Facebook annotation, figure out
what the corresponding Facebook JSON field name to map to it is. |
protected boolean |
isEmptyObject(String json)
Is the given JSON equivalent to the empty object (
{} )? |
protected void |
logMultipleMappingFailedForField(String facebookFieldName,
ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation,
String json)
Dumps out a log message when one of a multiple-mapped Facebook field name
JSON-to-Java mapping operation fails.
|
<T> List<T> |
toJavaList(String json,
Class<T> type)
Given a JSON string, create and return a new instance of a corresponding
Java
List which contains elements of type type . |
<T> T |
toJavaObject(String json,
Class<T> type)
Given a JSON string, create and return a new instance of a corresponding
Java object of type
type . |
protected Object |
toJavaType(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation,
JsonObject jsonObject,
String facebookFieldName)
Extracts JSON data for a field according to its
Facebook annotation
and returns it converted to the proper Java type. |
String |
toJson(Object object)
Given a Java
object , create and return a JSON string that
represents it. |
protected Object |
toJsonInternal(Object object)
Recursively marshal the given
object to JSON. |
protected <T> T |
toPrimitiveJavaType(String json,
Class<T> type)
Given a
json value of something like MyValue or 123
, return a representation of that value of type type . |
protected void |
verifyThatJsonIsOfObjectType(String json)
Is the given
json a valid JSON object? |
public <T> List<T> toJavaList(String json, Class<T> type)
JsonMapper
List
which contains elements of type type
.
The Java type
must have a visible no-argument constructor.toJavaList
in interface JsonMapper
T
- Java type to map to for each element of the list.json
- The JSON to be mapped to a Java type.type
- Java type token.List
which contains elements of type
type
) representation of the JSON input.JsonMapper.toJavaList(String, Class)
public <T> T toJavaObject(String json, Class<T> type)
JsonMapper
type
.
The Java type
must have a visible no-argument constructor.toJavaObject
in interface JsonMapper
T
- Java type to map to.json
- The JSON to be mapped to a Java type.type
- Java type token.type
) representation of the JSON
input.JsonMapper.toJavaObject(String, Class)
protected void logMultipleMappingFailedForField(String facebookFieldName, ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation, String json)
facebookFieldName
- The Facebook field name.fieldWithAnnotation
- The Java field to map to and its annotation.json
- The JSON that failed to map to the Java field.protected String getFacebookFieldName(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation)
Facebook
annotation, figure out
what the corresponding Facebook JSON field name to map to it is.fieldWithAnnotation
- A Java field annotated with the Facebook
annotation.protected Set<String> facebookFieldNamesWithMultipleMappings(List<ReflectionUtils.FieldWithAnnotation<Facebook>> fieldsWithAnnotation)
fieldsWithAnnotation
- Java fields annotated with the Facebook
annotation.public String toJson(Object object)
JsonMapper
object
, create and return a JSON string that
represents it.
The object
's properties will be traversed recursively, allowing for
arbitrarily complex JSON generation.toJson
in interface JsonMapper
object
- The Java object to map to JSON. Can be a Javabean,
List
, or Map
.JsonMapper.toJson(Object)
protected void verifyThatJsonIsOfObjectType(String json)
json
a valid JSON object?json
- The JSON to check.FacebookJsonMappingException
- If json
is not a valid JSON object.protected Object toJsonInternal(Object object)
object
to JSON.
Used by toJson(Object)
.
object
- The object to marshal.object
.FacebookJsonMappingException
- If an error occurs while marshaling to JSON.protected <T> T toPrimitiveJavaType(String json, Class<T> type)
json
value of something like MyValue
or 123
, return a representation of that value of type type
.
This is to support non-legal JSON served up by Facebook for API calls like
Friends.get
(example result: [222333,1240079]
).
T
- The Java type to map to.json
- The non-legal JSON to map to the Java type.type
- Type token.json
.FacebookJsonMappingException
- If an error occurs while mapping JSON to Java.protected Object toJavaType(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation, JsonObject jsonObject, String facebookFieldName) throws JsonException, FacebookJsonMappingException
Facebook
annotation
and returns it converted to the proper Java type.fieldWithAnnotation
- The field/annotation pair which specifies what Java type to
convert to.jsonObject
- "Raw" JSON object to pull data from.facebookFieldName
- Specifies what JSON field to pull "raw" data from.JsonException
- If an error occurs while mapping JSON to Java.FacebookJsonMappingException
- If an error occurs while mapping JSON to Java.protected <T> T createInstance(Class<T> type)
type
.T
- Java type to map to.type
- Type token.type
.FacebookJsonMappingException
- If an error occurs when creating a new instance (type
is
inaccessible, doesn't have a public no-arg constructor, etc.)protected boolean isEmptyObject(String json)
{}
)?json
- The JSON to check.true
if the JSON is equivalent to the empty object,
false
otherwise.Copyright © 2014. All rights reserved.