Sha256: eee2c8ef7b5fece7eaa1564b66074545fcca0c803e0f8fffc805102b9bbe620f

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

module BubbleWrap

  # Handles JSON encoding and decoding in a similar way Ruby 1.9 does.
  module JSON

    class ParserError < StandardError; end

    # Parses a string or data object and converts it in data structure.
    #
    # @param [String, NSData] str_data the string or data to serialize.
    # @raise [ParserError] If the parsing of the passed string/data isn't valid.
    # @return [Hash, Array, NilClass] the converted data structure, nil if the incoming string isn't valid.
    #
    # TODO: support options like the C Ruby module does
    def self.parse(str_data, &block)
      data = str_data.respond_to?(:to_data) ? str_data.to_data : str_data
      opts = NSJSONReadingMutableContainers & NSJSONReadingMutableLeaves & NSJSONReadingAllowFragments
      error = Pointer.new(:id)
      obj = NSJSONSerialization.JSONObjectWithData(data, options:opts, error:error)
      raise ParserError, error[0].description if error[0]
      if block_given?
        yield obj
      else 
        obj
      end
      
    end

    def self.generate(obj)
      # opts = NSJSONWritingPrettyPrinted
      data = NSJSONSerialization.dataWithJSONObject(obj, options:0, error:nil)
      data.to_str
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
bubble-wrap-1.1.2 motion/core/json.rb
bubble-wrap-1.1.1 motion/core/json.rb
bubble-wrap-1.1.0 motion/core/json.rb
bubble-wrap-1.0.0 motion/core/json.rb
bubble-wrap-1.0.0.pre.2 motion/core/json.rb
bubble-wrap-1.0.0.pre motion/core/json.rb
bubble-wrap-0.4.0 motion/core/json.rb
bubble-wrap-0.3.1 lib/bubble-wrap/json.rb
bubble-wrap-0.3.0 lib/bubble-wrap/json.rb
bubble-wrap-0.2.1 lib/bubble-wrap/json.rb
bubble-wrap-0.2.0 lib/bubble-wrap/json.rb
bubble-wrap-0.1.2 lib/bubble-wrap/json.rb
bubble-wrap-0.1.1 lib/bubble-wrap/json.rb
bubble-wrap-0.1.0 lib/bubble-wrap/json.rb