Methods enabling Database
object integration with the json type.
Methods
Public Class
- db_parse_json
- db_parse_jsonb
- extended
- json_primitive_wrapper
- json_wrapper
- jsonb_primitive_wrapper
- jsonb_wrapper
- parse_json
Public Instance
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 304 def self.db_parse_json(s) 305 # SEQUEL6: Remove 306 parse_json(s) 307 rescue Sequel::InvalidValue 308 raise unless s.is_a?(String) 309 parse_json("[#{s}]").first 310 end
db_parse_jsonb(s)
Deprecated
[show source]
# File lib/sequel/extensions/pg_json.rb 313 def self.db_parse_jsonb(s) 314 # SEQUEL6: Remove 315 parse_json(s, true) 316 rescue Sequel::InvalidValue 317 raise unless s.is_a?(String) 318 parse_json("[#{s}]").first 319 end
extended(db)
[show source]
# File lib/sequel/extensions/pg_json.rb 226 def self.extended(db) 227 db.instance_exec do 228 add_conversion_proc(114, method(:_db_parse_json)) 229 add_conversion_proc(3802, method(:_db_parse_jsonb)) 230 if respond_to?(:register_array_type) 231 register_array_type('json', :oid=>199, :scalar_oid=>114) 232 register_array_type('jsonb', :oid=>3807, :scalar_oid=>3802) 233 end 234 @schema_type_classes[:json] = [JSONObject] 235 @schema_type_classes[:jsonb] = [JSONBObject] 236 end 237 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 260 def self.json_primitive_wrapper(value) 261 case value 262 when ::Hash 263 JSONHash 264 when ::Array 265 JSONArray 266 when ::String 267 JSONString 268 when ::Integer 269 JSONInteger 270 when ::Float 271 JSONFloat 272 when ::NilClass 273 JSONNull 274 when ::TrueClass 275 JSONTrue 276 when ::FalseClass 277 JSONFalse 278 end 279 end
json_wrapper(value)
[show source]
# File lib/sequel/extensions/pg_json.rb 240 def self.json_wrapper(value) 241 case value 242 when ::Hash 243 JSONHash 244 when ::Array 245 JSONArray 246 end 247 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 282 def self.jsonb_primitive_wrapper(value) 283 case value 284 when ::Hash 285 JSONBHash 286 when ::Array 287 JSONBArray 288 when ::String 289 JSONBString 290 when ::Integer 291 JSONBInteger 292 when ::Float 293 JSONBFloat 294 when ::NilClass 295 JSONBNull 296 when ::TrueClass 297 JSONBTrue 298 when ::FalseClass 299 JSONBFalse 300 end 301 end
jsonb_wrapper(value)
[show source]
# File lib/sequel/extensions/pg_json.rb 250 def self.jsonb_wrapper(value) 251 case value 252 when ::Hash 253 JSONBHash 254 when ::Array 255 JSONBArray 256 end 257 end
parse_json(s, jsonb=false)
Deprecated
[show source]
# File lib/sequel/extensions/pg_json.rb 322 def self.parse_json(s, jsonb=false) 323 # SEQUEL6: Remove 324 Sequel::Deprecation.deprecate("Sequel::Postgres::JSONDatabaseMethods.{parse_json,db_parse_json,db_parse_jsonb} are deprecated and will be removed in Sequel 6.") 325 begin 326 value = Sequel.parse_json(s) 327 rescue Sequel.json_parser_error_class => e 328 raise Sequel.convert_exception_class(e, Sequel::InvalidValue) 329 end 330 331 case value 332 when Array 333 (jsonb ? JSONBArray : JSONArray).new(value) 334 when Hash 335 (jsonb ? JSONBHash : JSONHash).new(value) 336 when String, Numeric, true, false, nil 337 value 338 else 339 raise Sequel::InvalidValue, "unhandled json value: #{value.inspect} (from #{s.inspect})" 340 end 341 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 356 def bound_variable_arg(arg, conn) 357 case arg 358 when JSONObject, JSONBObject 359 Sequel.object_to_json(arg) 360 else 361 super 362 end 363 end