Class JsonMarshaller

All Implemented Interfaces:
Serializable
Direct Known Subclasses:
IdentificationParametersMarshaller, PrideMarshaller, PTMFactoryMarshaller

public class JsonMarshaller extends ExperimentObject
This class converts non-specific objects to the JSON format and vice versa.
Author:
Kenneth Verheggen, Marc Vaudel, Harald Barsnes
See Also:
  • Field Details

    • gson

      protected com.google.gson.Gson gson
      GSON parser instance to convert JSON to Java objects and back.
    • builder

      protected final com.google.gson.GsonBuilder builder
      GsonBuilder that can be used to append interfaces so the parser knows how to handle them.
  • Constructor Details

    • JsonMarshaller

      public JsonMarshaller()
      Default constructor.
    • JsonMarshaller

      public JsonMarshaller(Class... interfaces)
      Constructor.
      Parameters:
      interfaces - comma separated list of interfaces that this class is using. They will automatically be added using a custom InterfaceAdapter.
  • Method Details

    • init

      protected void init()
      Initializes the marshaller with (custom) type adapters.
    • toJson

      public String toJson(Object anObject)
      Convert an object to JSON.
      Parameters:
      anObject - the input object
      Returns:
      the JSON representation of an object
    • saveObjectToJson

      public void saveObjectToJson(Object anObject, File jsonFile) throws IOException
      Save an object to JSON.
      Parameters:
      anObject - the input object
      jsonFile - the target file to which the JSON will be saved
      Throws:
      IOException - if the object cannot be successfully saved into a JSON file
    • fromJson

      public Object fromJson(Class objectType, String jsonString)
      Convert from JSON to object.
      Parameters:
      objectType - the class the object belongs to
      jsonString - the string representation of the JSON object
      Returns:
      an instance of the objectType containing the JSON information
    • fromJson

      public Object fromJson(Class objectType, File jsonFile) throws IOException
      Convert from JSON to object.
      Parameters:
      objectType - the class the object belongs to
      jsonFile - a JSON file
      Returns:
      an instance of the objectType containing the JSON information
      Throws:
      IOException - if the object cannot be successfully read from a JSON file
    • fromJson

      public Object fromJson(Class objectType, URL jsonURL) throws IOException
      Convert from JSON to object.
      Parameters:
      objectType - the class the object belongs to
      jsonURL - a JSON URL
      Returns:
      an instance of the objectType containing the JSON information
      Throws:
      IOException - if the object cannot be successfully read from a JSON file
    • fromJson

      public Object fromJson(Type objectType, String jsonString)
      Convert from JSON to object.
      Parameters:
      objectType - the typetoken the object belongs to
      jsonString - the string representation of the JSON object
      Returns:
      an instance of the objectType containing the JSON information
    • fromJson

      public Object fromJson(Type objectType, File jsonFile) throws IOException
      Convert from JSON to object.
      Parameters:
      objectType - the typetoken the object belongs to
      jsonFile - a JSON file
      Returns:
      an instance of the objectType containing the JSON information
      Throws:
      IOException - if the object cannot be successfully read from a JSON file
    • fromJson

      public Object fromJson(Type objectType, URL jsonURL) throws IOException
      Convert from JSON to object.
      Parameters:
      objectType - the typetoken the object belongs to
      jsonURL - a JSON URL
      Returns:
      an instance of the objectType containing the JSON information
      Throws:
      IOException - if the object cannot be successfully read from a JSON file
    • getJsonStringFromFile

      protected String getJsonStringFromFile(File jsonFile) throws FileNotFoundException, IOException
      Convert JSON string from file.
      Parameters:
      jsonFile - the input JSON file
      Returns:
      the string representation of the JSON content
      Throws:
      FileNotFoundException - if the JSON file can not be reached
      IOException - if the object cannot be successfully read from a JSON file