Skip to content

Instantly share code, notes, and snippets.

@nurse
Created March 23, 2009 23:04
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 nurse/83836 to your computer and use it in GitHub Desktop.
Save nurse/83836 to your computer and use it in GitHub Desktop.
r = Regexp.compile(<<'__REGEXP__'.strip, Regexp::EXTENDED)
(?<json> \g<object> | \g<array> ){0}
(?<begin-array> \g<ws> \[ \g<ws> ){0}
(?<begin-object> \g<ws> \{ \g<ws> ){0}
(?<end-array> \g<ws> \] \g<ws> ){0}
(?<end-object> \g<ws> \} \g<ws> ){0}
(?<name-separator> \g<ws> : \g<ws> ){0}
(?<value-separator> \g<ws> , \g<ws> ){0}
(?<ws> [\x20\t\n\r]* ){0}
(?<value> false | null | true | \g<object> | \g<array> | \g<number> | \g<string> ){0}
(?<object> \g<begin-object> (?: \g<member> (?: \g<value-separator> \g<member> )* )? \g<end-object> ){0}
(?<member> \g<string> \g<name-separator> \g<value> ){0}
(?<array> \g<begin-array> (?: \g<value> (?: \g<value-separator> \g<value> )* )? \g<end-array> ){0}
(?<number> \-? \g<int> \g<frac>? \g<exp>? ){0}
(?<exp> [eE] [-+] [0-9]+ ){0}
(?<frac> \. [0-9]+ ){0}
(?<int> 0 | [1-9] [0-9]* ){0}
(?<string> " \g<char>* " ){0}
(?<char> [^\x00-\x1F"\\] | \\ (?: ["\\\/\b\f\n\r\t] | u [0-9a-fA-F]{4} ) ){0}
\A\g<json>\z
__REGEXP__
j1 = <<'__JSON__'
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": "100"
},
"IDs": [116, 943, 234, 38793]
}
}
__JSON__
j2 = <<'__JSON__'
[
{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
"Zip": "94107",
"Country": "US"
},
{
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026020,
"Address": "",
"City": "SUNNYVALE",
"State": "CA",
"Zip": "94085",
"Country": "US"
}
]
__JSON__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment