Class JSONUtils


  • public class JSONUtils
    extends java.lang.Object
    Collection of JSON Utility methods. This class is immutable.
    Author:
    Roy Wetherall, Kevin Roast
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONUtils()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object encodeJSONString​(java.lang.Object value)
      Encodes a JSON string value
      org.json.simple.JSONObject toJSONObject​(java.lang.Object object)
      Converts the given JavaScript native object to a org.json.simple.JSONObject Java Object.
      java.lang.String toJSONString​(java.lang.Object object)
      Converts a given JavaScript native object and converts it to the relevant JSON string.
      org.mozilla.javascript.ScriptableObject toObject​(java.lang.String jsonString)
      Takes a JSON string and converts it to a native java script object or array
      org.mozilla.javascript.NativeArray toObject​(org.json.simple.JSONArray jsonArray)
      Takes a JSON array and converts it to a native JS array.
      org.mozilla.javascript.NativeObject toObject​(org.json.simple.JSONObject jsonObject)
      Takes a JSON object and converts it to a native JS object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JSONUtils

        public JSONUtils()
    • Method Detail

      • toJSONString

        public java.lang.String toJSONString​(java.lang.Object object)
                                      throws java.io.IOException
        Converts a given JavaScript native object and converts it to the relevant JSON string.
        Parameters:
        object - JavaScript object
        Returns:
        String JSON
        Throws:
        java.io.IOException
      • toJSONObject

        public org.json.simple.JSONObject toJSONObject​(java.lang.Object object)
                                                throws java.io.IOException
        Converts the given JavaScript native object to a org.json.simple.JSONObject Java Object. This is a specialized method only used by routines that will later expect a JSONObject.
        Parameters:
        object - JavaScript native object
        Returns:
        JSONObject
        Throws:
        java.io.IOException
      • toObject

        public org.mozilla.javascript.ScriptableObject toObject​(java.lang.String jsonString)
        Takes a JSON string and converts it to a native java script object or array
        Parameters:
        jsonString - a valid json string
        Returns:
        NativeObject the created native JS object that represents the JSON object or array
      • toObject

        public org.mozilla.javascript.NativeObject toObject​(org.json.simple.JSONObject jsonObject)
        Takes a JSON object and converts it to a native JS object.
        Parameters:
        jsonObject - the json object
        Returns:
        NativeObject the created native object
      • toObject

        public org.mozilla.javascript.NativeArray toObject​(org.json.simple.JSONArray jsonArray)
        Takes a JSON array and converts it to a native JS array.
        Parameters:
        jsonArray - the json array
        Returns:
        NativeObject the created native array
      • encodeJSONString

        public java.lang.Object encodeJSONString​(java.lang.Object value)
        Encodes a JSON string value
        Parameters:
        value - value to encode
        Returns:
        String encoded value