I made a streaming parser for a new JSON variant. I call it “tagged HJSON”. It handles HJSON with the addition of tags to objects, which is about the only thing that seriously makes JSON less useful than XML, because XML blocks have named tags. It looks a bit like this:
#thjson (optional first line)
{ # Optional root {} construct
# comments are useful
# specify rate in requests/second
"rate": 1000
// maybe you prefer js style comments
/* or if you feel old fashioned */
# key names do not need to be placed in quotes
key: "value"
# you don't need quotes for strings
text: look ma, no quotes!
# note that for quoteless strings everything up
# to the next line is part of the string!
# commas are optional
commas:
{
one: 1
two: 2
}
# trailing commas are allowed
trailing:
{
one: 1,
two: 2,
}
# This is a tagged object
tagged:
clazz {
one: 1,
two: 2,
}
# It does tagged arrays too
taggedarray: integers [1, 2, 3, 4]
# multiline string
haiku:
'''
JSON I love you.
But you strangle my expression.
This is so much better.
'''
# Obviously you can always use standard JSON syntax as well:
favNumbers: [ 1, 2, 3, 6, 42 ]
}
The tagging is purely optional and essentially just passes in a string to the beginObject() or beginArray() events. I plan to use it to refer to a map of tags to Java classes - so I will be able to use this new THJSON format to deserialise resource data directly into POJOs. Neat!
It’s about 680 lines of source and could do with optimising… it’s nowhere near as fancy lightweight as the FTL parser. I will tidy it up a tiny bit, optimise it a bit, and share it on JGO soonish.
Cas