It is a sad fact but I don’t always get to pick the tools I use at work. Just today I felt like my colleague and I woke up in 1996 – back when java 1.6 was released.
The client wanted to keep track of all record changes in one of the fields – the history field. I am not sure how the other consultancy firm convinced them to do that instead of having either a log file or putting the logging information into a separate table.
The plan was to store all the record changes in the history field in JSON format. The JSON format is actually great for holding the information in an easy to use format but that is true only with a good parsing class.
That is where our problems began. We were given the task of dealing with JSON data but the boss said we couldn’t use anything that didn’t come standard with Java 1.6. This meant that we couldn’t use google’s solution GSON or any other external libraries.
We kicked a few ideas around before we decided we could use the StringTokenizer class to split up the JSON strings. Our solution works but only because there is only a single array of records.
We did some rudimentary parsing of the data, but it still felt like going back to pre-internet where you had to write every function yourself for formatting every little thing.
package my.json; import java.util.StringTokenizer; public class TestJson { public String mydata = "[{\"Date\":\"2017-10-20 16:55\",\"User\":\"Interface\",\"Action\":\"Create\",\"Type\":\"INTERFACE\",\"UserComment\":\"Item created\",\"Level\":\"info\",\"Details\":\"Item created\",\"NotificationStatus\":\"\",\"NotificationReason\":\"\",\"NotificationMessage\":\"\"},{\"Date\":\"2017-10-20 17:35\",\"User\":\"Interface\",\"Action\":\"email\",\"Type\":\"INTERFACE\",\"UserComment\":\"Sending\",\"Level\":\"error\",\"Details\":\"Item created\",\"NotificationStatus\":\"Notification Overdue\",\"NotificationReason\":\"Overdue\",\"NotificationMessage\":\"\"},{\"Date\":\"2017-10-20 17:41\",\"User\":\"Interface\",\"Action\":\"email\",\"Type\":\"INTERFACE\",\"UserComment\":\"Sending\",\"Level\":\"error\",\"Details\":\"Item created\",\"NotificationStatus\":\"Notification Overdue\",\"NotificationReason\":\"Overdue\",\"NotificationMessage\":\"\"}]"; public String newrec = "{\"Date\":\"2017-10-20 17:41\",\"User\":\"Interface\",\"Action\":\"email\",\"Type\":\"INTERFACE\",\"UserComment\":\"Sending\",\"Level\":\"error\",\"Details\":\"Item created\",\"NotificationStatus\":\"Notification Overdue\",\"NotificationReason\":\"Overdue\",\"NotificationMessage\":\"\"}"; public void printValue(int idx, String datapair) { StringTokenizer tok = new StringTokenizer(datapair, "\":"); String key = tok.nextToken(); String value = ""; if (tok.hasMoreTokens()) value = tok.nextToken(); key = (key + " ").substring(0,22); System.out.println(idx++ + " " + key + " " + value);; } public void printfields(String data) { System.out.println(data); StringTokenizer tok = new StringTokenizer(data, "[]{},"); if (tok.hasMoreElements() == false) return; int idx = 0; do { String str = tok.nextToken(); printValue(idx,str); idx++; } while (tok.hasMoreElements() == true); } public void process (String args[]) { StringTokenizer tok = new StringTokenizer(mydata, "}"); do { String str = tok.nextToken(); printfields(str); System.out.println("\n"); } while (tok.hasMoreElements() == true); } public int ItemCount(String data) { int retval = 0; if (data.length() == 0) return 0; data = data.replace("[", "").replace("]", ""); String parts[] = data.split("}"); retval = parts.length; return retval; } public String getItem(int item, String data) { String retval = ""; try { data = data.replace("[", "").replace("]", ""); if (data.subSequence(0, 1).equals(",")) data = data.substring(1); String parts[] = data.split("}"); if (parts[item].substring(0,1).equals(",")) retval = parts[item].substring(1) + "}"; else retval = parts[item] + "}"; } catch (ArrayIndexOutOfBoundsException ex) { retval = null; } return retval; } public String getItem(int item) { return getItem(item,mydata); } public int ItemCount() { return ItemCount(mydata); } public TestJson() { } public TestJson(String record) { mydata = record; } static public void main (String args[]) { TestJson x = new TestJson(); System.out.println(x.ItemCount()); System.out.println(x.ItemCount(x.newrec)); System.out.println(x.ItemCount("")); System.out.println(x.getItem(0)); System.out.println(x.getItem(1)); System.out.println(x.getItem(2)); System.out.println(x.getItem(3)); } }