diff --git a/JSONObject.java b/JSONObject.java index e28c9cd37..f5b5e2024 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -24,9 +24,7 @@ of this software and associated documentation files (the "Software"), to deal SOFTWARE. */ -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; +import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -256,6 +254,38 @@ public JSONObject(Map map) { } } + public JSONObject(File file) { + this(); + + BufferedReader br = null; + StringBuffer contents = new StringBuffer(); + try { + String sCurrentLine; + br = new BufferedReader(new FileReader(file)); + while ((sCurrentLine = br.readLine()) != null) { +// System.out.println(sCurrentLine); + contents.append(sCurrentLine); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (br != null) br.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + this.add(new JSONObject(contents.toString())); + } + + public void add(JSONObject rhs) + { + for (String key : rhs.keySet()) + { + this.put(key, rhs.get(key)); + } + } + /** * Construct a JSONObject from an Object using bean getters. It reflects on * all of the public methods of the object. For each of the methods with no @@ -487,6 +517,7 @@ public Object get(String key) throws JSONException { * @throws JSONException * if the value is not a Boolean or the String "true" or * "false". + * if an integer, follow the C convention */ public boolean getBoolean(String key) throws JSONException { Object object = this.get(key); @@ -496,8 +527,15 @@ public boolean getBoolean(String key) throws JSONException { return false; } else if (object.equals(Boolean.TRUE) || (object instanceof String && ((String) object) - .equalsIgnoreCase("true"))) { + .equalsIgnoreCase("true"))) { return true; + + } else if ( object instanceof Integer) + { + return ((Integer)object)!=0; + } else if ( object instanceof Double) + { + return ((Double)object)!=0; } throw new JSONException("JSONObject[" + quote(key) + "] is not a Boolean."); @@ -1692,4 +1730,47 @@ Writer write(Writer writer, int indentFactor, int indent) throw new JSONException(exception); } } + + //TODO: add to this logic + public boolean containsAll(JSONObject rhs) + { + if (!this.keySet().containsAll(rhs.keySet())) + { + return false; + } + + for(String key : this.keySet()) + { + if (!rhs.has(key)) { + // we've hit a key that is in LHS, but ont in RHS. + // this case is allowed, so we skip. + continue; + } + + Object lhs_val = this.get(key); + Object rhs_val = rhs.get(key); + Object lhs_class = lhs_val.getClass(); + Object rhs_class = rhs_val.getClass(); + + if (lhs_class != rhs_class) { + return false; + } else { + // pass + // TODO: recurse into JSONObjects + // TODO: recurse into JSONArrays + // TODO: break into type and compare strings + } + } + return true; + + } + //TODO: add to this logic + public boolean equals(JSONObject rhs) + { + // TODO: this is sub-optimal + boolean lhs_e = this.containsAll(rhs); + boolean rhs_e = rhs.containsAll(this); + + return this.containsAll(rhs) && rhs.containsAll(this); + } }