JSON

by Nickr5
A library for reading and writing JavaScript Object Notation.
ID:111084
 
Provides two methods: json2list() and list2json().

For more information, see the Wikipedia article:
JSON is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for most programming languages.
Cool, I was hoping someone would write a lib for this.
I need to remember to check this out. I could use it for my interface library (which ended up having very little to do with interfaces and a lot with calling JS functions from DM and vice versa).
I haven't look through the code too much, but is there an easy way to parse any JS data? I don't want to parse objects all the time, sometimes I just want to parse an array.
Not really, though you could just add the array to an object and then parse that:
var/array = "\[1,2,3]"
var/list/result = json2list("{array:[array]}")
for(var/number in result["array"])
world<<number
Ok, I'll look at it again today. There are some other changes I'd need to make too, but I can probably get it working without having to change your library. All this to avoid writing my own parser =)
Actually, there's a more direct way of doing this. You should be able to create a new /json_reader and use it to parse any value. I haven't tested this code, but it should work.
proc/get_value(json)
var/json_reader/reader = new()
reader.tokens = reader.ScanJson(json)
reader.i = 1
return reader.read_value()

Where json is any value: an array, a number, an object, a string, true, false, or null. This is not the way the library is meant to be used, so I will probably add better support for what you're trying to do in the future.
Hi hello, just for posterity it's worth mentioning that this forms improper JSON strings.

For list: foo=bar
It will produce: {foo:"bar"}
This is missing the double quotes around the key (foo).

You can fix it by just modifying WriteObject() in JSON Writer.dm (put quotes around [k]).

Good job I guess.