You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/07/28 03:28:19 UTC

[dubbo] branch 3.0 updated: Expand on the common tools and methods of JSON (#10370)

This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 196374dca3 Expand on the common tools and methods of JSON (#10370)
196374dca3 is described below

commit 196374dca3f778d1767e3e84f85907e4241a2fe1
Author: chenziqiang666 <50...@users.noreply.github.com>
AuthorDate: Thu Jul 28 11:28:14 2022 +0800

    Expand on the common tools and methods of JSON (#10370)
    
    * extend JsonUtils
    
    * add license
---
 .../java/org/apache/dubbo/common/json/JSON.java    |  22 ++
 .../dubbo/common/json/impl/AbstractJSONImpl.java   | 238 +++++++++++++++++++++
 .../dubbo/common/json/impl/FastJsonImpl.java       |   3 +-
 .../apache/dubbo/common/json/impl/GsonImpl.java    |   3 +-
 4 files changed, 262 insertions(+), 4 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSON.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSON.java
index 5d6094c2b8..c4210c60cb 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSON.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSON.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.common.json;
 
 import java.lang.reflect.Type;
 import java.util.List;
+import java.util.Map;
 
 public interface JSON {
     boolean isSupport();
@@ -27,4 +28,25 @@ public interface JSON {
     <T> List<T> toJavaList(String json, Class<T> clazz);
 
     String toJson(Object obj);
+
+    List<?> getList(Map<String, ?> obj, String key);
+
+    List<Map<String, ?>> getListOfObjects(Map<String, ?> obj, String key);
+
+    List<String> getListOfStrings(Map<String, ?> obj, String key);
+
+    Map<String, ?> getObject(Map<String, ?> obj, String key);
+
+    Double getNumberAsDouble(Map<String, ?> obj, String key);
+
+    Integer getNumberAsInteger(Map<String, ?> obj, String key);
+
+    Long getNumberAsLong(Map<String, ?> obj, String key);
+
+    String getString(Map<String, ?> obj, String key);
+
+    List<Map<String, ?>> checkObjectList(List<?> rawList);
+
+    List<String> checkStringList(List<?> rawList);
+
 }
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/AbstractJSONImpl.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/AbstractJSONImpl.java
new file mode 100644
index 0000000000..1d5fccb4b7
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/AbstractJSONImpl.java
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.json.impl;
+
+import org.apache.dubbo.common.json.JSON;
+
+import java.util.List;
+import java.util.Map;
+
+public abstract class AbstractJSONImpl implements JSON {
+
+    @Override
+    public List<?> getList(Map<String, ?> obj, String key) {
+        assert obj != null;
+        assert key != null;
+        if (!obj.containsKey(key)) {
+            return null;
+        }
+        Object value = obj.get(key);
+        if (!(value instanceof List)) {
+            throw new ClassCastException(
+                String.format("value '%s' for key '%s' in '%s' is not List", value, key, obj));
+        }
+        return (List<?>) value;
+    }
+
+    /**
+     * Gets a list from an object for the given key, and verifies all entries are objects.  If the key
+     * is not present, this returns null.  If the value is not a List or an entry is not an object,
+     * throws an exception.
+     */
+    @Override
+    public List<Map<String, ?>> getListOfObjects(Map<String, ?> obj, String key) {
+        assert obj != null;
+        List<?> list = getList(obj, key);
+        if (list == null) {
+            return null;
+        }
+        return checkObjectList(list);
+    }
+
+    /**
+     * Gets a list from an object for the given key, and verifies all entries are strings.  If the key
+     * is not present, this returns null.  If the value is not a List or an entry is not a string,
+     * throws an exception.
+     */
+    @Override
+    public List<String> getListOfStrings(Map<String, ?> obj, String key) {
+        assert obj != null;
+        List<?> list = getList(obj, key);
+        if (list == null) {
+            return null;
+        }
+        return checkStringList(list);
+    }
+
+    /**
+     * Gets an object from an object for the given key.  If the key is not present, this returns null.
+     * If the value is not a Map, throws an exception.
+     */
+    @SuppressWarnings("unchecked")
+    @Override
+    public Map<String, ?> getObject(Map<String, ?> obj, String key) {
+        assert obj != null;
+        assert key != null;
+        if (!obj.containsKey(key)) {
+            return null;
+        }
+        Object value = obj.get(key);
+        if (!(value instanceof Map)) {
+            throw new ClassCastException(
+                String.format("value '%s' for key '%s' in '%s' is not object", value, key, obj));
+        }
+        return (Map<String, ?>) value;
+    }
+
+    /**
+     * Gets a number from an object for the given key.  If the key is not present, this returns null.
+     * If the value does not represent a double, throws an exception.
+     */
+    @Override
+    public Double getNumberAsDouble(Map<String, ?> obj, String key) {
+        assert obj != null;
+        assert key != null;
+        if (!obj.containsKey(key)) {
+            return null;
+        }
+        Object value = obj.get(key);
+        if (value instanceof Double) {
+            return (Double) value;
+        }
+        if (value instanceof String) {
+            try {
+                return Double.parseDouble((String) value);
+            } catch (NumberFormatException e) {
+                throw new IllegalArgumentException(
+                    String.format("value '%s' for key '%s' is not a double", value, key));
+            }
+        }
+        throw new IllegalArgumentException(
+            String.format("value '%s' for key '%s' in '%s' is not a number", value, key, obj));
+    }
+
+    /**
+     * Gets a number from an object for the given key, casted to an integer.  If the key is not
+     * present, this returns null.  If the value does not represent an integer, throws an exception.
+     */
+    @Override
+    public Integer getNumberAsInteger(Map<String, ?> obj, String key) {
+        assert obj != null;
+        assert key != null;
+        if (!obj.containsKey(key)) {
+            return null;
+        }
+        Object value = obj.get(key);
+        if (value instanceof Double) {
+            Double d = (Double) value;
+            int i = d.intValue();
+            if (i != d) {
+                throw new ClassCastException("Number expected to be integer: " + d);
+            }
+            return i;
+        }
+        if (value instanceof String) {
+            try {
+                return Integer.parseInt((String) value);
+            } catch (NumberFormatException e) {
+                throw new IllegalArgumentException(
+                    String.format("value '%s' for key '%s' is not an integer", value, key));
+            }
+        }
+        throw new IllegalArgumentException(
+            String.format("value '%s' for key '%s' is not an integer", value, key));
+    }
+
+    /**
+     * Gets a number from an object for the given key, casted to an long.  If the key is not
+     * present, this returns null.  If the value does not represent a long integer, throws an
+     * exception.
+     */
+    @Override
+    public Long getNumberAsLong(Map<String, ?> obj, String key) {
+        assert obj != null;
+        assert key != null;
+        if (!obj.containsKey(key)) {
+            return null;
+        }
+        Object value = obj.get(key);
+        if (value instanceof Double) {
+            Double d = (Double) value;
+            long l = d.longValue();
+            if (l != d) {
+                throw new ClassCastException("Number expected to be long: " + d);
+            }
+            return l;
+        }
+        if (value instanceof String) {
+            try {
+                return Long.parseLong((String) value);
+            } catch (NumberFormatException e) {
+                throw new IllegalArgumentException(
+                    String.format("value '%s' for key '%s' is not a long integer", value, key));
+            }
+        }
+        throw new IllegalArgumentException(
+            String.format("value '%s' for key '%s' is not a long integer", value, key));
+    }
+
+    /**
+     * Gets a string from an object for the given key.  If the key is not present, this returns null.
+     * If the value is not a String, throws an exception.
+     */
+    @Override
+    public String getString(Map<String, ?> obj, String key) {
+        assert obj != null;
+        assert key != null;
+        if (!obj.containsKey(key)) {
+            return null;
+        }
+        Object value = obj.get(key);
+        if (!(value instanceof String)) {
+            throw new ClassCastException(
+                String.format("value '%s' for key '%s' in '%s' is not String", value, key, obj));
+        }
+        return (String) value;
+    }
+
+    /**
+     * Casts a list of unchecked JSON values to a list of checked objects in Java type.
+     * If the given list contains a value that is not a Map, throws an exception.
+     */
+    @SuppressWarnings("unchecked")
+    @Override
+    public List<Map<String, ?>> checkObjectList(List<?> rawList) {
+        assert rawList != null;
+        for (int i = 0; i < rawList.size(); i++) {
+            if (!(rawList.get(i) instanceof Map)) {
+                throw new ClassCastException(
+                    String.format("value %s for idx %d in %s is not object", rawList.get(i), i, rawList));
+            }
+        }
+        return (List<Map<String, ?>>) rawList;
+    }
+
+
+    /**
+     * Casts a list of unchecked JSON values to a list of String. If the given list
+     * contains a value that is not a String, throws an exception.
+     */
+    @SuppressWarnings("unchecked")
+    @Override
+    public List<String> checkStringList(List<?> rawList) {
+        assert rawList != null;
+        for (int i = 0; i < rawList.size(); i++) {
+            if (!(rawList.get(i) instanceof String)) {
+                throw new ClassCastException(
+                    String.format(
+                        "value '%s' for idx %d in '%s' is not string", rawList.get(i), i, rawList));
+            }
+        }
+        return (List<String>) rawList;
+    }
+
+}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/FastJsonImpl.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/FastJsonImpl.java
index b05fd7cd99..fff944e8aa 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/FastJsonImpl.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/FastJsonImpl.java
@@ -16,13 +16,12 @@
  */
 package org.apache.dubbo.common.json.impl;
 
-import org.apache.dubbo.common.json.JSON;
 import org.apache.dubbo.common.utils.ClassUtils;
 
 import java.lang.reflect.Type;
 import java.util.List;
 
-public class FastJsonImpl implements JSON {
+public class FastJsonImpl extends AbstractJSONImpl {
 
     @Override
     public boolean isSupport() {
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/GsonImpl.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/GsonImpl.java
index f202793e13..cbd76fe1b3 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/GsonImpl.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/GsonImpl.java
@@ -16,7 +16,6 @@
  */
 package org.apache.dubbo.common.json.impl;
 
-import org.apache.dubbo.common.json.JSON;
 import org.apache.dubbo.common.utils.ClassUtils;
 
 import com.google.gson.Gson;
@@ -25,7 +24,7 @@ import com.google.gson.reflect.TypeToken;
 import java.lang.reflect.Type;
 import java.util.List;
 
-public class GsonImpl implements JSON {
+public class GsonImpl extends AbstractJSONImpl {
     // weak reference of com.google.gson.Gson, prevent throw exception when init
     private volatile Object gsonCache = null;