class Crack::JSON
Constants
- DATE_REGEX
-
matches YAML-formatted dates
Public Class Methods
Source
# File lib/crack/json.rb, line 98 def self.format_dates(output, date_starts, date_ends) if YAML.constants.include?('Syck') (date_starts + date_ends).each { |i| output[i-1] = ' ' } else extra_chars_to_be_added = 0 timestamp_marker = '!!timestamp ' timestamp_marker_size = timestamp_marker.size date_starts.each do |i| output[i-2+extra_chars_to_be_added] = timestamp_marker extra_chars_to_be_added += timestamp_marker_size - 1 end end end
Source
# File lib/crack/json.rb, line 16 def self.parse(json) yaml = unescape(convert_json_to_yaml(json)) YAML.safe_load(yaml, permitted_classes: [Regexp, Date, Time]) rescue *parser_exceptions raise ParseError, "Invalid JSON string" rescue Psych::DisallowedClass yaml end
Source
# File lib/crack/json.rb, line 11 def self.parser_exceptions @parser_exceptions ||= [ArgumentError, Psych::SyntaxError] end
Source
# File lib/crack/json.rb, line 36 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