Module: Sequel::Postgres::JSONDatabaseMethods

Defined in:
lib/sequel/extensions/pg_json.rb

Overview

Methods enabling Database object integration with the json type.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#typecast_json_stringsObject

Whether to typecast strings for json/jsonb types as JSON strings, instead of trying to parse the string as JSON. False by default.



282
283
284
# File 'lib/sequel/extensions/pg_json.rb', line 282

def typecast_json_strings
  @typecast_json_strings
end

#wrap_json_primitivesObject

Whether to wrap JSON primitives instead of using Ruby objects. Wrapping the primitives allows the primitive values to roundtrip, but it can cause problems, especially as false/null JSON values will be treated as truthy in Ruby due to the wrapping. False by default.



277
278
279
# File 'lib/sequel/extensions/pg_json.rb', line 277

def wrap_json_primitives
  @wrap_json_primitives
end

Class Method Details

.db_parse_json(s) ⇒ Object

Deprecated



233
234
235
236
237
238
239
# File 'lib/sequel/extensions/pg_json.rb', line 233

def self.db_parse_json(s)
  # SEQUEL6: Remove
  parse_json(s)
rescue Sequel::InvalidValue
  raise unless s.is_a?(String)
  parse_json("[#{s}]").first
end

.db_parse_jsonb(s) ⇒ Object

Deprecated



242
243
244
245
246
247
248
# File 'lib/sequel/extensions/pg_json.rb', line 242

def self.db_parse_jsonb(s)
  # SEQUEL6: Remove
  parse_json(s, true)
rescue Sequel::InvalidValue
  raise unless s.is_a?(String)
  parse_json("[#{s}]").first
end

.extended(db) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/sequel/extensions/pg_json.rb', line 218

def self.extended(db)
  db.instance_exec do
    add_conversion_proc(114, method(:_db_parse_json))
    add_conversion_proc(3802, 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] = [JSONObject]
    @schema_type_classes[:jsonb] = [JSONBObject]
  end
end

.parse_json(s, jsonb = false) ⇒ Object

Deprecated



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/sequel/extensions/pg_json.rb', line 251

def self.parse_json(s, jsonb=false)
  # SEQUEL6: Remove
  Sequel::Deprecation.deprecate("Sequel::Postgres::JSONDatabaseMethods.{parse_json,db_parse_json,db_parse_jsonb} are deprecated and will be removed in Sequel 6.")
  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

Instance Method Details

#bound_variable_arg(arg, conn) ⇒ Object

Handle json and jsonb types in bound variables



285
286
287
288
289
290
291
292
# File 'lib/sequel/extensions/pg_json.rb', line 285

def bound_variable_arg(arg, conn)
  case arg
  when JSONObject, JSONBObject
    Sequel.object_to_json(arg)
  else
    super
  end
end