Methods enabling Database object integration with the json type.
Reset the conversion procs when extending the Database object, so it will pick up the json convertor. This is only done for the native postgres adapter.
# File lib/sequel/extensions/pg_json.rb, line 109 109: def self.extended(db) 110: db.reset_conversion_procs if db.respond_to?(:reset_conversion_procs) 111: end
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.
# File lib/sequel/extensions/pg_json.rb, line 89 89: def self.parse_json(s) 90: begin 91: value = JSON.parse(s) 92: rescue JSON::ParserError=>e 93: raise Sequel.convert_exception_class(e, Sequel::InvalidValue) 94: end 95: 96: case value 97: when Array 98: JSONArray.new(value) 99: when Hash 100: JSONHash.new(value) 101: else 102: raise Sequel::InvalidValue, "unhandled json value: #{value.inspect} (from #{s.inspect})" 103: end 104: end
Given a value to typecast to the json column
If given a String, parse it as would be done during database retrieval.
# File lib/sequel/extensions/pg_json.rb, line 141 141: def typecast_value_json(value) 142: case value 143: when JSONArray, JSONHash 144: value 145: when Array 146: JSONArray.new(value) 147: when Hash 148: JSONHash.new(value) 149: when String 150: JSONDatabaseMethods.parse_json(value) 151: else 152: raise Sequel::InvalidValue, "invalid value for json: #{value.inspect}" 153: end 154: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.