Class MarshallerService

java.lang.Object
org.eclipse.dawnsci.json.MarshallerService
All Implemented Interfaces:
IMarshallerService

public class MarshallerService extends Object implements IMarshallerService
JSON marshaller implementation which allows objects to be converted to and from JSON strings

This implementation adds type information to encoded JSON strings and will use it when deserializing.

Type information is typically obtained from the attribute definition of the class in which it is defined, or the class passed to the marshal(Object anyObject, boolean useRegisteredClassTyping).

This is not useful when working with interface, abstract base class or Object definitions, as it could be a number of different classes. In these situations, the key "@type" is used to indicate a string for class identification.

  • Constructor Details

    • MarshallerService

      public MarshallerService()
      Default public constructor - for testing purposes only! Otherwise use OSGi to get the service.
    • MarshallerService

      public MarshallerService(IClassRegistry... extraRegistries)
      Constructor for testing to allow IClassRegistry('s) to be injected.
      Parameters:
      extraRegistries -
    • MarshallerService

      public MarshallerService(IMarshaller... marshallers)
      Constructor for testing to allow IMarshaller(s) to be injected.
      Parameters:
      marshallers -
    • MarshallerService

      public MarshallerService(List<IClassRegistry> extraRegistries, List<IMarshaller> marshallers)
      Constructor for testing to allow IMarshaller(s) or IClassRegistry('s) to be injected.
      Parameters:
      extraRegistries -
      marshallers -
  • Method Details

    • marshal

      public String marshal(Object anyObject) throws Exception
      Serialize the given object to a JSON string

      Objects to be marshalled with this implementation should have no-arg constructors, and getters and setters for their fields. Primitive and collection types (arrays, Collections and Maps) should work correctly. More complicated types (generics other than collections, inner classes etc) might or might not work properly.

      Specified by:
      marshal in interface IMarshallerService
      Parameters:
      anyObject - the object to be serialized
      Returns:
      the JSON string representing the object
      Throws:
      Exception - if the object cannot be marshalled correctly
    • marshal

      public String marshal(Object anyObject, boolean useRegisteredClassTyping) throws Exception
      Description copied from interface: IMarshallerService
      This marshal gives the option of turning off the class registry id information in the serialization. If you are not using interfaces (the norm) then this information is not needed because the types are static.
      Specified by:
      marshal in interface IMarshallerService
      Parameters:
      anyObject -
      useRegisteredClassTyping -
      Returns:
      the JSON string representing the object
      Throws:
      Exception
    • unmarshal

      public <U> U unmarshal(String string, Class<U> beanClass) throws Exception
      Deserialize the given JSON string as an instance of the given class

      This method will try to find the correct classes for deserialization from the class id, if type not given.

      Specified by:
      unmarshal in interface IMarshallerService
      Parameters:
      string - the JSON string to be deserialized
      beanClass - the expected class of the deserialized object
      Returns:
      the deserialized object
      Throws:
      Exception - if the object cannot be unmarshalled correctly
    • isObjMixInSupported

      public boolean isObjMixInSupported(Object roi)
      Description copied from interface: IMarshallerService
      Returns true if this Object has a mix-in class (for ROIs for instance)
      Specified by:
      isObjMixInSupported in interface IMarshallerService
      Parameters:
      roi -
      Returns:
      boolean