Stringtree – Simple JSON in Java

If you are like me, you probabely find that most of the Java implementations of JSON are rather bloated, especially for simple uses of JSON. Most of the time, I only need to convert a simple map to JSON, which I do using a simple class I had written

Some time ago, I came accross a very simple implementation of JSON-Java converstion. See Stringtree JSON .

It is a nice, simple implementation consisting of all of 2 classes. I have only used the JSONReader class so far, and it seems to do its job well.

I had to make a few changes to fix a couple of issues, though. In case anybody runs into the same issues, here are my changes (in all their Unified Diff glory :)) –

Bugfix for infinite loop if a value is null, true or false –
@@ -81,13 +81,17 @@
} else if (c == 't' && next() == 'r' && next() == 'u' && next() == 'e') {
ret = Boolean.TRUE;
+ next();
} else if (c == 'f' && next() == 'a' && next() == 'l' && next() == 's' && next() == 'e') {
ret = Boolean.FALSE;
+ next();
} else if (c == 'n' && next() == 'u' && next() == 'l' && next() == 'l') {
ret = null;
+ next();
} else if (Character.isDigit(c) || c == '-') {
ret = number();
}

– System.out.println(“token: ” + ret); // enable this line to see the token stream
+// System.out.println(“token: ” + ret); // enable this line to see the token stream
+
token = ret;
return ret;

In JSONWriter. To fix the null pointer error, if the field has no getter or is inaccessible otherwise –

String name = prop.getName();
Method accessor = prop.getReadMethod();
- Object value = accessor.invoke(object, (Object[])null);
- add(name, value);
+ if(accessor != null) {
+ Object value = accessor.invoke(object, (Object[])null);
+ add(name, value);
+ }

Making Auto-Login user script work on Flock

I use the Auto-Login user script at http://labs.beffa.org/greasemonkey/ to ease some of the pain of JEE development.

Recently, I decided to try out Flock for a few days, and the script didn’t work with it.

Thanks to Dive Into Greasemonkey, here is a quick fix.

This part of the script fails when running on Flock browser –

//to prevent submit to stupid site which put fake login / pass value
if (thisElement.value != thisElement.defaultValue) {
passfield = true;
thisElement.addEventListener(’keypress’, al_KeyPress, true);
}

For some reason, thisElement.value is always empty on Flock. On Firefox, it shows the actual value of the field.
Anyway, since I only use this script to login into a particular web page, I removed that if condition, and it works fine.