Skip to content

Instantly share code, notes, and snippets.

@hyrious
Last active September 3, 2019 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyrious/c1313b8d1ced7cb49f15d3fe1de37b5f to your computer and use it in GitHub Desktop.
Save hyrious/c1313b8d1ced7cb49f15d3fe1de37b5f to your computer and use it in GitHub Desktop.
json.rb for rgss?
# coding: utf-8
# pure ruby json implementation
module JSON
def self.parse json
ret = nil
json.slice!(/^\s+/)
case
when json.slice!(/^null/)
ret = nil
when json.slice!(/^false/)
ret = false
when json.slice!(/^true/)
ret = true
when raw = json.slice!(/^[-]?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)?/)
ret = (Integer(raw) rescue Float(raw))
when raw = json.slice!(/^\"(?:\\.|[^"])*\"/)
ret = eval(raw.force_encoding('utf-8'))
when json.slice!(/^\[/)
ret = []
until json.slice!(/^\]/)
ret << parse(json)
json.slice!(/^,/)
end
when json.slice!(/^\{/)
ret = {}
until json.slice!(/^\}/)
key = parse(json)
json.slice!(/^:/)
value = parse(json)
ret[key] = value
end
else
raise ArgumentError, 'expect json string'
end
json.slice!(/^\s+/)
ret
end
end
JSON.parse '"#{p self}"' # injection!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment