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);
+ }