Class ObjectToJsonConverter

java.lang.Object
org.jolokia.service.serializer.json.ObjectToJsonConverter

public final class ObjectToJsonConverter extends Object

A converter/serializer that transforms Java objects (JMX attribute values, JMX operation parameters and return values) into a JSON representation. It delegates handling of supported types to ObjectAccessor implementations.

Each ObjectAccessor gets a reference to this converter object so it can use it for a recursive serialization of nested objects.

Additionally (since the dawn of Jolokia) this converter is used to set inner values of any objects. Also by delegating to extractors which can handle particular object types. For example DateAccessor can call Date.setTime(long).

Since:
Apr 19, 2009
Author:
roland
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Set<Class<?>>
    Classes of the objects that can be returned directly as proper JSON types and can't be used with extra "path" do return part of the value.
    static final Map<Class<?>,String>
    Simple types which are not proper JSON types (even if they seem to) but can be easily converted.
    static final Set<Class<?>>
    Classes of the objects that can be returned directly as proper JSON types.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ObjectToJsonConverter(ObjectToObjectConverter pObjectToObjectConverter, ObjectToOpenTypeConverter pObjectToOpenTypeConverter, org.jolokia.server.core.service.api.JolokiaContext pContext)
    Create a converter that can serialize objects into JSON representation
  • Method Summary

    Modifier and Type
    Method
    Description
    extractObject(Object pValue, Deque<String> pPathParts, boolean pJsonify)
    Related to extractObjectWithContext(java.lang.Object, java.util.Deque<java.lang.String>, org.jolokia.server.core.service.serializer.SerializeOptions, boolean) except that it does not set up a context.
    org.jolokia.server.core.service.serializer.ValueFaultHandler
    Get the fault handler used for dealing with exceptions during value extraction.
    serialize(Object pValue, List<String> pPathParts, org.jolokia.server.core.service.serializer.SerializeOptions pOptions)
    Convert the return value to a JSON object.
    setInnerValue(Object pOuterObject, Object pNewValue, List<String> pPathParts)
    Set an inner value of a complex object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • JSON_TYPES

      public static final Set<Class<?>> JSON_TYPES
      Classes of the objects that can be returned directly as proper JSON types. All other objects need to be converted in some way to proper JSON objects.
    • JSON_BASIC_TYPES

      public static final Set<Class<?>> JSON_BASIC_TYPES
      Classes of the objects that can be returned directly as proper JSON types and can't be used with extra "path" do return part of the value.
    • JSON_CONVERSIONS

      public static final Map<Class<?>,String> JSON_CONVERSIONS
      Simple types which are not proper JSON types (even if they seem to) but can be easily converted.
  • Constructor Details

    • ObjectToJsonConverter

      public ObjectToJsonConverter(ObjectToObjectConverter pObjectToObjectConverter, ObjectToOpenTypeConverter pObjectToOpenTypeConverter, org.jolokia.server.core.service.api.JolokiaContext pContext)
      Create a converter that can serialize objects into JSON representation
      Parameters:
      pObjectToObjectConverter - used when setting values
      pObjectToOpenTypeConverter - used when converting TabularData
      pContext - JolokiaContext used to load ObjectAccessor services
  • Method Details

    • serialize

      public Object serialize(Object pValue, List<String> pPathParts, org.jolokia.server.core.service.serializer.SerializeOptions pOptions) throws AttributeNotFoundException
      Convert the return value to a JSON object.
      Parameters:
      pValue - the value to convert
      pPathParts - path parts to use for extraction
      pOptions - options used for parsing
      Returns:
      the converter object. This either a subclass of JSONStructure or a basic data type like String or Long.
      Throws:
      AttributeNotFoundException - if within a path an attribute could not be found
    • setInnerValue

      public Object setInnerValue(Object pOuterObject, Object pNewValue, List<String> pPathParts) throws AttributeNotFoundException, IllegalAccessException, InvocationTargetException
      Set an inner value of a complex object. A given path must point to the attribute/index to set within the outer object.
      Parameters:
      pOuterObject - the object to dive in
      pNewValue - the value to set
      pPathParts - the path within the outer object. This object will be modified and must be a modifiable list.
      Returns:
      the old value
      Throws:
      AttributeNotFoundException
      IllegalAccessException
      InvocationTargetException
    • extractObject

      public Object extractObject(Object pValue, Deque<String> pPathParts, boolean pJsonify) throws AttributeNotFoundException
      Related to extractObjectWithContext(java.lang.Object, java.util.Deque<java.lang.String>, org.jolokia.server.core.service.serializer.SerializeOptions, boolean) except that it does not set up a context. This method is called back from the various extractors to recursively continue the extraction, hence it is public.

      This method must not be used as entry point for serialization. Use serialize(Object, List, SerializeOptions) or setInnerValue(Object, Object, List) instead.

      Parameters:
      pValue - value to extract from
      pPathParts - stack for diving into the object
      pJsonify - whether a JSON representation JSONObject
      Returns:
      extracted object either in native format or as JSONObject
      Throws:
      AttributeNotFoundException - if an attribute is not found during traversal
    • getValueFaultHandler

      public org.jolokia.server.core.service.serializer.ValueFaultHandler getValueFaultHandler()
      Get the fault handler used for dealing with exceptions during value extraction.
      Returns:
      the fault handler