Last active
September 3, 2019 03:26
-
-
Save hyrious/c1313b8d1ced7cb49f15d3fe1de37b5f to your computer and use it in GitHub Desktop.
json.rb for rgss?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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