JSON API v2.0.1 for ComputerCraft
ElvishJerricco 10 Nov 2012
I've written a JSON parser! Simply give it a JSON block and it returns a lua value. Just use os.loadAPI() to load it up. Then call json.decode(). This is really useful because a lot of http servers (for example, the github api) use JSON to transmit data. So now you can actually use that data!
Example:
And now introducing json.encodePretty(). Let's say you want the text to be pretty instead of small. json.encodePretty() will return formatted text instead of compact text. This is useful for things like config files where you want your users to be able to read the json easily.
Finally, json.decodeFromFile(). If you have some JSON in a file, this function will read it for you without you having to open the file and read it manually. Just do
pastebin get 4nRg9CHU json
Changelog:
Example:
os.loadAPI("json") str = http.get("http://www.someserver.com/").readAll() obj = json.decode(str) value = obj.thisVariableWasInTheJSONAndThisIsCoolerThanUsingStringGmatchToFindEverything jsonstring = json.encode(obj) sendThisToWhateverNeedsIt(jsonstring) -- Also, it is very good for very readable config files. prettystring = json.encodePretty(obj) saveThisToAFile(prettystring)
And now introducing json.encodePretty(). Let's say you want the text to be pretty instead of small. json.encodePretty() will return formatted text instead of compact text. This is useful for things like config files where you want your users to be able to read the json easily.
Finally, json.decodeFromFile(). If you have some JSON in a file, this function will read it for you without you having to open the file and read it manually. Just do
local object = json.decodeFromFile("path/to/file")
pastebin get 4nRg9CHU json
Changelog:
Spoiler
ElvishJerricco 10 Nov 2012
gknova61, on 10 November 2012 - 04:30 PM, said:
I can't be sure but you appear to be trying to be condescending, as if what I've done is worthless because it's been done before... I guess people can't make their own implementations of anything anymore... But seeing as nothing like this is on the CC forums I figured I'd be alright to post it.
Kilobyte 14 Nov 2012
hey, mind if i add this to KilOS? (link is in my signature and ofc i'll credit you)
also this might better fit into the apis category />
also this might better fit into the apis category />
Orwell 14 Nov 2012
I think you did a great job on this. It seems to be quite good. Even if it has been implemented before, there are numerous reasons to make it again. The most important one for me is simply developing your own skill set.
ElvishJerricco 17 Nov 2012
Stiepen, on 14 November 2012 - 10:47 AM, said:
hey, mind if i add this to KilOS? (link is in my signature and ofc i'll credit you)
also this might better fit into the apis category />
also this might better fit into the apis category />
I don't mind you using this at all. I actually quite like the KilOS concept (UNIX and stuffs) so yea go ahead and use it.
And yea i realize this would have been better suited there but it's too late now.
Orwell, on 14 November 2012 - 11:24 AM, said:
I think you did a great job on this. It seems to be quite good. Even if it has been implemented before, there are numerous reasons to make it again. The most important one for me is simply developing your own skill set.
I made sure to build it to the best of my ability. The only info i had was json.org which merely explains the format =P
Kilobyte 17 Nov 2012
Orwell 17 Nov 2012
ElvishJerricco, on 17 November 2012 - 11:28 AM, said:
*snip*
I made sure to build it to the best of my ability. The only info i had was json.org which merely explains the format =P
I made sure to build it to the best of my ability. The only info i had was json.org which merely explains the format =P
ElvishJerricco 17 Nov 2012
Orwell, on 17 November 2012 - 02:07 PM, said:
ElvishJerricco, on 17 November 2012 - 11:28 AM, said:
*snip*
I made sure to build it to the best of my ability. The only info i had was json.org which merely explains the format =P
I made sure to build it to the best of my ability. The only info i had was json.org which merely explains the format =P
They both just describe the format. Neither's really more detailed. I was just pointing that out because I know there's a lot of people on the CC Forums that need to copy paste a lot of their code. I don't like to do that kind of programming.
CoolisTheName007 08 Dec 2012
Will test this later, seems like it's a good way to make readable config files!!!
ElvishJerricco 01 Feb 2013
Updated. I added encodePretty. It's really useful for config files (that's why I made it)
Skullblade 01 Feb 2013
not a big deal but FYI this should be in the APIs section
nice job though
nice job though
ElvishJerricco 01 Feb 2013
theoriginalbit 01 Feb 2013
ElvishJerricco 01 Feb 2013
ElvishJerricco 12 Jun 2013
New update v2.0: Completely rewrote the encoder. Much simpler now. The decoder has been coded a bit more smartly, and it handles the technically illegal null value. There is now a decodeFromFile function. Oh and I should mention it's half as long now because of the encoder rewrite. Went from 396 lines to 191
Martin2789 11 Feb 2014
Nice Api you have there i get it to work with easy json like:
EDIT:
Thank you LBPHacker
Edited by Martin2789, 14 February 2014 - 11:19 AM.
{ "firstline": "John", "secline": "Doe", "txtcol": "colors.white", "backcol": "colors.blue" }with
os.loadAPI("json") str = http.get("http://www.someserver.com/").readAll() obj = json.decode(str) print(obj.firstline)but with complicated json like:
{ "monitornorth": [ { "firstline": "John", "secline": "Doe", "txtcol": "colors.white", "backcol": "colors.blue" } ] }
os.loadAPI("json") str = http.get("http://www.someserver.com/").readAll() obj = json.decode(str) print(obj.monitornorth.firstline)What am i do wrong?
EDIT:
LBPHacker, on 13 February 2014 - 12:51 PM, said:
Take a look at that piece of JSON again. monitornorth is an array with one object in it. That object is the first thing in monitornorth, so this is how you access it:
print(obj.monitornorth[1].firstline)
Edited by Martin2789, 14 February 2014 - 11:19 AM.
daphee 20 Jun 2014
Thank you for this nice lib. I've used it successfully on numerous projects.
This time it fails to decode this string
This time it fails to decode this string
{"success":true,"code":0,"error":"","data":{"clientId":"testClient","requestId":"1","data":{"method":"get_redstone","side":"right"}}}It fails with "json:167: attempt to index ? (a nil value)"