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.
-
.db_parse_jsonb(s) ⇒ Object
Same as db_parse_json, but consider the input as jsonb.
- .extended(db) ⇒ Object
-
.parse_json(s, jsonb = false) ⇒ Object
Parse the given string as json, returning either a JSONArray or JSONHash instance (or JSONBArray or JSONBHash instance if jsonb argument is true), or a String, Numeric, true, false, or nil if the json library used supports that.
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.
151 152 153 154 155 156 |
# File 'lib/sequel/extensions/pg_json.rb', line 151 def self.db_parse_json(s) parse_json(s) rescue Sequel::InvalidValue raise unless s.is_a?(String) parse_json("[#{s}]").first end |
.db_parse_jsonb(s) ⇒ Object
Same as db_parse_json, but consider the input as jsonb.
159 160 161 162 163 164 |
# File 'lib/sequel/extensions/pg_json.rb', line 159 def self.db_parse_jsonb(s) parse_json(s, true) rescue Sequel::InvalidValue raise unless s.is_a?(String) parse_json("[#{s}]").first end |
.extended(db) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sequel/extensions/pg_json.rb', line 135 def self.extended(db) db.instance_eval do add_conversion_proc(114, JSONDatabaseMethods.method(:db_parse_json)) add_conversion_proc(3802, JSONDatabaseMethods.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] = [JSONHash, JSONArray] @schema_type_classes[:jsonb] = [JSONBHash, JSONBArray] end end |
.parse_json(s, jsonb = false) ⇒ Object
Parse the given string as json, returning either a JSONArray or JSONHash instance (or JSONBArray or JSONBHash instance if jsonb argument is true), or a String, Numeric, true, false, or nil if the json library used supports that.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/sequel/extensions/pg_json.rb', line 170 def self.parse_json(s, jsonb=false) 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 JSONArray and JSONHash in bound variables
190 191 192 193 194 195 196 197 |
# File 'lib/sequel/extensions/pg_json.rb', line 190 def bound_variable_arg(arg, conn) case arg when JSONArrayBase, JSONHashBase Sequel.object_to_json(arg) else super end end |