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
ConstructorsConstructorDescriptionCreates a JSON mapper which will throwFacebookJsonMappingExceptionwhenever 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 StringgetFacebookFieldName(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation) For a Java field annotated with theFacebookannotation, figure out what the corresponding Facebook JSON field name to map to it is.protected voidFinds and invokes methods onobjectthat are annotated with the@JsonMappingCompletedannotation.protected voidlogMultipleMappingFailedForField(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.voidsetFacebookClient(FacebookClient facebookClient) adds aFacebookClientimplementation 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 JavaListwhich contains elements of typetype.<T> TtoJavaObject(String json, Class<T> type) Given a JSON string, create and return a new instance of a corresponding Java object of typetype.protected ObjecttoJavaType(ReflectionUtils.FieldWithAnnotation<Facebook> fieldWithAnnotation, JsonObject jsonObject, String facebookFieldName) Extracts JSON data for a field according to itsFacebookannotation 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 JsonValuetoJsonInternal(Object object, boolean ignoreNullValuedProperties) Recursively marshal the givenobjectto JSON.protected <T> TtoPrimitiveJavaType(String json, Class<T> type) Given ajsonvalue of something likeMyValueor123, return a representation of that value of typetype.
-
Constructor Details
-
DefaultJsonMapper
public DefaultJsonMapper()Creates a JSON mapper which will throwFacebookJsonMappingExceptionwhenever an error occurs when mapping JSON data to Java objects.
-
-
Method Details
-
setFacebookClient
Description copied from interface:JsonMapperadds aFacebookClientimplementation to the mapper.This is especially useful for mapping json to Connection objects that need the client itself.
- Specified by:
setFacebookClientin interfaceJsonMapper- Parameters:
facebookClient- that is used for Connection objects
-
toJavaList
Description copied from interface:JsonMapperGiven a JSON string, create and return a new instance of a corresponding JavaListwhich contains elements of typetype.The Java
typemust have a no-argument constructor.- Specified by:
toJavaListin 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
Listwhich contains elements of typetype) representation of the JSON input.
-
toJavaObject
Description copied from interface:JsonMapperGiven a JSON string, create and return a new instance of a corresponding Java object of typetype.The Java
typemust have a no-argument constructor.- Specified by:
toJavaObjectin 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 onobjectthat are annotated with the@JsonMappingCompletedannotation.This will even work on
privatemethods.- 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 theFacebookannotation, figure out what the corresponding Facebook JSON field name to map to it is.- Parameters:
fieldWithAnnotation- A Java field annotated with theFacebookannotation.- 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 theFacebookannotation.- Returns:
- Any Facebook JSON fields that are mapped to more than 1 Java field.
-
toJson
Description copied from interface:JsonMapperGiven 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:
toJsonin 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:JsonMapperGiven 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:
toJsonin interfaceJsonMapper- Parameters:
object- The Java object to map to JSON. Can be a Javabean,List, orMap.ignoreNullValuedProperties- Iftrue, no Javabean properties withnullvalues will be included in the generated JSON.- Returns:
- A JSON string.
-
toJsonInternal
Recursively marshal the givenobjectto JSON.Used by
toJson(Object).- Parameters:
object- The object to marshal.ignoreNullValuedProperties- If this istrue, no Javabean properties withnullvalues 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 ajsonvalue of something likeMyValueor123, 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 itsFacebookannotation 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.
-