class PhusionPassenger::Utils::JSON


parsed as JSON and to display the result in Ruby.
Run tests by executing this file directly. Pipe standard input to the script to have it
JSON.generate(object) => json string
JSON.parse(json_string) => Array/Hash
Usage:

def self.parse(data) new(data).parse end

def self.parse(data) new(data).parse end

def array

def array
  ary = []
  space
  repeat_until(AEN) do
    space
    ary << value
    endkey
  end
  ary
end

def endkey() scan(KEY) or space end

def endkey() scan(KEY) or space end

def error

def error
  raise "parse error at: #{scan(/.{1,20}/m).inspect}"
end

def hash

def hash
  obj = {}
  space
  repeat_until(HEN) do
    space
    k = string
    scan(COL)
    obj[k] = value
    endkey
  end
  obj
end

def initialize data

def initialize data
  @scanner = StringScanner.new data.to_s
end

def object

def object
  matched == '{' ? hash : array if scan(OBJ)
end

def parse

def parse
  space
  object
end

def repeat_until reg

def repeat_until reg
  until scan(reg)
    pos = s.pos
    yield
    error unless s.pos > pos
  end
end

def space() scan WSP end

def space() scan WSP end

def string

def string
  if scan(STR)
    str, esc = '', false
    while c = s.getch
      if esc
        str << (c == UNI ? (s.scan(CODE) || error).to_i(16).chr : SPEC[c] || c)
        esc = false
      else
        case c
        when ESC then esc = true
        when STE then break
        else str << c
        end
      end
    end
    str
  end
end

def value

def value
  object or string or
    scan(NUL) ? nil :
    scan(BOL) ? matched.size == 4:
    scan(NUM) ? eval(matched) :
    error
end