module Sequel::Postgres::JSONDatabaseMethods

  1. lib/sequel/extensions/pg_json.rb
Parent: Postgres

Methods enabling Database object integration with the json type.

Methods

Public Class

  1. extended
  2. parse_json

Public Instance

  1. bound_variable_arg

Public Class methods

extended (db)
[show source]
# File lib/sequel/extensions/pg_json.rb, line 96
def self.extended(db)
  db.instance_eval do
    copy_conversion_procs([114, 199])
    @schema_type_classes[:json] = [JSONHash, JSONArray]
  end
end
parse_json (s)

Parse the given string as json, returning either a JSONArray or JSONHash instance, and raising an error if the JSON parsing does not yield an array or hash.

[show source]
# File lib/sequel/extensions/pg_json.rb, line 106
def self.parse_json(s)
  begin
    value = Sequel.parse_json(s)
  rescue JSON::ParserError=>e
    raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
  end

  case value
  when Array
    JSONArray.new(value)
  when Hash 
    JSONHash.new(value)
  else
    raise Sequel::InvalidValue, "unhandled json value: #{value.inspect} (from #{s.inspect})"
  end
end

Public Instance methods

bound_variable_arg (arg, conn)

Handle JSONArray and JSONHash in bound variables

[show source]
# File lib/sequel/extensions/pg_json.rb, line 124
def bound_variable_arg(arg, conn)
  case arg
  when JSONArray, JSONHash
    arg.to_json
  else
    super
  end
end