Jump to content




JSON API v2.0.1 for ComputerCraft


54 replies to this topic

#1 ElvishJerricco

  • Members
  • 803 posts

Posted 10 November 2012 - 02:17 AM

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:

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


#2 gknova61

  • Members
  • 74 posts

Posted 10 November 2012 - 04:30 PM

Nice job, guy! =D

#3 ElvishJerricco

  • Members
  • 803 posts

Posted 10 November 2012 - 05:16 PM

View Postgknova61, 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.

#4 zekesonxx

  • Signature Abuser
  • 263 posts
  • LocationWhere you aren't

Posted 11 November 2012 - 04:34 PM

-snip-

Fixed, nevermind.

#5 Kilobyte

  • Members
  • 122 posts

Posted 14 November 2012 - 10:47 AM

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 :P/>

#6 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 14 November 2012 - 11:24 AM

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.

#7 ElvishJerricco

  • Members
  • 803 posts

Posted 17 November 2012 - 11:28 AM

View PostStiepen, 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 :)/>

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.

View PostOrwell, 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

#8 Kilobyte

  • Members
  • 122 posts

Posted 17 November 2012 - 11:45 AM

View PostElvishJerricco, on 17 November 2012 - 11:28 AM, said:

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.
Will be in next version

#9 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 17 November 2012 - 02:07 PM

View PostElvishJerricco, 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 a JSON parser myself once, and I used the RFC: https://www.ietf.org/rfc/rfc4627.txt . Is this more detailed than the info you had? :)/>

#10 ElvishJerricco

  • Members
  • 803 posts

Posted 17 November 2012 - 05:06 PM

View PostOrwell, on 17 November 2012 - 02:07 PM, said:

View PostElvishJerricco, 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 a JSON parser myself once, and I used the RFC: https://www.ietf.org/rfc/rfc4627.txt . Is this more detailed than the info you had? :)/>

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.

#11 CoolisTheName007

  • Members
  • 304 posts

Posted 08 December 2012 - 11:22 AM

Will test this later, seems like it's a good way to make readable config files!!! :D

#12 ElvishJerricco

  • Members
  • 803 posts

Posted 01 February 2013 - 03:08 PM

Updated. I added encodePretty. It's really useful for config files (that's why I made it)

#13 Skullblade

  • Members
  • 470 posts
  • LocationThe Big Apple, NY

Posted 01 February 2013 - 03:58 PM

not a big deal but FYI this should be in the APIs section ;)
nice job though

#14 ElvishJerricco

  • Members
  • 803 posts

Posted 01 February 2013 - 04:05 PM

View PostSkullblade, on 01 February 2013 - 03:58 PM, said:

not a big deal but FYI this should be in the APIs section ;)
nice job though

Yea I wasn't thinking when I posted this. Not sure how to move it though =P

#15 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 01 February 2013 - 04:06 PM

View PostElvishJerricco, on 01 February 2013 - 04:05 PM, said:

Yea I wasn't thinking when I posted this. Not sure how to move it though =P
Click the report button and say something like "Posted in wrong section, please move to API's and Utilities"

#16 ElvishJerricco

  • Members
  • 803 posts

Posted 01 February 2013 - 04:11 PM

View PostTheOriginalBIT, on 01 February 2013 - 04:06 PM, said:

View PostElvishJerricco, on 01 February 2013 - 04:05 PM, said:

Yea I wasn't thinking when I posted this. Not sure how to move it though =P
Click the report button and say something like "Posted in wrong section, please move to API's and Utilities"

Ah. Thanks

#17 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,451 posts

Posted 01 February 2013 - 04:22 PM

Moved to APIs and Utilities per user request.

#18 ElvishJerricco

  • Members
  • 803 posts

Posted 12 June 2013 - 04:37 AM

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

#19 Martin2789

  • Members
  • 5 posts

Posted 11 February 2014 - 03:37 PM

Nice Api you have there i get it to work with easy json like:
		{
			"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:

View PostLBPHacker, 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)
Thank you LBPHacker ;)

Edited by Martin2789, 14 February 2014 - 11:19 AM.


#20 daphee

  • Members
  • 8 posts
  • LocationGermany

Posted 20 June 2014 - 04:09 PM

Thank you for this nice lib. I've used it successfully on numerous projects.
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)"





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users