Method: Sequel::Postgres::JSONDatabaseMethods.parse_json

Defined in:
lib/sequel/extensions/pg_json.rb

.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