Package com.restfb
Class DefaultJsonMapper
java.lang.Object
com.restfb.DefaultJsonMapper
- All Implemented Interfaces:
JsonMapper
Default implementation of a JSON-to-Java mapper.
- Author:
- Mark Allen
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.restfb.JsonMapper
JsonMapper.JsonMappingCompleted
-
Constructor Summary
ConstructorDescriptionCreates a JSON mapper which will throwFacebookJsonMappingException
whenever an error occurs when mapping JSON data to Java objects. -
Method Summary
Modifier and TypeMethodDescriptionfacebookFieldNamesWithMultipleMappings
(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 theFacebook
annotation, figure out what the corresponding Facebook JSON field name to map to it is.protected void
Finds and invokes methods onobject
that are annotated with the@JsonMappingCompleted
annotation.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.void
setFacebookClient
(FacebookClient facebookClient) adds aFacebookClient
implementation to the mapper.<T> List<T>
toJavaList
(String json, Class<T> type) Given a JSON string, create and return a new instance of a corresponding JavaList
which contains elements of typetype
.<T> T
toJavaObject
(String json, Class<T> type) Given a JSON string, create and return a new instance of a corresponding Java object of typetype
.protected Object
toJavaType
(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation, JsonObject jsonObject, String facebookFieldName) Extracts JSON data for a field according to itsFacebook
annotation and returns it converted to the proper Java type.Given a Javaobject
, create and return a JSON string that represents it.Given a Javaobject
, create and return a JSON string that represents it.protected JsonValue
toJsonInternal
(Object object, boolean ignoreNullValuedProperties) Recursively marshal the givenobject
to JSON.protected <T> T
toPrimitiveJavaType
(String json, Class<T> type) Given ajson
value of something likeMyValue
or123
, return a representation of that value of typetype
.
-
Constructor Details
-
DefaultJsonMapper
public DefaultJsonMapper()Creates a JSON mapper which will throwFacebookJsonMappingException
whenever an error occurs when mapping JSON data to Java objects.
-
-
Method Details
-
setFacebookClient
Description copied from interface:JsonMapper
adds aFacebookClient
implementation to the mapper.This is especially useful for mapping json to Connection objects that need the client itself.
- Specified by:
setFacebookClient
in interfaceJsonMapper
- Parameters:
facebookClient
- that is used for Connection objects
-
toJavaList
Description copied from interface:JsonMapper
Given a JSON string, create and return a new instance of a corresponding JavaList
which contains elements of typetype
.The Java
type
must have a no-argument constructor.- Specified by:
toJavaList
in interfaceJsonMapper
- Type Parameters:
T
- Java type to map to for each element of the list.- Parameters:
json
- The JSON to be mapped to a Java type.type
- Java type token.- Returns:
- A Java object (of type
List
which contains elements of typetype
) representation of the JSON input.
-
toJavaObject
Description copied from interface:JsonMapper
Given a JSON string, create and return a new instance of a corresponding Java object of typetype
.The Java
type
must have a no-argument constructor.- Specified by:
toJavaObject
in interfaceJsonMapper
- Type Parameters:
T
- Java type to map to.- Parameters:
json
- The JSON to be mapped to a Java type.type
- Java type token.- Returns:
- A Java object (of type
type
) representation of the JSON input.
-
invokeJsonMappingCompletedMethods
protected void invokeJsonMappingCompletedMethods(Object object) throws IllegalAccessException, InvocationTargetException Finds and invokes methods onobject
that are annotated with the@JsonMappingCompleted
annotation.This will even work on
private
methods.- Parameters:
object
- The object on which to invoke the method.- Throws:
IllegalAccessException
- If unable to invoke the method.InvocationTargetException
- If unable to invoke the method.
-
logMultipleMappingFailedForField
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.- Parameters:
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.
-
getFacebookFieldName
protected String getFacebookFieldName(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation) For a Java field annotated with theFacebook
annotation, figure out what the corresponding Facebook JSON field name to map to it is.- Parameters:
fieldWithAnnotation
- A Java field annotated with theFacebook
annotation.- Returns:
- The Facebook JSON field name that should be mapped to this Java field.
-
facebookFieldNamesWithMultipleMappings
protected Set<String> facebookFieldNamesWithMultipleMappings(List<ReflectionUtils.FieldWithAnnotation<Facebook>> fieldsWithAnnotation) Finds any Facebook JSON fields that are mapped to more than 1 Java field.- Parameters:
fieldsWithAnnotation
- Java fields annotated with theFacebook
annotation.- Returns:
- Any Facebook JSON fields that are mapped to more than 1 Java field.
-
toJson
Description copied from interface:JsonMapper
Given a Javaobject
, create and return a JSON string that represents it.The
object
's properties will be traversed recursively, allowing for arbitrarily complex JSON generation.- Specified by:
toJson
in interfaceJsonMapper
- Parameters:
object
- The Java object to map to JSON. Can be a Javabean,List
, orMap
.- Returns:
- A JSON string.
-
toJson
Description copied from interface:JsonMapper
Given a Javaobject
, create and return a JSON string that represents it.The
object
's properties will be traversed recursively, allowing for arbitrarily complex JSON generation.- Specified by:
toJson
in interfaceJsonMapper
- Parameters:
object
- The Java object to map to JSON. Can be a Javabean,List
, orMap
.ignoreNullValuedProperties
- Iftrue
, no Javabean properties withnull
values will be included in the generated JSON.- Returns:
- A JSON string.
-
toJsonInternal
Recursively marshal the givenobject
to JSON.Used by
toJson(Object)
.- Parameters:
object
- The object to marshal.ignoreNullValuedProperties
- If this istrue
, no Javabean properties withnull
values will be included in the generated JSON.- Returns:
- JSON representation of the given
object
. - Throws:
FacebookJsonMappingException
- If an error occurs while marshaling to JSON.
-
toPrimitiveJavaType
Given ajson
value of something likeMyValue
or123
, return a representation of that value of typetype
.This is to support non-legal JSON served up by Facebook for API calls like
Friends.get
(example result:[222333,1240079]
).- Type Parameters:
T
- The Java type to map to.- Parameters:
json
- The non-legal JSON to map to the Java type.type
- Type token.- Returns:
- Java representation of
json
. - Throws:
FacebookJsonMappingException
- If an error occurs while mapping JSON to Java.
-
toJavaType
protected Object toJavaType(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation, JsonObject jsonObject, String facebookFieldName) Extracts JSON data for a field according to itsFacebook
annotation and returns it converted to the proper Java type.- Parameters:
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.- Returns:
- A
- Throws:
ParseException
- If an error occurs while mapping JSON to Java.FacebookJsonMappingException
- If an error occurs while mapping JSON to Java.
-