Class jSonUtils

java.lang.Object
org.jspace.io.json.jSonUtils

public class jSonUtils
extends java.lang.Object
  • Field Summary

    Fields
    Modifier and Type Field Description
    static java.lang.String FORMAL_ID
    Json tag indicating if a template field is a formal or an actual.
    static java.lang.String TYPE_ID
    Json tag associated to string referring to Class names.
    static java.lang.String VALUE_ID
    Json tag associated to generic objects.
  • Method Summary

    Modifier and Type Method Description
    <T> T fromByte​(java.lang.Class<T> clazz, byte[] data)  
    <T> T fromString​(java.lang.Class<T> clazz, java.lang.String message)  
    com.google.gson.Gson getGson()  
    static jSonUtils getInstance()  
    com.google.gson.JsonElement jsonFromObject​(java.lang.Object o, com.google.gson.JsonSerializationContext context)
    Serialize an object into a JsonElement.
    com.google.gson.JsonElement jsonFromTeplate​(TemplateField field, com.google.gson.JsonSerializationContext context)  
    java.lang.Object objectFromJson​(com.google.gson.JsonElement json, com.google.gson.JsonDeserializationContext context)
    Deserialize an object from a JsonElement.
    <T> T read​(java.io.BufferedReader r, java.lang.Class<T> clazz)  
    void register​(java.lang.String uri, java.lang.Class<?> clazz)  
    <T> void register​(java.lang.String uri, java.lang.Class<T> clazz, com.google.gson.JsonSerializer<T> serializer, com.google.gson.JsonDeserializer<T> deserializer)  
    TemplateField templateFromJSon​(com.google.gson.JsonElement json, com.google.gson.JsonDeserializationContext context)  
    byte[] toByte​(java.lang.Object o)  
    java.lang.String toString​(java.lang.Object o)  
    void write​(java.io.PrintWriter w, java.lang.Object o)  

    Methods inherited from class java.lang.Object

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

    • FORMAL_ID

      public static final java.lang.String FORMAL_ID
      Json tag indicating if a template field is a formal or an actual.
      See Also:
      Constant Field Values
    • TYPE_ID

      public static final java.lang.String TYPE_ID
      Json tag associated to string referring to Class names.
      See Also:
      Constant Field Values
    • VALUE_ID

      public static final java.lang.String VALUE_ID
      Json tag associated to generic objects. The type of the specific object is determined by relying on attribute TYPE_ID
      See Also:
      Constant Field Values
  • Method Details

    • getInstance

      public static jSonUtils getInstance()
    • toString

      public java.lang.String toString​(java.lang.Object o)
    • toByte

      public byte[] toByte​(java.lang.Object o)
    • fromByte

      public <T> T fromByte​(java.lang.Class<T> clazz, byte[] data)
    • fromString

      public <T> T fromString​(java.lang.Class<T> clazz, java.lang.String message)
    • write

      public void write​(java.io.PrintWriter w, java.lang.Object o)
    • read

      public <T> T read​(java.io.BufferedReader r, java.lang.Class<T> clazz) throws java.io.IOException
      Throws:
      java.io.IOException
    • jsonFromObject

      public com.google.gson.JsonElement jsonFromObject​(java.lang.Object o, com.google.gson.JsonSerializationContext context)
      Serialize an object into a JsonElement. The object is rendered as a JsonObject containing two attributes:
      • type, containing a string with the fully qualified name of the serialized object;
      • value, containing the JsonElement associated to the serialized object.
      When the object will be deserialized, the first attribute will be used to identify the object class, while the second one will be used to retrieve object status.
      Parameters:
      o - object to serialize
      context - Context for serialization
      Returns:
      a json representation of o
    • objectFromJson

      public java.lang.Object objectFromJson​(com.google.gson.JsonElement json, com.google.gson.JsonDeserializationContext context)
      Deserialize an object from a JsonElement. We assume that the received JsonElement is a JsonObject providing two attributes:
      • type, containing a string with the fully qualified name of the serialized object;
      • value, containing the JsonElement associated to the serialized object.
      Parameters:
      json - element to deserialize
      context - context for serialization
      Returns:
      the object represented by json
    • jsonFromTeplate

      public com.google.gson.JsonElement jsonFromTeplate​(TemplateField field, com.google.gson.JsonSerializationContext context)
    • templateFromJSon

      public TemplateField templateFromJSon​(com.google.gson.JsonElement json, com.google.gson.JsonDeserializationContext context)
    • register

      public void register​(java.lang.String uri, java.lang.Class<?> clazz)
    • register

      public <T> void register​(java.lang.String uri, java.lang.Class<T> clazz, com.google.gson.JsonSerializer<T> serializer, com.google.gson.JsonDeserializer<T> deserializer)
    • getGson

      public com.google.gson.Gson getGson()