JSON refactoring

libgdx has its own JSON parser, emitter, and reflection-based object serialization. I’ve never been happy with the parsing results being OrderedMaps and Arrays because it is so clunky to iterate over the data and get out the values you want. At one point I had a JsonMap and JsonArray classes, but it was too much of a mess. Recently I worked with cJSON, a JSON parser for C, and I liked the simplicity of its DOM. I was inspired have committed something similar for libgdx.

There is a new class called JsonValue. It is a single container that represents a JSON object, array, string, double, long, boolean, or null. You might think it strange that double and long are in the list since all numbers in JSON are floats, but these data types allow us to use JSON for object serialization without losing data. Anyway, JsonValue provides methods to get the value out as a specific type, pretty printing, and iteration. No more casting, pointy brackets, and verbose map iteration. Children are a doubly linked list and iterated this way:

JsonValue map = ...;
for (JsonValue entry = map.child(); entry != null; entry = entry.next())
    System.out.println(entry.name() + " = " + entry.asString());

Arrays and objects are treated the same, they both simply have children. The children values of an object can use name() to get the object key.

Convenience methods are provided to get the value or children of a child. Eg, getString(“name”) returns the value of the key “name” as a string. If not found, it throws an exception. With getString(“name”, “defaultValue”), the second parameter is returned if not found, instead of throwing an exception. The get(“name”) method returns a child as a JsonValue, or null if not found. require(“name”) does the same but throws an exception if not found. These methods remove the need for null checking while still allowing you to write code that fails properly for unexpected input.

So what does this mean for your code that uses libgdx’s JSON classes? Only reading JSON is affected. If you use JsonReader, you’ll get back a JsonValue instead of an OrderedMap. If you use the Json class for object serialization, you won’t be affected unless you were parsing into OrderedMaps. The Json.Serializer and Json.Serializable have changed slightly. My apologies if you have to make changes! The JsonValue class is fully documented and hopefully any changes you have to make are minor. GWT emulation has been updated. If you encounter bugs, please file an issue on the tracker and it will be fixed post-haste!

The libgdx JSON stuff is also available as JsonBeans for use outside of libgdx. The next thing I’ll do is allow KryoNet to use it for a human readable serialization format.

-Nate

The curious case of Kilobolt using my book’s code

Update: James Cho from Kilobolt contacted me via e-mail. We had a very lengthy discussion, here’s the entire conversation, start reading at the bottom. James agreed to put up a link to the book. To those sending hate-mail to Kilobolt: please stop, that’s my job, and my job only. Our community is better than this, and your thoughts are better expressed in the comments below and on the forums. I leave the original post intact so you can judge how much of an ass i made myself look like 🙂

So, a few days ago i found Kilobolt’s “Android Game Development Tutorial”. The tutorial is currently the #1 search result on Google for the terms “android game development tutorial”.

Obviously, the code on that page is my book’s code. While the author states that he improve the framework, all he really did was fuck up the formatting, and add modifications that will ensure that the game will run horribly on any mid-level device. The tutorials explain nothing, hand waving via phrases like “you don’t have to understand this”.

That’s fine. The code is Apache 2, so legally all is well. If people want to learn from such a poor resource as this tutorial, that’s cool with me as well. If you want the real deal, click on the book image in the top right corner of this page. Or find a backup copy on the internet. The code is here.

What’s a bit funky is that the tutorial pages are full of ads. They are everywhere. Fair game, the code is Apache 2, but it leaves a very bad taste in my mouth.

Here’s what’s not fine. I wrote a friendly comment on one of the tutorial pages. That was deleted/not approved. After that, the author appearantly shit his pants a little and added the paragraph about the code being based on my book. Without linking to my book. I tried to contact the author via e-mail, twice, but never got a reply.

Now, just to make sure i get my message across properly: i love when people use my stuff to create awesome things, be they commercial or non-commercial. I have no problem with not getting a “cut”. I do have a problem with leeches like Kilobolt, that take the hard work of others, sell it as their own, not even giving a single bit of credit where its due, and monetizing it heavily. I also have a problem with poor tutorials, that explain nothing, give terrible advice in form of horrible code, while making the reader think they are learning something worth while.

It kinda sucks that things like this happen. Again, form a legal perspective it’s fair game, and i do not complain that someone’s using OSS code for commercial endevours. However, the way Kilobolt is doing this (no credit, then a little credit, no replies to e-mails, censoring of posts) is disgusting. The book was one of the few ways i try to make a bit of money from libgdx and all the other gaming related things i put out there for free. Seeing it being ripped off like this doesn’t evoke the greatest feelings.

Here’s a link to the “Android Game Development Workshop” i gave a while ago. It contains extensive slides explaining everything, and comes with full source-code. Do yourself a favor and use that as a resource, instead of this horrible money grab by Kilobolt.