Sunday, October 30, 2011

Class generator out of JSON definitions

This is my first tool I like to offer. I started to write a Plasma Widget (Plasmoids) which is able to show my streams of Google+ on my Desktop. Unfortunately Google does not have an API for C++. It would be possible to write the Plasmoids in other languages like Python but because my knowledge of Python is very poor I decided to use C++. The data from Google+ is provided as JSON objects. Therefore I needed a compiler which is able to generates classes directly from the definition from Google+. For example the definition for a comment looks like that:

{
  "kind": "plus#comment",
  "id": string,
  "published": datetime,
  "updated": datetime,
  "actor": {
    "id": string,
    "displayName": string,
    "url": string,
    "image": {
      "url": string
    }
  },
  "verb": "post",
  "object": {
    "objectType": "comment",
    "content": string
  },
  "selfLink": string,
  "inReplyTo": [
    {
      "id": string,
      "url": string
    }
  ]
}

The tool is now able to create objects out of this definition and of course out of all other definitions similar to that. It is possible to generate classes for Qt and Java. It should be easy to extend this to other languages without very much effort.
The tool uses JavaCC to parse the JSON definition.

If something has to be changed on the grammar rules, I recommend the Eclipse plugin for JavaCC which one can find at http://eclipse-javacc.sourceforge.net/ If one just needs to add other languages there is no need for JavaCC and all changes can be done directly in the Java sources.

To use the tool, download the jar file from http://141.3.80.191/~mehrwald/JSONClassGenerator.jar
In the command line type:
java -jar JSONClassGenerator.jar <FileToParse> <ObjectName> <PathToSave> (Qt|Java) [<FileToParse> <ObjectName> <PathToSave> (Qt|Java)...]

If one needs to change something or add another language please find the sources at http://141.3.80.191/~mehrwald/JSONClassGenerator_src.zip

I would appreciate all sorts of comments to this tool. If you have questions do not hesitate to ask me.

4 comments:

  1. C:\Users\i064196\Downloads>java -jar JSONClassGenerator.jar input.json ObjectName ./ Java
    Exception in thread "main" java.lang.UnsupportedClassVersionError: mmehrwald/GooglePlusJSONClassGenerator : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: mmehrwald.GooglePlusJSONClassGenerator. Program will exit.

    ReplyDelete
    Replies
    1. I do not know what exactly went wrong but my first suggestion is to take the source code and compile it for your actuall VM.

      Delete
  2. Your code is built in Java 1.7. Its not backward compatible to 1.6.

    ReplyDelete
    Replies
    1. Yes, you are right but you can compile the code yourself. It should not contain any structures from 1.7.

      Delete