Sha256: 7de0405be64fb4991fb587fba5a78efae9a55ac9c8255fee025cdd0a88bc7182

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

require 'pp'
module Parse

  # Parse a JSON representation into a fully instantiated
  # class. obj can be either a string or a Hash as parsed
  # by JSON.parse
  # @param class_name [Object]
  # @param obj [Object]
  def Parse.parse_json(class_name, obj)
    if obj.nil?
      nil

    # Array
    elsif obj.is_a? Array
      obj.collect { |o| parse_json(class_name, o) }

    # Hash
    elsif obj.is_a? Hash

      # If it's a datatype hash
      if obj.has_key?(Protocol::KEY_TYPE)
        parse_datatype obj
      elsif obj.size == 1 && obj.has_key?(Protocol::KEY_RESULTS) && obj[Protocol::KEY_RESULTS].is_a?(Array)
        obj[Protocol::KEY_RESULTS].collect { |o| parse_json(class_name, o) }
      else # otherwise it must be a regular object, so deep parse it avoiding re-JSON.parsing raw Strings
        Parse::Object.new class_name, Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
      end

    # primitive
    else
      obj
    end
  end

  def Parse.parse_datatype(obj)
    type = obj[Protocol::KEY_TYPE]

    case type
      when Protocol::TYPE_POINTER
        Parse::Pointer.new obj
      when Protocol::TYPE_BYTES
        Parse::Bytes.new obj
      when Protocol::TYPE_DATE
        Parse::Date.new obj
      when Protocol::TYPE_GEOPOINT
        Parse::GeoPoint.new obj
      when Protocol::TYPE_FILE
        Parse::File.new obj
      when Protocol::TYPE_OBJECT # used for relation queries, e.g. "?include=post"
        Parse::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
parse-ruby-client-0.1.14 lib/parse/util.rb
parse-ruby-client-0.1.13 lib/parse/util.rb
parse-ruby-client-0.1.12 lib/parse/util.rb
parse-ruby-client-0.1.11 lib/parse/util.rb