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
-
#typecast_json_strings ⇒ Object
Whether to typecast strings for json/jsonb types as JSON strings, instead of trying to parse the string as JSON.
-
#wrap_json_primitives ⇒ Object
Whether to wrap JSON primitives instead of using Ruby objects.
Class Method Summary collapse
-
.db_parse_json(s) ⇒ Object
Deprecated.
-
.db_parse_jsonb(s) ⇒ Object
Deprecated.
- .extended(db) ⇒ Object
-
.json_primitive_wrapper(value) ⇒ Object
Return the wrapper class for the json type if value is a supported type.
-
.json_wrapper(value) ⇒ Object
Return the wrapper class for the json type if value is Hash or Array.
-
.jsonb_primitive_wrapper(value) ⇒ Object
Return the wrapper class for the jsonb type if value is a supported type.
-
.jsonb_wrapper(value) ⇒ Object
Return the wrapper class for the jsonb type if value is Hash or Array.
-
.parse_json(s, jsonb = false) ⇒ Object
Deprecated.
Instance Method Summary collapse
-
#bound_variable_arg(arg, conn) ⇒ Object
Handle json and jsonb types in bound variables.
Instance Attribute Details
#typecast_json_strings ⇒ Object
Whether to typecast strings for json/jsonb types as JSON strings, instead of trying to parse the string as JSON. False by default.
363 364 365 |
# File 'lib/sequel/extensions/pg_json.rb', line 363 def typecast_json_strings @typecast_json_strings end |
#wrap_json_primitives ⇒ Object
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.
358 359 360 |
# File 'lib/sequel/extensions/pg_json.rb', line 358 def wrap_json_primitives @wrap_json_primitives end |
Class Method Details
.db_parse_json(s) ⇒ Object
Deprecated
314 315 316 317 318 319 320 |
# File 'lib/sequel/extensions/pg_json.rb', line 314 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
323 324 325 326 327 328 329 |
# File 'lib/sequel/extensions/pg_json.rb', line 323 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
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/sequel/extensions/pg_json.rb', line 236 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.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/sequel/extensions/pg_json.rb', line 270 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.
250 251 252 253 254 255 256 257 |
# File 'lib/sequel/extensions/pg_json.rb', line 250 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.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/sequel/extensions/pg_json.rb', line 292 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.
260 261 262 263 264 265 266 267 |
# File 'lib/sequel/extensions/pg_json.rb', line 260 def self.jsonb_wrapper(value) case value when ::Hash JSONBHash when ::Array JSONBArray end end |
.parse_json(s, jsonb = false) ⇒ Object
Deprecated
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/sequel/extensions/pg_json.rb', line 332 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
366 367 368 369 370 371 372 373 |
# File 'lib/sequel/extensions/pg_json.rb', line 366 def bound_variable_arg(arg, conn) case arg when JSONObject, JSONBObject Sequel.object_to_json(arg) else super end end |