class Crack::JSON

Constants

DATE_REGEX

matches YAML-formatted dates

Public Class Methods

parse(json) click to toggle source
# File lib/crack/json.rb, line 25
def self.parse(json)
  args = [unescape(convert_json_to_yaml(json))]
  args << nil if SafeYAML::MULTI_ARGUMENT_YAML_LOAD
  args << { :whitelisted_tags => ['!ruby/regexp'] }

  SafeYAML.load(*args)

rescue *parser_exceptions
  raise ParseError, "Invalid JSON string"
end
parser_exceptions() click to toggle source
# File lib/crack/json.rb, line 11
def self.parser_exceptions
  @parser_exceptions ||= begin
    exceptions = [ArgumentError]

    if const_defined?(:Psych)
      if Psych.const_defined?(:SyntaxError)
        exceptions << Psych::SyntaxError
      end
    end

    exceptions
  end
end

Protected Class Methods

format_dates(output, date_starts, date_ends) click to toggle source
# File lib/crack/json.rb, line 99
def self.format_dates(output, date_starts, date_ends)
  if YAML.constants.include?('Syck')
    (date_starts + date_ends).each { |i| output[i-1] = ' ' }
  else
    date_starts.each { |i| output[i-2] = '!!timestamp ' }
  end
end
unescape(str) click to toggle source
# File lib/crack/json.rb, line 37
def self.unescape(str)
  # Force the encoding to be UTF-8 so we can perform regular expressions
  # on 1.9.2 without blowing up.
  # see http://stackoverflow.com/questions/1224204/ruby-mechanize-getting-force-encoding-exception for a similar issue
  str.force_encoding('UTF-8') if defined?(Encoding) && str.respond_to?(:force_encoding)
  str.gsub(/\u0000/, "").gsub(/\[u|U]([0-9a-fA-F]{4})/) { [$1.hex].pack("U") }
end