#=============================================================================== # JSON Encoder/Decoder # Version 1.2 # Author: game_guy # Mods: Midi #------------------------------------------------------------------------------- # Changelog: # - v. 1.2: # - whitespaces management # - decimal number parsing #------------------------------------------------------------------------------- # Intro: # JSON (JavaScript Object Notation) is a lightweight data-interchange # format. It is easy for humans to read and write. It is easy for machines to # parse and generate. # This is a simple JSON Parser or Decoder. It'll take JSON thats been # formatted into a string and decode it into the proper object. # This script can also encode certain ruby objects into JSON. # # Features: # Decodes JSON format into ruby strings, arrays, hashes, integers, booleans. # # Instructions: # This is a scripters utility. To decode JSON data, call # JSON.decode("json string") # -Depending on "json string", this method can return any of the values: # -Integer # -Float # -String # -Boolean # -Hash # -Array # -Nil # # To Encode objects, use # JSON.encode(object) # -This will return a string with JSON. Object can be any one of the following # -Integer # -String # -Boolean # -Hash # -Array # -Nil # # Credits: # game_guy ~ Creating it. # Midi ~ Midifyng it :) #=============================================================================== module JSON TOKEN_NONE = 0; TOKEN_CURLY_OPEN = 1; TOKEN_CURLY_CLOSED = 2; TOKEN_SQUARED_OPEN = 3; TOKEN_SQUARED_CLOSED = 4; TOKEN_COLON = 5; TOKEN_COMMA = 6; TOKEN_STRING = 7; TOKEN_NUMBER = 8; TOKEN_TRUE = 9; TOKEN_FALSE = 10; TOKEN_NULL = 11; TOKEN_WHITESPACE = 12; @index = 0 @json = "" @length = 0 def self.decode(json) source = if defined?(Ruby310Compat) && Ruby310Compat.respond_to?(:normalize_json_source) Ruby310Compat.normalize_json_source(json) else string = json.to_s if string.respond_to?(:encoding) && string.encoding == Encoding::ASCII_8BIT utf8 = string.dup utf8.force_encoding(Encoding::UTF_8) string = utf8 if utf8.valid_encoding? end bytes = string.b if bytes.start_with?("") stripped = bytes.byteslice(3, bytes.bytesize - 3) || ''.b string = stripped.force_encoding(Encoding::UTF_8) end string.gsub(" ", " ").gsub(" ", " ") end @json = source.to_s @index = 0 @length = @json.length return self.parse end def self.encode(obj) if obj.is_a?(Hash) return self.encode_hash(obj) elsif obj.is_a?(Array) return self.encode_array(obj) elsif obj.is_a?(Numeric) return self.encode_integer(obj) elsif obj.is_a?(String) return self.encode_string(obj) elsif obj.is_a?(TrueClass) || obj.is_a?(FalseClass) return self.encode_bool(obj) elsif obj.is_a?(NilClass) return "null" end return nil end def self.encode_hash(hash) string = "{" hash.each_key {|key| string += "\"#{key}\":" + self.encode(hash[key]).to_s + "," } string[string.size - 1, 1] = "}" return string end def self.encode_array(array) string = "[" array.each {|i| string += self.encode(i).to_s + "," } string[string.size - 1, 1] = "]" return string end def self.encode_string(string) return "\"#{string}\"" end def self.encode_integer(int) return int.to_s end def self.encode_bool(bool) return (bool.is_a?(TrueClass) ? "true" : "false") end # def self.next_token(debug = 0) # char = @json[@index, 1] # @index += 1 # case char # when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '.' # return TOKEN_NUMBER # when '{' # return TOKEN_CURLY_OPEN # when '}' # return TOKEN_CURLY_CLOSED # when '"' # return TOKEN_STRING # when ',' # return TOKEN_COMMA # when '[' # return TOKEN_SQUARED_OPEN # when ']' # return TOKEN_SQUARED_CLOSED # when ':' # return TOKEN_COLON # when ' ', "\n", "\r", "\t" # return self.next_token(debug) # end # @index -= 1 # if @json[@index, 5] == "false" # @index += 5 # return TOKEN_FALSE # elsif @json[@index, 4] == "true" # @index += 4 # return TOKEN_TRUE # elsif @json[@index, 4] == "null" # @index += 4 # return TOKEN_NULL # end # return TOKEN_NONE # end def self.next_token(debug = 0) char = @json[@index, 1] @index += 1 case char when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '.' return TOKEN_NUMBER when '{' return TOKEN_CURLY_OPEN when '}' return TOKEN_CURLY_CLOSED when '"' return TOKEN_STRING when ',' return TOKEN_COMMA when '[' return TOKEN_SQUARED_OPEN when ']' return TOKEN_SQUARED_CLOSED when ':' return TOKEN_COLON when ' ', "\n", "\r", "\t" return self.next_token(debug) when '/' # check for comment if @json[@index, 1] == '/' # skip until newline while @index < @length && @json[@index] != "\n" @index += 1 end return self.next_token(debug) end end @index -= 1 if @json[@index, 5] == "false" @index += 5 return TOKEN_FALSE elsif @json[@index, 4] == "true" @index += 4 return TOKEN_TRUE elsif @json[@index, 4] == "null" @index += 4 return TOKEN_NULL end return TOKEN_NONE end def self.parse(debug = 0) complete = false while !complete if @index >= @length break end token = self.next_token case token when TOKEN_NONE return nil when TOKEN_NUMBER return self.parse_number when TOKEN_CURLY_OPEN return self.parse_object when TOKEN_STRING return self.parse_string when TOKEN_SQUARED_OPEN return self.parse_array when TOKEN_TRUE return true when TOKEN_FALSE return false when TOKEN_NULL return nil end end end def self.parse_object obj = {} complete = false while !complete token = self.next_token if token == TOKEN_CURLY_CLOSED complete = true break elsif token == TOKEN_NONE return nil elsif token == TOKEN_COMMA else name = self.parse_string return nil if name == nil token = self.next_token return nil if token != TOKEN_COLON value = self.parse obj[name] = value end end return obj end def self.parse_string complete = false string = "" while !complete break if @index >= @length char = @json[@index, 1] @index += 1 case char when '"' complete = true break when '\\' esc = @json[@index, 1] @index += 1 case esc when '"', '\\', '/' string += esc.to_s when 'b' string += "\b" when 'f' string += "\f" when 'n' string += "\n" when 'r' string += "\r" when 't' string += "\t" when 'u' hex = @json[@index, 4].to_s @index += 4 string += [hex.to_i(16)].pack("U") else string += esc.to_s end else string += char.to_s end end if !complete return nil end return string end def self.parse_number @index -= 1 negative = false string = "" decimal = false complete = false while !complete break if @index >= @length char = @json[@index, 1] @index += 1 case char when "{", "}", ":", ",", "[", "]" @index -= 1 complete = true break when "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "." raise "Number Format error" if (decimal && char == ".") decimal = char == "." unless decimal string += char.to_s when "-" raise "Number Format error" unless string.empty? negative = true #string += char.to_s end end result = decimal ? string.to_f : string.to_i result = -result if negative return result end # def self.parse_number # @index -= 1 # negative = @json[@index, 1] == "-" ? true : false # string = "" # decimal = false # complete = false # while !complete # break if @index >= @length # char = @json[@index, 1] # @index += 1 # case char # when "{", "}", ":", ",", "[", "]" # @index -= 1 # complete = true # break # when "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "." # raise "Number Format error" if (decimal && char == ".") # decimal = char == "." unless decimal # string += char.to_s # end # end # return decimal ? string.to_f : string.to_i # end # def self.parse_array # obj = [] # complete = false # while !complete # token = self.next_token(1) # if token == TOKEN_SQUARED_CLOSED # complete = true # break # elsif token == TOKEN_NONE # return nil # elsif token == TOKEN_COMMA # else # @index -= 1 # value = self.parse # obj.push(value) # end # end # return obj # end # def self.parse_array # obj = [] # complete = false # while !complete # token = self.next_token(1) # case token # when TOKEN_SQUARED_CLOSED # complete = true # break # when TOKEN_NONE # return nil # when TOKEN_COMMA # # skip # when TOKEN_NULL # obj.push(nil) # else # @index -= 1 # value = self.parse # obj.push(value) # end # end # return obj # end def self.parse_array obj = [] complete = false while !complete token = self.next_token(1) case token when TOKEN_SQUARED_CLOSED complete = true when TOKEN_COMMA next when TOKEN_NULL obj.push(nil) when TOKEN_TRUE obj.push(true) when TOKEN_FALSE obj.push(false) else @index -= 1 value = self.parse obj.push(value) end end return obj end end