Class JsonType


public class JsonType extends AbstractDataType
Adapter to handle conversion between PostgreSQL native json/ jsonb types and Strings.

PostgreSQL requires a bound parameter's declared type to match its target json/jsonb column exactly - it has no implicit cast between the two - so one instance handles only the single sql type name it was constructed with. PostgresqlDataTypeFactory constructs the matching instance for each column automatically; there is normally no need to instantiate this class directly.

This class only shuttles the column's raw text between the driver and the dataset; it does not compare JSON values semantically. Use IsActualEqualToExpectedJsonValueComparer to compare json/jsonb column values by their parsed document structure instead of raw text.

Since:
3.5.0
Author:
Jeff Jensen
  • Constructor Details

    • JsonType

      public JsonType(String sqlTypeName)
      Creates a data type adapter for the given PostgreSQL JSON sql type name.
      Parameters:
      sqlTypeName - The sql type name, "json" or "jsonb", needed to invoke the setType() method on the PGobject class.
  • Method Details

    • getSqlValue

      public Object getSqlValue(int column, ResultSet resultSet) throws SQLException, TypeCastException
      Description copied from class: DataType
      Returns the specified column value from the specified resultset object.
      Overrides:
      getSqlValue in class AbstractDataType
      Parameters:
      column - the column index to read, starting at 1.
      resultSet - the result set to read the column value from.
      Returns:
      the column value.
      Throws:
      SQLException - if a database access error occurs.
      TypeCastException - if the value cannot be typecast to this DataType.
    • setSqlValue

      public void setSqlValue(Object value, int column, PreparedStatement statement) throws SQLException, TypeCastException
      Description copied from class: DataType
      Set the specified value to the specified prepared statement object.
      Overrides:
      setSqlValue in class AbstractDataType
      Parameters:
      value - the value to bind.
      column - the parameter index to bind to, starting at 1.
      statement - the prepared statement to bind the value on.
      Throws:
      SQLException - if a database access error occurs.
      TypeCastException - if the value cannot be typecast to this DataType.
    • typeCast

      public Object typeCast(Object value) throws TypeCastException
      Description copied from class: DataType
      Returns the specified value typecasted to this DataType
      Specified by:
      typeCast in class DataType
      Parameters:
      value - the value to typecast.
      Returns:
      the typecast value.
      Throws:
      TypeCastException - if the value cannot be typecast to this DataType.
    • getSqlTypeName

      public String getSqlTypeName()
      Returns the sql type name this instance handles.
      Overrides:
      getSqlTypeName in class DataType
      Returns:
      "json" or "jsonb", whichever sql type name this instance was constructed with.