Thursday, February 28, 2013

Simple Java Swing JSON formatter

Most of web developers that are using AJAX to retrieve data from the server will be using the JSON lightweight format (instead of XML) to encode the data. To help them with this, the web development consoles in all web browsers are capable to show a hierarchical view of received JSON data, and this is usually fine, but when the size of the data grows or its hierarchy becomes complicated, you need to copy the data out to study it. More specifically, I usually need to compare the differences in the responses of different calls to the server.

The problem here is that JSON data is generally encoded in a very long string without line breaks, so you must format the JSON first. Many online JSON formatters exist, but I just needed a simple non-remote tool to format large JSON strings, leaving each element in its own line and ordering the properties of the objects, so I wrote a little Java Swing program, JsonFormatter.java, helped by the json-simple Java toolkit. To use this program you need to download the json-simple JAR file in order to compile and run the program (see below), and do Ctrl+V/Ctrl+A/Ctrl+C to process the JSON text, along with a text editor capable of doing diff comparisons, like Vim. Use it!

To compile this program using the javac compiler, after putting the json-simple JAR file in the same directory (for example the 1.1.1 version), type: javac -cp json-simple-1.1.1.jar JsonFormatter.java

Then you could build a JAR file to run it easily, just typing: jar cfm json-formatter.jar MANIF-ADD.MF JsonFormatter*.class, where the MANIF-ADD.MF is a text file with the following two lines (ensuring that last line ends with a return):
Main-Class: JsonFormatter
Class-Path: json-simple-1.1.1.jar


After that, you only need the two JAR files, and to run it, if the double click doesn't work, you can type: java -jar json-formatter.jar

Sunday, January 13, 2013

Modern 10 PRINT ASCII-Art labyrinth generator

After reading a Slashdot post about the 10 PRINT one-liner maze generator, I spent many days thinking about how a simple random sequence of / and \ could make those labyrinths.
Anyone who tried to generate labyrinths from a program knows that it isn't an easy task, so I immediately wrote a program to test if that algorithm could be so good, and I got this output:
/\////\\//\/\//\\\\\/\//\\
\////\\\///\/\////\/\\/\/\
/\\/\\\///\/\/\///\/\/\/\\
/\/\\\\\//\/\\\\/\\\\//\\\
///\/\\\/\\///\/\/////////
/\//\/\//\\/\///\\\/\/\/\/
\////\\\/\/\\\\\/\/////\\/
/\//\/\/////\\//\/\\///\\\
\\\\///\\/\/\/\/\\//\\/\\\
///\\/\\//\\\\/\///\\/\\\\
Well, that wasn't exactly what I expected, and I quickly realized that there was a problem with the fonts, so I made some tests trying to change many parameters of the text in a word processor, without success. Modern "\" and "/" characters are not designed for such effect! And the separation of lines was also a problem.

Anyway, I was not satisfied yet, so I studied how to design a program to represent, generate and print such maze using horizontal and vertical characters, learning in the way a lot about this thing. Today I would like to publish my little program 10print.c for anyone interested. This was the result:
   |  ___  |   |   |   |  _______  |   |  ___  |  __
 | | |  ___|___|___|___|___  |  ___| | | |  ___|___ 
 |___| |   |   |   |  _______|___  | | | | |  ___  |
___  | | | | | | | |___  |  _______| | |___| |  ___|
   | |___|___| | |_______|___  |   | | |  ___| |  __
 | |___  |   | | |  ___  |  ___| | |___| |  ___|___ 
 | |  ___| | |___|_______|_______| |   | |___  |  __
___|_______| |  _______________  | | | |_______|___ 
   |   |   | |___  |  ___________| | |_______  |   |
 | |___| | |___  | | |   |   |   | | |   |   | | | |
 | |  ___| |   | |___| | |___| | | | |___|___| | |__
 | | |   | |___|_______| |  ___| | |___  |  ___| |  
 |___| | | |   |   |  ___|_______| |  ___| |  ___|__
_______|___| | |___| |   |   |   | | |  ___|___  |  
   |  ___  | | |  ___| | |___| | | | | |   |   | |__
___| |   | |___| |  ___|___  | | | | | |___|___|___ 
  ___|___|_______| |   |   | |___|___| |  ___  |  __
 |  ___  |  _______| | | | |_______  | |_______|___ 
 | |   | | |  _______| | |_______  | | |  __________
___| | |___| |   |   | |_______  | |___|___________ 
Much better! I haven't read the 10 PRINT book, but I enjoyed a lot doing this. Thank you for give me such an interesting problem!