module Sequel::Postgres::JSONDatabaseMethods

Methods enabling Database object integration with the json type.

Public Class Methods

db_parse_json(s) click to toggle source

Parse JSON data coming from the database. Since PostgreSQL allows non JSON data in JSON fields (such as plain numbers and strings), we don't want to raise an exception for that.

# File lib/sequel/extensions/pg_json.rb, line 144
def self.db_parse_json(s)
  parse_json(s)
rescue Sequel::InvalidValue
  raise unless s.is_a?(String)
  parse_json("[#{s}]").first
end
db_parse_jsonb(s) click to toggle source

Same as ::db_parse_json, but consider the input as jsonb.

# File lib/sequel/extensions/pg_json.rb, line 152
def self.db_parse_jsonb(s)
  parse_json(s, true)
rescue Sequel::InvalidValue
  raise unless s.is_a?(String)
  parse_json("[#{s}]").first
end
extended(db) click to toggle source
# File lib/sequel/extensions/pg_json.rb, line 128
def self.extended(db)
  db.instance_exec do
    add_conversion_proc(114, JSONDatabaseMethods.method(:db_parse_json))
    add_conversion_proc(3802, JSONDatabaseMethods.method(:db_parse_jsonb))
    if respond_to?(:register_array_type)
      register_array_type('json', :oid=>199, :scalar_oid=>114)
      register_array_type('jsonb', :oid=>3807, :scalar_oid=>3802)
    end
    @schema_type_classes[:json] = [JSONHash, JSONArray]
    @schema_type_classes[:jsonb] = [JSONBHash, JSONBArray]
  end
end
parse_json(s, jsonb=false) click to toggle source

Parse the given string as json, returning either a JSONArray or JSONHash instance (or JSONBArray or JSONBHash instance if jsonb argument is true), or a String, Numeric, true, false, or nil if the json library used supports that.

# File lib/sequel/extensions/pg_json.rb, line 163
def self.parse_json(s, jsonb=false)
  begin
    value = Sequel.parse_json(s)
  rescue Sequel.json_parser_error_class => e
    raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
  end

  case value
  when Array
    (jsonb ? JSONBArray : JSONArray).new(value)
  when Hash 
    (jsonb ? JSONBHash : JSONHash).new(value)
  when String, Numeric, true, false, nil
    value
  else
    raise Sequel::InvalidValue, "unhandled json value: #{value.inspect} (from #{s.inspect})"
  end
end

Public Instance Methods

bound_variable_arg(arg, conn) click to toggle source

Handle json and jsonb types in bound variables

Calls superclass method
# File lib/sequel/extensions/pg_json.rb, line 183
def bound_variable_arg(arg, conn)
  case arg
  when JSONArrayBase, JSONHashBase
    Sequel.object_to_json(arg)
  else
    super
  end
end

Private Instance Methods

bound_variable_array(a) click to toggle source

Handle json[] and jsonb[] types in bound variables.

Calls superclass method
# File lib/sequel/extensions/pg_json.rb, line 195
def bound_variable_array(a)
  case a
  when JSONHashBase, JSONArrayBase
    "\"#{Sequel.object_to_json(a).gsub('"', '\\"')}\""
  else
    super
  end
end
schema_column_type(db_type) click to toggle source

Make the column type detection recognize the json types.

Calls superclass method
# File lib/sequel/extensions/pg_json.rb, line 205
def schema_column_type(db_type)
  case db_type
  when 'json'
    :json
  when 'jsonb'
    :jsonb
  else
    super
  end
end
schema_post_process(_) click to toggle source

Set the :callable_default value if the default value is recognized as an empty json/jsonb array/hash.

Calls superclass method
# File lib/sequel/extensions/pg_json.rb, line 217
def schema_post_process(_)
  super.each do |a|
    h = a[1]
    if (h[:type] == :json || h[:type] == :jsonb) && h[:default] =~ /\A'(\{\}|\[\])'::jsonb?\z/
      is_array = $1 == '[]'

      klass = if h[:type] == :json
        if is_array
          JSONArray
        else
          JSONHash
        end
      elsif is_array
        JSONBArray
      else
        JSONBHash
      end

      h[:callable_default] = lambda{klass.new(is_array ? [] : {})}
    end
  end
end
typecast_value_json(value) click to toggle source

Convert the value given to a JSONArray or JSONHash

# File lib/sequel/extensions/pg_json.rb, line 241
def typecast_value_json(value)
  case value
  when JSONArray, JSONHash
    value
  when Array
    JSONArray.new(value)
  when Hash 
    JSONHash.new(value)
  when JSONBArray
    JSONArray.new(value.to_a)
  when JSONBHash
    JSONHash.new(value.to_hash)
  when String
    JSONDatabaseMethods.parse_json(value)
  else
    raise Sequel::InvalidValue, "invalid value for json: #{value.inspect}"
  end
end
typecast_value_jsonb(value) click to toggle source

Convert the value given to a JSONBArray or JSONBHash

# File lib/sequel/extensions/pg_json.rb, line 261
def typecast_value_jsonb(value)
  case value
  when JSONBArray, JSONBHash
    value
  when Array
    JSONBArray.new(value)
  when Hash 
    JSONBHash.new(value)
  when JSONArray
    JSONBArray.new(value.to_a)
  when JSONHash
    JSONBHash.new(value.to_hash)
  when String
    JSONDatabaseMethods.parse_json(value, true)
  else
    raise Sequel::InvalidValue, "invalid value for jsonb: #{value.inspect}"
  end
end