Class SimpleJsonUtils

java.lang.Object
org.apache.derby.optional.api.SimpleJsonUtils

public abstract class SimpleJsonUtils extends Object

Utility methods for simple JSON support.

  • Constructor Details

    • SimpleJsonUtils

      public SimpleJsonUtils()
  • Method Details

    • toJSON

      public static org.json.simple.JSONArray toJSON(ResultSet rs) throws SQLException

      Pack a ResultSet into a JSONArray. This method could be called client-side on any query result from any DBMS. Each row is converted into a JSONObject whose keys are the corresponding column names from the ResultSet. Closes the ResultSet once it has been drained. Datatypes map to JSON values as follows:

      • NULL - The JSON null literal.
      • SMALLINT, INT, BIGINT - JSON integer values.
      • DOUBLE, FLOAT, REAL, DECIMAL, NUMERIC - JSON floating point values.
      • CHAR, VARCHAR, LONG VARCHAR, CLOB - JSON string values.
      • BLOB, VARCHAR FOR BIT DATA, LONG VARCHAR FOR BIT DATA - The byte array is turned into a hex string (2 hex digits per byte) and the result is returned as a JSON string.
      • All other types - Converted to JSON string values via their toString() methods.
      Parameters:
      rs - A ResultSet to turn into a JSON array
      Returns:
      a json array
      Throws:
      SQLException - on error
    • readArray

      public static org.json.simple.JSONArray readArray(Reader reader) throws SQLException
      Construct a JSONArray from a Reader.
      Parameters:
      reader - A Reader from which to de-serialize a json array
      Returns:
      a json array
      Throws:
      SQLException - on error
    • readArrayFromString

      public static org.json.simple.JSONArray readArrayFromString(String document) throws SQLException

      SQL FUNCTION to convert a JSON document string into a JSONArray. This function is registered by the simpleJson optional tool.

      Parameters:
      document - A json array as a string
      Returns:
      a json array
      Throws:
      SQLException - on error
    • readArrayFromStream

      public static org.json.simple.JSONArray readArrayFromStream(InputStream inputStream, String characterSetName) throws SQLException
      Read a JSONArray from an InputStream. Close the stream after reading the JSONArray.
      Parameters:
      inputStream - A stream to read from
      characterSetName - The character set of the stream
      Returns:
      a json array
      Throws:
      SQLException - on error
    • readArrayFromFile

      public static org.json.simple.JSONArray readArrayFromFile(String fileName, String characterSetName) throws SQLException
      SQL FUNCTION to read a JSONArray from a File. This function is registered by the simpleJson optional tool.
      Parameters:
      fileName - A file to read
      characterSetName - The character encoding of the file
      Returns:
      a json array
      Throws:
      SQLException - on error
    • readArrayFromURL

      public static org.json.simple.JSONArray readArrayFromURL(String urlString, String characterSetName) throws SQLException
      SQL FUNCTION to read a JSONArray from an URL address. This function is registered by the simpleJson optional tool.
      Parameters:
      urlString - An url pointing to a json array
      characterSetName - The character encoding of the resource identified by the URL
      Returns:
      a json array
      Throws:
      SQLException - on error
    • arrayToClob

      public static Clob arrayToClob(org.json.simple.JSONArray array)
      SQL FUNCTION to convert a JSONArray into a CLOB.
      Parameters:
      array - A json array
      Returns:
      a Clob containing the array as a string