Class/Module Index [+]

Quicksearch

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 145
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 153
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 134
def self.extended(db)
  db.instance_eval do
    copy_conversion_procs([114, 199, 3802, 3807])
    @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 164
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 JSONArray and JSONHash in bound variables

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.