Class JSONFormatterPdxToJsonConverter

java.lang.Object
org.springframework.geode.data.json.converter.support.JacksonObjectToJsonConverter
org.springframework.geode.data.json.converter.support.JSONFormatterPdxToJsonConverter
All Implemented Interfaces:
org.springframework.core.convert.converter.Converter<Object,String>, ObjectToJsonConverter

public class JSONFormatterPdxToJsonConverter extends JacksonObjectToJsonConverter
An ObjectToJsonConverter implementation using the Apache Geode JSONFormatter to convert from a PdxInstance to a JSON String.
Since:
1.3.0
See Also:
  • Constructor Details

    • JSONFormatterPdxToJsonConverter

      public JSONFormatterPdxToJsonConverter()
  • Method Details

    • convert

      @Nullable public final String convert(@Nullable Object source)
      Description copied from class: JacksonObjectToJsonConverter
      Converts the given Object into JSON.
      Specified by:
      convert in interface org.springframework.core.convert.converter.Converter<Object,String>
      Overrides:
      convert in class JacksonObjectToJsonConverter
      Parameters:
      source - Object to convert into JSON.
      Returns:
      JSON generated from the given Object using Jackson's ObjectMapper.
      See Also:
    • convertPojoToJson

      @Nullable protected String convertPojoToJson(Object source)
      Converts the given Object to JSON.
      Parameters:
      source - Object to convert to JSON.
      Returns:
      the JSON generated from the given Object.
      See Also:
    • convertPdxToJson

      @NonNull protected String convertPdxToJson(@NonNull org.apache.geode.pdx.PdxInstance pdxInstance)
      Converts the given PDX to JSON.
      Parameters:
      pdxInstance - PdxInstance to convert to JSON; must not be null.
      Returns:
      JSON generated from the given PdxInstance.
      See Also:
      • JSONFormatter.toJSON(PdxInstance)
      • PdxInstance
      • jsonFormatterToJson(PdxInstance)
    • decorate

      @NonNull protected org.apache.geode.pdx.PdxInstance decorate(@NonNull org.apache.geode.pdx.PdxInstance pdxInstance)
      WARNING!!! First, this method might be less than optimal and could lead to PDX type explosion! Second, this pdxInstance.createWriter().setField(AT_TYPE_METADATA_PROPERTY_NAME, className); ... Throws: org.apache.geode.pdx.PdxFieldDoesNotExistException: A field named @type does not exist on ... PdxType[dsid=0,typenum=7232261,name=example.app.crm.model.Customer,fields=[id:long:identity:0:idx0(relativeOffset)=0:idx1(vlfOffsetIndex)=0, name:String:1:idx0(relativeOffset)=8:idx1(vlfOffsetIndex)=-1,]] at org.apache.geode.pdx.internal.WritablePdxInstanceImpl.setField(WritablePdxInstanceImpl.java:119) ... This code needs to create a new PdxInstance from an existing PdxInstance or add the new (PDX) field to the PDX type metadata using PdxType.addField(:PdxField) before setting the new field on the PdxInstance using the WritablePdxInstance. Unfortunately, PdxType is part of the internal API and updating and ditributing a PdxType is complicated, requiring a Distributed Lock, among other responsibilities.
    • decorate

      @NonNull protected String decorate(@NonNull org.apache.geode.pdx.PdxInstance pdxInstance, @NonNull String json)
      Decorates the given JSON to include the @type metadata property in order to indicate the type of the JSON object, which is required for deserialization back to PDX. If an JsonProcessingException is thrown during this operation and if the PdxInstance has a valid class name, then an attempt is made to serialize the object instance of the PdxInstance to JSON using Jackson's ObjectMapper.
      Parameters:
      pdxInstance - required PdxInstance from which the JSON was serialized; must not be null.
      json - JSON generated from the serialization of the PdxInstance; must not be null.
      Returns:
      the decorated JSON including the @type metadata property.
      Throws:
      org.springframework.dao.DataRetrievalFailureException - if JSON cannot be decorated with type metadata and the PdxInstance is not based on a valid Class type.
      See Also: