JSON parser

Started by Dmi, July 20, 2005, 08:04:06 AM

Previous topic - Next topic

Dmi

I wrote a parser for JSON data language http://www.crockford.com/JSON">//http://www.crockford.com/JSON.

Of course, on newLisp :-)



I'am a novice in newLisp, so I can't guess about a quality of code.

Source is downloadable here: http://en.feautec.pp.ru/SiteNews/JSONParser">//http://en.feautec.pp.ru/SiteNews/JSONParser



BTW, the size is comparable to perl one! :-)
WBR, Dmi

Dmi

#1
v1.1 issued:

Fixed call to (replace) - thanks Lutz.

Added usage description
WBR, Dmi

HJH

#2
A JSON parser is a contribution which is very welcome.

Did anybody else use and test the code?



Is it as well possible to write JSON files?



--HJH

Dmi

#3
Hi!

Use (JSON:parse-str str) to parse JSON string to newLisp's json-list equivalent.

Use (JSON:spool-lst lst) to spool newLisp's json-list to JSON string.

See the comments in front of the code for description of json-list structure (or parse something and look at the result ;-)



About the quality: I haven't tested it in a real world.

The examples from json's site was parsed good, stuff that will be spooled with spool-lst will be succesfully parsed by parse-str anyway and will also be JSON-compatible.



About compatibility:

- there is no special unicode support, but none can stop U from using unicode strings.

- when parsing-in the json string, the scalar-type recognition rules are slightly free:

-- initially any value is read as a string, leading double-quote isn't required.

-- double quote's pairs can be inserted in the middle of any strings and this will protect any special symbols between them.

-- if the string ends with a JSON standard token symbols, such as ",[]{}" etc., they will be stripped away (but, this can safely be turned off).

-- next, translation to fload and integer is performed by newLisp's internal functions and, if succeded, the value type will be float or int. So, for ex., string "12abc" will be translated to integer 12.

-- next, if the string is equal to "true", "false", or "null", then the value type will be JSON's true, false or null equvalents.

-- so, note again, that the double quotes does not protect value from type recognition.



Nevertheless the code is quite small and simple, so the most of this behavior can be easily modified. Feel free to request for assistance...
WBR, Dmi