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
-
.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.
-
#schema_column_type(db_type) ⇒ Object
Make the column type detection recognize the json type.
Class Method Details
.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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sequel/extensions/pg_json.rb', line 94 def self.parse_json(s) begin value = JSON.parse(s) rescue JSON::ParserError=>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
112 113 114 115 116 117 118 119 |
# File 'lib/sequel/extensions/pg_json.rb', line 112 def bound_variable_arg(arg, conn) case arg when JSONArray, JSONHash arg.to_json else super end end |
#schema_column_type(db_type) ⇒ Object
Make the column type detection recognize the json type.
122 123 124 125 126 127 128 129 |
# File 'lib/sequel/extensions/pg_json.rb', line 122 def schema_column_type(db_type) case db_type when 'json' :json else super end end |