Package com.restfb

Class DefaultJsonMapper

java.lang.Object
com.restfb.DefaultJsonMapper
All Implemented Interfaces:
JsonMapper

public class DefaultJsonMapper extends Object implements JsonMapper
Default implementation of a JSON-to-Java mapper.
Author:
Mark Allen
  • Constructor Details

  • Method Details

    • setFacebookClient

      public void setFacebookClient(FacebookClient facebookClient)
      Description copied from interface: JsonMapper
      adds a FacebookClient implementation to the mapper. This is especially useful for mapping json to Connection objects that need the client itself.
      Specified by:
      setFacebookClient in interface JsonMapper
      Parameters:
      facebookClient - that is used for Connection objects
    • toJavaList

      public <T> List<T> toJavaList(String json, Class<T> type)
      Description copied from interface: JsonMapper
      Given a JSON string, create and return a new instance of a corresponding Java List which contains elements of type type.

      The Java type must have a no-argument constructor.

      Specified by:
      toJavaList in interface JsonMapper
      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 type type) representation of the JSON input.
    • toJavaObject

      public <T> T toJavaObject(String json, Class<T> type)
      Description copied from interface: JsonMapper
      Given a JSON string, create and return a new instance of a corresponding Java object of type type.

      The Java type must have a no-argument constructor.

      Specified by:
      toJavaObject in interface JsonMapper
      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

      Finds and invokes methods on object 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

      For a Java field annotated with the Facebook annotation, figure out what the corresponding Facebook JSON field name to map to it is.
      Parameters:
      fieldWithAnnotation - A Java field annotated with the Facebook annotation.
      Returns:
      The Facebook JSON field name that should be mapped to this Java field.
    • facebookFieldNamesWithMultipleMappings

      Finds any Facebook JSON fields that are mapped to more than 1 Java field.
      Parameters:
      fieldsWithAnnotation - Java fields annotated with the Facebook annotation.
      Returns:
      Any Facebook JSON fields that are mapped to more than 1 Java field.
    • toJson

      public String toJson(Object object)
      Description copied from interface: JsonMapper
      Given a Java object, 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 interface JsonMapper
      Parameters:
      object - The Java object to map to JSON. Can be a Javabean, List, or Map.
      Returns:
      A JSON string.
    • toJson

      public String toJson(Object object, boolean ignoreNullValuedProperties)
      Description copied from interface: JsonMapper
      Given a Java object, 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 interface JsonMapper
      Parameters:
      object - The Java object to map to JSON. Can be a Javabean, List, or Map.
      ignoreNullValuedProperties - If true, no Javabean properties with null values will be included in the generated JSON.
      Returns:
      A JSON string.
    • toJsonInternal

      protected JsonValue toJsonInternal(Object object, boolean ignoreNullValuedProperties)
      Recursively marshal the given object to JSON.

      Used by toJson(Object).

      Parameters:
      object - The object to marshal.
      ignoreNullValuedProperties - If this is true, no Javabean properties with null 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

      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.

      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 its Facebook 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.