JSON

by Nickr5
A library for reading and writing JavaScript Object Notation. [More]
To download this library for your Linux/Mac installation, enter this on your command line:

DreamDownload byond://Nickr5.n_Json##version=0

Emulator users, in the BYOND pager go to File | Open Location and enter this URL:

byond://Nickr5.n_Json##version=0

101 downloads
Version 11.3.21
Date added: Mar 20 2011
Last updated: Mar 22 2011
0 fans
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.

Comments

Wirewraith: (Feb 25 2015, 1:38 pm)
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.
Nickr5: (Mar 28 2011, 10:08 am)
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.
Forum_account: (Mar 28 2011, 8:58 am)
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 =)
Nickr5: (Mar 28 2011, 8:42 am)
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
Forum_account: (Mar 28 2011, 3:42 am)
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.