Saturday, December 21, 2013

JSON Configuration File ~ Notes

AWS uses JSON extensively for configuration as do many other modern platforms.

JSON is pretty straight forward.

http://json.org

Here are some JSON examples including what the examples look like in JSON compared to XML:

http://json.org/example

If you want some Java objects to parse JSON:

http://json.org/java/

This nifty class can convert XML to JSON and vice versa:

https://github.com/douglascrockford/JSON-java/blob/master/XML.java

There are no comments in JSON. Your comments are the data but you could create some generic comment field/value pair that your application ignores.

Google has a JSON style guide:

http://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml

JSON Simple is a JSON Java toolkit:

http://code.google.com/p/json-simple/

Nice example of parsing a JSON file with Java:

http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

Parse web request with line breaks:

        HttpResponse response; // some response object
        BufferedReader reader = new BufferedReader

            (new InputStreamReader(response.getEntity().getContent(), 
               "UTF-8"));
        StringBuilder builder = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        JSONTokener tokener = new JSONTokener(builder.toString());
        JSONArray finalResult = new JSONArray(tokener);


Without line breaks:

        HttpResponse response; // some response object
        BufferedReader reader = new BufferedReader

          (new InputStreamReader(response.getEntity().getContent(), 
            "UTF-8"));
        String json = reader.readLine();
        JSONTokener tokener = new JSONTokener(json);
        JSONArray finalResult = new JSONArray(tokener);