Module: Sequel::Postgres::JSONDatabaseMethods
- Defined in:
- lib/sequel/extensions/pg_json.rb
Overview
Methods enabling Database object integration with the json type.
Class Method Summary collapse
-
.db_parse_json(s) ⇒ Object
Parse JSON data coming from the database.
- .extended(db) ⇒ Object
-
.parse_json(s) ⇒ Object
Parse the given string as json, returning either a JSONArray or JSONHash instance, and raising an error if the JSON parsing does not yield an array or hash.
Instance Method Summary collapse
-
#bound_variable_arg(arg, conn) ⇒ Object
Handle JSONArray and JSONHash in bound variables.
Class Method Details
.db_parse_json(s) ⇒ Object
Parse JSON data coming from the database. Since PostgreSQL allows non JSON data in JSON fields (such as plain numbers and strings), we don’t want to raise an exception for that.
106 107 108 109 110 111 |
# File 'lib/sequel/extensions/pg_json.rb', line 106 def self.db_parse_json(s) parse_json(s) rescue Sequel::InvalidValue raise unless s.is_a?(String) parse_json("[#{s}]").first end |
.extended(db) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/sequel/extensions/pg_json.rb', line 96 def self.extended(db) db.instance_eval do copy_conversion_procs([114, 199]) @schema_type_classes[:json] = [JSONHash, JSONArray] end end |
.parse_json(s) ⇒ Object
Parse the given string as json, returning either a JSONArray or JSONHash instance, and raising an error if the JSON parsing does not yield an array or hash.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/sequel/extensions/pg_json.rb', line 116 def self.parse_json(s) 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 JSONArray.new(value) when Hash JSONHash.new(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 JSONArray and JSONHash in bound variables
134 135 136 137 138 139 140 141 |
# File 'lib/sequel/extensions/pg_json.rb', line 134 def bound_variable_arg(arg, conn) case arg when JSONArray, JSONHash Sequel.object_to_json(arg) else super end end |