Module jonix.json

Class JonixJson

java.lang.Object
com.tectonica.jonix.json.JonixJson

public class JonixJson extends Object
Helper class for JSON serialization service of Jonix objects.

The fundamental APIs are the generic objectToJson(Object) and the very-specific productToJson(OnixProduct).

  • Constructor Details

    • JonixJson

      public JonixJson()
  • Method Details

    • toJson

      public static String toJson(Object object, boolean indent)
      Creates a JSON representation of an object using either its public fields or its OnixProduct accessors, depending on its actual type.

      See productToJson(OnixProduct, boolean) and objectToJson(Object) for more details.

      Parameters:
      object - object to serialize as JSON using its public fields or its OnixProduct accessors
      indent - whether the returned JSON string should be indented (OnixProduct only)
      Returns:
      JSON representation of the object
    • toJson

      public static String toJson(Object object, boolean indent, boolean sortSets)
      Creates a JSON representation of an object using either its public fields or its OnixProduct accessors, depending on its actual type.

      See productToJson(OnixProduct, boolean, boolean) and objectToJson(Object) for more details.

      Parameters:
      object - object to serialize as JSON using its public fields or its OnixProduct accessors
      indent - whether the returned JSON string should be indented (OnixProduct only)
      sortSets - whether the returned JSON string should have its sets sorted (OnixProduct only)
      Returns:
      JSON representation of the object
    • toJson

      public static String toJson(Object object)
      Parameters:
      object - object to serialize as JSON using its public fields or its OnixProduct accessors
      Returns:
      indented JSON representation of the object
      See Also:
    • productToJson

      public static String productToJson(OnixProduct onixProduct, boolean indent)
      Creates a JSON representation of an OnixProduct object (i.e. low-level xml-parsed ONIX Product record).

      This JSON serializer is needed in addition to the more generic objectToJson(Object) because OnixProducts are populated "lazily". In other words, their internal fields get populated with actual values only when accessed explicitly (via accessor methods, that induce the population of the OnixComposite they refer to). This is a design choice to make Jonix more performant and less memory hungry. However, for the underlying JSON serialization framework (Jackson is this case), this laziness presents a problem, as no accessors are explicitly invoked, and nothing triggers population of the (private) fields that are accessed directly during serialization. Therefore, some advanced configuration of the Jackson serializer is required, which is exactly what this API does.

      Parameters:
      onixProduct - an OnixProduct object to serialize as JSON
      indent - whether the returned JSON should be indented
      Returns:
      JSON representation of the product
    • productToJson

      public static String productToJson(OnixProduct onixProduct, boolean indent, boolean sortSets)
      Creates a JSON representation of an OnixProduct object (i.e. low-level xml-parsed ONIX Product record).

      This JSON serializer is needed in addition to the more generic objectToJson(Object) because OnixProducts are populated "lazily". In other words, their internal fields get populated with actual values only when accessed explicitly (via accessor methods, that induce the population of the OnixComposite they refer to). This is a design choice to make Jonix more performant and less memory hungry. However, for the underlying JSON serialization framework (Jackson is this case), this laziness presents a problem, as no accessors are explicitly invoked, and nothing triggers population of the (private) fields that are accessed directly during serialization. Therefore, some advanced configuration of the Jackson serializer is required, which is exactly what this API does.

      Parameters:
      onixProduct - an OnixProduct object to serialize as JSON
      indent - whether the returned JSON should be indented
      sortSets - whether the returned JSON should have its sets sorted
      Returns:
      JSON representation of the product
    • productToJson

      public static String productToJson(OnixProduct onixProduct)
      Parameters:
      onixProduct - an OnixProduct object to serialize as JSON
      Returns:
      indented JSON representation of the product
      See Also:
    • objectToJson

      public static String objectToJson(Object object)
      Creates a JSON representation of an object using its public fields (i.e. NOT its getters or similar), therefore is suitable for the BaseProduct family.
      Parameters:
      object - Any Java object whose serializable values are stored in public fields
      Returns:
      JSON representation of the object