Created
May 23, 2012 14:57
-
-
Save pmarreck/2775709 to your computer and use it in GitHub Desktop.
Apparently this is an actual JSON validator, written in late-model Regexp, using the Ruby interpreter.
This file contains hidden or 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
# encoding: utf-8 | |
module Constants | |
JSON_VALIDATOR_RE = /( | |
# define subtypes and build up the json syntax, BNF-grammar-style | |
# The {0} is a hack to simply define them as named groups here but not match on them yet | |
# I added some atomic grouping to prevent catastrophic backtracking on invalid inputs | |
(?<number> -?(?=[1-9]|0(?!\d))\d+(\.\d+)?([eE][+-]?\d+)?){0} | |
(?<boolean> true | false | null ){0} | |
(?<string> " (?>[^"\\\\]* | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* " ){0} | |
(?<array> \[ (?> \g<json> (?: , \g<json> )* )? \s* \] ){0} | |
(?<pair> \s* \g<string> \s* : \g<json> ){0} | |
(?<object> \{ (?> \g<pair> (?: , \g<pair> )* )? \s* \} ){0} | |
(?<json> \s* (?> \g<number> | \g<boolean> | \g<string> | \g<array> | \g<object> ) \s* ){0} | |
) | |
\A \g<json> \Z | |
/uix | |
end | |
########## inline test running | |
if __FILE__==$PROGRAM_NAME | |
# support | |
class String | |
def unindent | |
gsub(/^#{scan(/^(?!\n)\s*/).min_by{|l|l.length}}/u, "") | |
end | |
end | |
require 'test/unit' unless defined? Test::Unit | |
class JsonValidationTest < Test::Unit::TestCase | |
include Constants | |
def setup | |
end | |
def test_json_validator_simple_string | |
assert_not_nil %s[ {"somedata": 5 }].match(JSON_VALIDATOR_RE) | |
end | |
def test_json_validator_deep_string | |
long_json = <<-JSON.unindent | |
{ | |
"glossary": { | |
"title": "example glossary", | |
"GlossDiv": { | |
"id": 1918723, | |
"boolean": true, | |
"title": "S", | |
"GlossList": { | |
"GlossEntry": { | |
"ID": "SGML", | |
"SortAs": "SGML", | |
"GlossTerm": "Standard Generalized Markup Language", | |
"Acronym": "SGML", | |
"Abbrev": "ISO 8879:1986", | |
"GlossDef": { | |
"para": "A meta-markup language, used to create markup languages such as DocBook.", | |
"GlossSeeAlso": ["GML", "XML"] | |
}, | |
"GlossSee": "markup" | |
} | |
} | |
} | |
} | |
} | |
JSON | |
assert_not_nil long_json.match(JSON_VALIDATOR_RE) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment