module Sequel::Postgres::JSONDatabaseMethods

  1. lib/sequel/extensions/pg_json.rb

Methods enabling Database object integration with the json type.

Attributes

typecast_json_strings [RW]

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

wrap_json_primitives [RW]

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.

Public Class methods

db_parse_json(s)

Deprecated

[show source]
    # File lib/sequel/extensions/pg_json.rb
314 def self.db_parse_json(s)
315   # SEQUEL6: Remove
316   parse_json(s)
317 rescue Sequel::InvalidValue
318   raise unless s.is_a?(String)
319   parse_json("[#{s}]").first
320 end
db_parse_jsonb(s)

Deprecated

[show source]
    # File lib/sequel/extensions/pg_json.rb
323 def self.db_parse_jsonb(s)
324   # SEQUEL6: Remove
325   parse_json(s, true)
326 rescue Sequel::InvalidValue
327   raise unless s.is_a?(String)
328   parse_json("[#{s}]").first
329 end
extended(db)
[show source]
    # File lib/sequel/extensions/pg_json.rb
236 def self.extended(db)
237   db.instance_exec do
238     add_conversion_proc(114, method(:_db_parse_json))
239     add_conversion_proc(3802, method(:_db_parse_jsonb))
240     if respond_to?(:register_array_type)
241       register_array_type('json', :oid=>199, :scalar_oid=>114)
242       register_array_type('jsonb', :oid=>3807, :scalar_oid=>3802)
243     end
244     @schema_type_classes[:json] = [JSONObject]
245     @schema_type_classes[:jsonb] = [JSONBObject]
246   end
247 end
json_primitive_wrapper(value)

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

[show source]
    # File lib/sequel/extensions/pg_json.rb
270 def self.json_primitive_wrapper(value)
271   case value
272   when ::Hash
273     JSONHash
274   when ::Array
275     JSONArray
276   when ::String
277     JSONString
278   when ::Integer
279     JSONInteger
280   when ::Float
281     JSONFloat
282   when ::NilClass
283     JSONNull
284   when ::TrueClass
285     JSONTrue
286   when ::FalseClass
287     JSONFalse
288   end
289 end
json_wrapper(value)

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

[show source]
    # File lib/sequel/extensions/pg_json.rb
250 def self.json_wrapper(value)
251   case value
252   when ::Hash
253     JSONHash
254   when ::Array
255     JSONArray
256   end
257 end
jsonb_primitive_wrapper(value)

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

[show source]
    # File lib/sequel/extensions/pg_json.rb
292 def self.jsonb_primitive_wrapper(value)
293   case value
294   when ::Hash
295     JSONBHash
296   when ::Array
297     JSONBArray
298   when ::String
299     JSONBString
300   when ::Integer
301     JSONBInteger
302   when ::Float
303     JSONBFloat
304   when ::NilClass
305     JSONBNull
306   when ::TrueClass
307     JSONBTrue
308   when ::FalseClass
309     JSONBFalse
310   end
311 end
jsonb_wrapper(value)

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

[show source]
    # File lib/sequel/extensions/pg_json.rb
260 def self.jsonb_wrapper(value)
261   case value
262   when ::Hash
263     JSONBHash
264   when ::Array
265     JSONBArray
266   end
267 end
parse_json(s, jsonb=false)

Deprecated

[show source]
    # File lib/sequel/extensions/pg_json.rb
332 def self.parse_json(s, jsonb=false)
333   # SEQUEL6: Remove
334   Sequel::Deprecation.deprecate("Sequel::Postgres::JSONDatabaseMethods.{parse_json,db_parse_json,db_parse_jsonb} are deprecated and will be removed in Sequel 6.")
335   begin
336     value = Sequel.parse_json(s)
337   rescue Sequel.json_parser_error_class => e
338     raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
339   end
340 
341   case value
342   when Array
343     (jsonb ? JSONBArray : JSONArray).new(value)
344   when Hash 
345     (jsonb ? JSONBHash : JSONHash).new(value)
346   when String, Numeric, true, false, nil
347     value
348   else
349     raise Sequel::InvalidValue, "unhandled json value: #{value.inspect} (from #{s.inspect})"
350   end
351 end

Public Instance methods

bound_variable_arg(arg, conn)

Handle json and jsonb types in bound variables

[show source]
    # File lib/sequel/extensions/pg_json.rb
366 def bound_variable_arg(arg, conn)
367   case arg
368   when JSONObject, JSONBObject
369     Sequel.object_to_json(arg)
370   else
371     super
372   end
373 end