LibJson
LibJson sample test
(For java supported modules) This program is a JSON parser.
Test files
This sample program contains several files and the source files can be found under the /src directory.
Java LibJson Test Sample Application
The libJson Test sample application can be found here: LibJson.zip
Basic usage
Compile, load and run program using java environment.
Sample of JsonArray.java file:
package com.digi.json; import java.text.ParseException; import java.util.LinkedList; import java.util.List; public class JsonArray { private List<Object> _objects = new LinkedList<Object>(); public JsonArray() { } public JsonArray(String string) throws ParseException { JsonTokenizer tokenizer = new JsonTokenizer(string); fromTokenizer(tokenizer); } public JsonArray(JsonTokenizer tokenizer) throws ParseException { fromTokenizer(tokenizer); } private void fromTokenizer(JsonTokenizer tokenizer) throws ParseException { if (!tokenizer.isNextTokenStartArray()) { throw new ParseException("Not an array!", -1); } // pop the open brace tokenizer.nextToken(); // check for empty array if (tokenizer.isNextTokenFinishArray()) { tokenizer.nextToken(); return; } while (true) { _objects.add(tokenizer.parseValue()); // if not comma, then it should've been a ']' if (!tokenizer.nextToken().equals(",")) { break; } } } public int size() { return _objects.size(); } public Object get(int index) { return _objects.get(index); } public Object get(String path) { List<String> dir = JsonTokenizer.parsePath(path); return get(dir); } protected Object get(List<String> directions) { if (directions.size() == 0) return this; String value = directions.remove(0); value = value.replace("[", ""); value = value.replace("]", ""); int item = Integer.parseInt(value); Object obj = _objects.get(item); if (obj instanceof JsonObject) { return ((JsonObject) obj).get(directions); } else if (obj instanceof JsonArray) { return ((JsonArray) obj).get(directions); } return obj; } public String getString(int index) { return (String) get(index); } public JsonObject getJsonObject(int index) { return (JsonObject) get(index); } public JsonArray getJsonArray(int index) { return (JsonArray) get(index); } public int getInt(int index) { return Integer.parseInt(getString(index)); } public long getLong(int index) { return Long.parseLong(getString(index)); } public float getFloat(int index) { return Float.parseFloat(getString(index)); } public double getDouble(int index) { return Double.parseDouble(getString(index)); } public Object set(int index, Object value) { while (index >= _objects.size()) _objects.add(null); return _objects.set(index, value); } protected void set(List<String> directions, Object value) throws ParseException { if (directions.size() == 0) throw new ParseException("Invalid path", 0); String item = directions.remove(0); item = item.replaceAll("\\[", ""); item = item.replaceAll("\\]", ""); int index = Integer.parseInt(item); if (directions.size() == 0) { set(index, value); } else { // already exists if (_objects.size() > index) { Object obj = _objects.get(index); if (obj instanceof JsonObject) { JsonObject jo = (JsonObject) obj; jo.put(directions, value); return; } else if (obj instanceof JsonArray) { JsonArray ja = (JsonArray) obj; String child = directions.get(0); if (child.equals("[]")) ja.add(directions, value); else ja.set(directions, value); return; } } else { String child = directions.get(0); if (child.startsWith("[") && child.endsWith("]")) { JsonArray ja = new JsonArray(); set(index, ja); if (child.equals("[]")) ja.add(directions, value); else ja.set(directions, value); return; } else { JsonObject jo = new JsonObject(); set(index, jo); jo.put(directions, value); return; } } } } public boolean add(Object value) { return _objects.add(value); } public void add(int index, Object value) { _objects.add(index, value); } protected void add(List<String> directions, Object value) throws ParseException { if (directions.size() == 0) throw new ParseException("Invalid path", 0); String item = directions.remove(0); if (!item.equals("[]")) throw new ParseException("This should never happen!", 0); if (directions.size() == 0) { add(value); } else { String child = directions.get(0); if (child.startsWith("[") && child.endsWith("]")) { JsonArray ja = new JsonArray(); _objects.add(ja); if (child.equals("[]")) ja.add(directions, value); else ja.set(directions, value); return; } else { JsonObject jo = new JsonObject(); _objects.add(jo); jo.put(directions, value); return; } } } public Object remove(int index) { return _objects.remove(index); } protected Object remove(List<String> directions) throws ParseException { if (directions.size() == 0) throw new ParseException("Invalid path!", -1); String item = directions.remove(0); item = item.replaceAll("\\[", ""); item = item.replaceAll("\\]", ""); int index = Integer.parseInt(item); if (directions.size() == 0) { return remove(index); } else { Object obj = get(index); if (obj instanceof JsonObject) { JsonObject jo = (JsonObject) obj; return jo.remove(directions); } else if (obj instanceof JsonArray) { JsonArray jo = (JsonArray) obj; return jo.remove(directions); } } return null; } public void clear() { _objects.clear(); } public String display() { return display(0).toString(); } protected StringBuilder display(int depth) { StringBuilder sb = new StringBuilder(); sb.append("[\n"); for (int i = 0; i < _objects.size(); i++) { Object obj = _objects.get(i); sb.append(JsonTokenizer.repeat(" ", depth + 1)); if (obj == null) { sb.append("null"); } else if (obj instanceof JsonObject) { sb.append(((JsonObject) obj).display(depth + 1)); } else if (obj instanceof JsonArray) { sb.append(((JsonArray) obj).display(depth + 1)); } else if (obj instanceof String) { sb.append(JsonTokenizer.escapeString((String) obj)); } else { sb.append(obj + ""); } if (i < _objects.size() - 1) { sb.append(","); } sb.append("\n"); } sb.append(JsonTokenizer.repeat(" ", depth)); sb.append("]"); return sb; } public StringBuilder toStringBuilder() { StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; i < _objects.size(); i++) { Object obj = _objects.get(i); if (obj == null) { sb.append("null"); } else if (obj instanceof JsonObject) { sb.append(((JsonObject) obj).toStringBuilder()); } else if (obj instanceof JsonArray) { sb.append(((JsonArray) obj).toStringBuilder()); } else if (obj instanceof String) { sb.append(JsonTokenizer.escapeString((String) obj)); } else { sb.append(obj + ""); } if (i < _objects.size() - 1) { sb.append(","); } } sb.append("]"); return sb; } public boolean has(int index) { if (index < _objects.size()) return true; return false; } protected boolean has(List<String> directions) throws ParseException { if (directions.size() == 0) throw new ParseException("Invalid path!", -1); String item = directions.remove(0); item = item.replaceAll("\\[", ""); item = item.replaceAll("\\]", ""); int index = Integer.parseInt(item); if (!has(index)) return false; else if (directions.size() == 0) return true; Object obj = get(index); if (obj instanceof JsonObject) { JsonObject jo = (JsonObject) obj; return jo.has(directions); } else if (obj instanceof JsonArray) { JsonArray ja = (JsonArray) obj; return ja.has(directions); } return false; } @Override public String toString() { return toStringBuilder().toString(); } }