WICKET-6287 Switch from json.org to open-json - 6.x * Updated to open-json 1.3
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/6f764146 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/6f764146 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/6f764146 Branch: refs/heads/wicket-6.x Commit: 6f764146a007ab38686a7d54210db0fcdfc2a7ef Parents: 0ba87f0 Author: Tobias Soloschenko <[email protected]> Authored: Tue Dec 6 08:54:38 2016 +0100 Committer: Tobias Soloschenko <[email protected]> Committed: Tue Jan 3 17:54:47 2017 +0100 ---------------------------------------------------------------------- .../org/apache/wicket/ajax/json/JSONArray.java | 33 +++++++++++++++++++- .../org/apache/wicket/ajax/json/JSONObject.java | 23 +++++++++++++- .../apache/wicket/ajax/json/JSONStringer.java | 31 ++++++++---------- 3 files changed, 67 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/6f764146/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONArray.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONArray.java b/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONArray.java index 914f8a6..65279d8 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONArray.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONArray.java @@ -65,7 +65,7 @@ public class JSONArray { * inconsistent state. */ /* Accept a raw type for API compatibility */ - public JSONArray(Collection copyFrom) { + public JSONArray(Collection<?> copyFrom) { this(); if (copyFrom != null) { for (Object aCopyFrom : copyFrom) { @@ -178,6 +178,20 @@ public class JSONArray { } /** + * Appends {@code value} wrapped by {@link JSONArray} to the end of this array. + * + * @param value any collection. + * @return this array. + */ + public JSONArray put(Collection<?> value) { + if (value == null) { + return put((Object)null); + } + values.add(new JSONArray(value)); + return this; + } + + /** * Appends {@code value} to the end of this array. * * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, @@ -263,6 +277,23 @@ public class JSONArray { } /** + * Sets the value at {@code index} to {@code value} wrapped into {@link JSONArray}, + * null padding this array to the required length if necessary. If a value already + * exists at {@code index}, it will be replaced. + * + * @param index Where to put the value. + * @param value The value to set. + * @return this array. + * @throws JSONException Should never actually happen. + */ + public JSONArray put(int index, Collection<?> value) throws JSONException { + if (value == null) { + return put(index, (Object)null); + } + return put(index, new JSONArray(value)); + } + + /** * Sets the value at {@code index} to {@code value}, null padding this array * to the required length if necessary. If a value already exists at {@code * index}, it will be replaced. http://git-wip-us.apache.org/repos/asf/wicket/blob/6f764146/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONObject.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONObject.java b/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONObject.java index 0640a17..67dfa75 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONObject.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONObject.java @@ -16,6 +16,10 @@ package org.apache.wicket.ajax.json; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -23,6 +27,7 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.TreeMap; // Note: this class was written without inspecting the non-free org.json sourcecode. @@ -203,6 +208,22 @@ public class JSONObject { } } + public JSONObject(Object bean) throws IntrospectionException, InvocationTargetException, IllegalAccessException { + this(propertiesAsMap(bean)); + } + + private static Map<String, Object> propertiesAsMap(Object bean) + throws IntrospectionException, IllegalAccessException, InvocationTargetException { + PropertyDescriptor[] properties = Introspector.getBeanInfo(bean.getClass(), Object.class) + .getPropertyDescriptors(); + Map<String, Object> props = new TreeMap<String, Object>(); + for (int i = 0; i < properties.length; i++) { + PropertyDescriptor propertyDescriptor = properties[i]; + props.put(propertyDescriptor.getDisplayName(), propertyDescriptor.getReadMethod().invoke(bean)); + } + return props; + } + /** * Returns the number of name/value mappings in this object. * @@ -792,7 +813,7 @@ public class JSONObject { public JSONArray names() { return nameValuePairs.isEmpty() ? null - : new JSONArray(new ArrayList<Object>(nameValuePairs.keySet())); + : new JSONArray(new ArrayList<String>(nameValuePairs.keySet())); } /** http://git-wip-us.apache.org/repos/asf/wicket/blob/6f764146/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONStringer.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONStringer.java b/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONStringer.java index 5aa8f5a..431af66 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONStringer.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/json/JSONStringer.java @@ -258,9 +258,12 @@ public class JSONStringer { } else { // Hack to make it possible that the value is not surrounded by quotes. (Used for JavaScript function calls) // Example: { "name": "testkey", "value": window.myfunction() } - if(value.getClass().getSimpleName().indexOf("JsonFunction") != -1){ - string(value.toString(), false); - }else{ + if (value.getClass().getSimpleName().contains("JsonFunction")) { + // note that no escaping of quotes (or anything else) is done in this case. + // that is fine because the only way to get to this point is to + // explicitly put a special kind of object into the JSON data structure. + out.append(value); + } else { string(value.toString()); } } @@ -318,17 +321,11 @@ public class JSONStringer { } private void string(String value) { - string(value, true); - } - - private void string(String value, boolean surroundingQuotes) { - if(surroundingQuotes){ - out.append("\""); - } - char previousChar = 0; + out.append("\""); char currentChar = 0; + for (int i = 0, length = value.length(); i < length; i++) { - previousChar = currentChar; + char previousChar = currentChar; currentChar = value.charAt(i); /* @@ -340,12 +337,12 @@ public class JSONStringer { switch (currentChar) { case '"': case '\\': - out.append("\\").append(currentChar); + out.append('\\').append(currentChar); break; case '/': - // It is not required to escape /, but to place it in script tags "</" has to be escaped - if(previousChar == '<'){ + // it makes life easier for HTML embedding of javascript if we escape </ sequences + if (previousChar == '<') { out.append('\\'); } out.append(currentChar); @@ -381,9 +378,7 @@ public class JSONStringer { } } - if(surroundingQuotes){ - out.append("\""); - } + out.append("\""); } private void newline() {
