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.


353
354
355
# File 'lib/sequel/extensions/pg_json.rb', line 353

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.


348
349
350
# File 'lib/sequel/extensions/pg_json.rb', line 348

def wrap_json_primitives
  @wrap_json_primitives
end

Class Method Details

.db_parse_json(s) ⇒ Object

Deprecated


304
305
306
307
308
309
310
# File 'lib/sequel/extensions/pg_json.rb', line 304

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


313
314
315
316
317
318
319
# File 'lib/sequel/extensions/pg_json.rb', line 313

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


226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/sequel/extensions/pg_json.rb', line 226

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

.json_primitive_wrapper(value) ⇒ Object

Return the wrapper class for the json type if value is a supported type.


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/sequel/extensions/pg_json.rb', line 260

def self.json_primitive_wrapper(value)
  case value
  when ::Hash
    JSONHash
  when ::Array
    JSONArray
  when ::String
    JSONString
  when ::Integer
    JSONInteger
  when ::Float
    JSONFloat
  when ::NilClass
    JSONNull
  when ::TrueClass
    JSONTrue
  when ::FalseClass
    JSONFalse
  end
end

.json_wrapper(value) ⇒ Object

Return the wrapper class for the json type if value is Hash or Array.


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

def self.json_wrapper(value)
  case value
  when ::Hash
    JSONHash
  when ::Array
    JSONArray
  end
end

.jsonb_primitive_wrapper(value) ⇒ Object

Return the wrapper class for the jsonb type if value is a supported type.


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/sequel/extensions/pg_json.rb', line 282

def self.jsonb_primitive_wrapper(value)
  case value
  when ::Hash
    JSONBHash
  when ::Array
    JSONBArray
  when ::String
    JSONBString
  when ::Integer
    JSONBInteger
  when ::Float
    JSONBFloat
  when ::NilClass
    JSONBNull
  when ::TrueClass
    JSONBTrue
  when ::FalseClass
    JSONBFalse
  end
end

.jsonb_wrapper(value) ⇒ Object

Return the wrapper class for the jsonb type if value is Hash or Array.


250
251
252
253
254
255
256
257
# File 'lib/sequel/extensions/pg_json.rb', line 250

def self.jsonb_wrapper(value)
  case value
  when ::Hash
    JSONBHash
  when ::Array
    JSONBArray
  end
end

.parse_json(s, jsonb = false) ⇒ Object

Deprecated


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/sequel/extensions/pg_json.rb', line 322

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


356
357
358
359
360
361
362
363
# File 'lib/sequel/extensions/pg_json.rb', line 356

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