The parser is hopefully easy to use and setup, although I didn't document it.
If you haven't heard of JSON, read this.
Three example programs and output:
Code: Select all
#include once "JSONParser.bas"
dim as string jsonstring = _
"{" + chr(10) + _
" ""firstName"": ""John""," + chr(10) + _
" ""lastName"": ""Smith""," + chr(10) + _
" ""age"": 25," + chr(10) + _
" ""address"":" + chr(10) + _
" {" + chr(10) + _
" ""streetAddress"": ""21 2nd Street""," + chr(10) + _
" ""city"": ""New York""," + chr(10) + _
" ""state"": ""NY""," + chr(10) + _
" ""postalCode"": ""10021""" + chr(10) + _
" }," + chr(10) + _
" ""phoneNumber"":" + chr(10) + _
" [" + chr(10) + _
" {" + chr(10) + _
" ""type"": ""home""," + chr(10) + _
" ""number"": ""212 555-1234""" + chr(10) + _
" }," + chr(10) + _
" {" + chr(10) + _
" ""type"": ""fax""," + chr(10) + _
" ""number"": ""646 555-4567""" + chr(10) + _
" }" + chr(10) + _
" ]" + chr(10) + _
"}"
dim as json.variable variable = json.variable(jsonstring)
print "Parsed this:"
print jsonstring
print
print "Variable type: " & variable.getType()
print "firstName: " & variable.getObject()->get("firstName")->toString()
sleep()
Code: Select all
Parsed this:
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
Variable type: Object
firstName: John
Code: Select all
#include once "JSONParser.bas"
dim as string numberstring = "-1.23"
dim as string boolstring = "false"
dim as string invalidstring = "invalid"
dim as string arraystring = "[]"
dim as string objectstring = "{}"
dim as json.variable num = json.variable(numberstring)
dim as json.variable boo = json.variable(boolstring)
dim as json.variable inv = json.variable(invalidstring)
dim as json.variable arr = json.variable(arraystring)
dim as json.variable obj = json.variable(objectstring)
print "For """ & numberstring & """:"
print "< " & num.getType() & " > < " & num.toString() & " >"
print
print "For """ & boolstring & """:"
print "< " & boo.getType() & " > < " & boo.toString() & " >"
print
print "For """ & invalidstring & """:"
print "< " & inv.getType() & " > < " & inv.toString() & " >"
print
print "For """ & arraystring & """:"
print "< " & arr.getType() & " > < " & arr.toString() & " >"
print
print "For """ & objectstring & """:"
print "< " & obj.getType() & " > < " & obj.toString() & " >"
print
sleep()
Code: Select all
For "-1.23":
< Number > < -1.23 >
For "false":
< Boolean > < false >
For "invalid":
< > < >
For "[]":
< Array > < ARRAY >
For "{}":
< Object > < OBJECT >
Code: Select all
#include once "JSONParser.bas"
dim as string jsonstring = "[[5, {}], null]"
dim as json.variable variable = json.variable(jsonstring)
#macro show(var)
print "< " & (var).getType() & " > < " & (var).toString() & " >"
#endmacro
print "For """ & jsonstring & """:"
show(variable)
print "Total children: " & variable.getArray()->getLength()
print
print "First child:"
show(*variable.getArray()->get(0))
print "Second child:"
show(*variable.getArray()->get(1))
print
print "First child's first child:"
show(*variable.getArray()->get(0)->getArray()->get(0))
print "First child's second child:"
show(*variable.getArray()->get(0)->getArray()->get(1))
sleep()
Code: Select all
For "[[5, {}], null]":
< Array > < ARRAY >
Total children: 2
First child:
< Array > < ARRAY >
Second child:
< null > < null >
First child's first child:
< Number > < 5 >
First child's second child:
< Object > < OBJECT >
Project notes / status:
- This can parse a JSON string. Unless the Wikipedia article forgot to mention something, the only limitation here is a lack of unicode support.
- The ability to convert a JSON object to a string could easily be added. Right now variables have a toString() function, but for arrays it just returns "ARRAY" instead of showing the array contents.
- The ability to build a JSON object procedurally (instead of from a string) could also be added. Currently you can add items to arrays and fields to objects, but you cannot mutate instantiated JSON objects in any other way.
- Everything is implemented with linked lists, so speed could be improved, but there is no limit to how much JSON this can handle.
- Most errors in the JSON format will result in the parsed JSON variable having type vartype.malformed, which is easy to catch in code. The only thing I can think of that I'm not catching is multiple fields in an object with the same key.
Source and the example .exes:
http://kristopher.jafile.com/jsonparser.zip