You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by ts...@apache.org on 2017/01/03 16:51:58 UTC

[6/7] wicket git commit: WICKET-6287 Switch from json.org to open-json - 7.x

WICKET-6287 Switch from json.org to open-json - 7.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/6b1d53b1
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/6b1d53b1
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/6b1d53b1

Branch: refs/heads/wicket-7.x
Commit: 6b1d53b17a0aa77a8b131fd161e12e08d335a251
Parents: 3dacec4
Author: Tobias Soloschenko <ts...@apache.org>
Authored: Tue Dec 6 08:57:39 2016 +0100
Committer: Tobias Soloschenko <ts...@apache.org>
Committed: Tue Jan 3 17:50:52 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/6b1d53b1/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/6b1d53b1/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/6b1d53b1/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() {