Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check child is json or not in zookeeper.it will be continue if not. #14166

Merged
merged 29 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f020aa6
check child is json or not.it will be continue if not.
walkinggo May 8, 2024
d6042b2
try to save code format problem.
walkinggo May 8, 2024
cd2cc51
child could be json array.so child could start with { or [.
walkinggo May 8, 2024
d95f757
fix the problem of it will continue some message to make zookeeper ou…
walkinggo May 8, 2024
96298d8
try to continue the string without warn.
walkinggo May 8, 2024
e215a95
format the code.
walkinggo May 8, 2024
b10ddc8
Merge branch '3.2' into 3.2
AlbumenJ May 9, 2024
30fe20a
Merge branch '3.2' into 3.2
CrazyHZM May 10, 2024
7e1fa5c
catch the exception of JSONException.
walkinggo May 11, 2024
1022ca5
Merge remote-tracking branch 'origin/3.2' into 3.2
walkinggo May 11, 2024
f991fe2
try to fix java.lang.NoClassDefFoundError because of JSONException.
walkinggo May 11, 2024
2fe1d10
add isJson method and implement it in JSON.
walkinggo May 16, 2024
0cfde5c
implement the checkJson method and use it in ZookeeperRegistry
walkinggo May 16, 2024
144e8ac
format the code.
walkinggo May 16, 2024
036d242
add some test case.
walkinggo May 17, 2024
5c5f64a
format the code.
walkinggo May 17, 2024
897b9b3
format the code.
walkinggo May 17, 2024
1b17fa8
format the code.
walkinggo May 17, 2024
d192864
format the code.
walkinggo May 17, 2024
318b959
format the code.
walkinggo May 17, 2024
a33bb10
format the code.
walkinggo May 17, 2024
16bc553
format the code.
walkinggo May 17, 2024
22a7d8b
format the code.
walkinggo May 17, 2024
1b65455
format the code.
walkinggo May 17, 2024
3dcca3a
add some information.
walkinggo May 17, 2024
44ea28c
format the code.
walkinggo May 17, 2024
7835f39
add human friendly comment.
walkinggo May 20, 2024
f5c4bc0
format the code.
walkinggo May 20, 2024
8012e9f
format the code.
walkinggo May 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
public interface JSON {
boolean isSupport();

boolean isJson(String json);

<T> T toJavaObject(String json, Type type);

<T> List<T> toJavaList(String json, Class<T> clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@
import java.lang.reflect.Type;
import java.util.List;

import com.alibaba.fastjson2.JSONValidator;
import com.alibaba.fastjson2.JSONWriter;

public class FastJson2Impl extends AbstractJSONImpl {

@Override
public boolean isJson(String json) {
JSONValidator validator = JSONValidator.from(json);
return validator.validate();
}

@Override
public <T> T toJavaObject(String json, Type type) {
return com.alibaba.fastjson2.JSON.parseObject(json, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@

public class FastJsonImpl extends AbstractJSONImpl {

@Override
public boolean isJson(String json) {
try {
Object obj = com.alibaba.fastjson.JSON.parse(json);
return obj instanceof com.alibaba.fastjson.JSONObject || obj instanceof com.alibaba.fastjson.JSONArray;
} catch (com.alibaba.fastjson.JSONException e) {
return false;
}
}

@Override
public <T> T toJavaObject(String json, Type type) {
return com.alibaba.fastjson.JSON.parseObject(json, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,25 @@
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

public class GsonImpl extends AbstractJSONImpl {
// weak reference of com.google.gson.Gson, prevent throw exception when init
private volatile Object gsonCache = null;

@Override
public boolean isJson(String json) {
try {
JsonElement jsonElement = JsonParser.parseString(json);
return jsonElement.isJsonObject() || jsonElement.isJsonArray();
} catch (JsonSyntaxException e) {
return false;
}
}

@Override
public <T> T toJavaObject(String json, Type type) {
return getGson().fromJson(json, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
Expand All @@ -31,6 +33,16 @@ public class JacksonImpl extends AbstractJSONImpl {

private volatile Object jacksonCache = null;

@Override
public boolean isJson(String json) {
try {
JsonNode node = objectMapper.readTree(json);
return node.isObject() || node.isArray();
} catch (JsonProcessingException e) {
return false;
}
}

@Override
public <T> T toJavaObject(String json, Type type) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ public static String getString(Map<String, ?> obj, String key) {
public static List<String> checkStringList(List<?> rawList) {
return getJson().checkStringList(rawList);
}

public static boolean checkJson(String json) {
return getJson().isJson(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,49 @@ void teardown() {
}
}

@Test
void testIsJson() {
JsonUtils.setJson(null);
// prefer use fastjson2
System.setProperty("dubbo.json-framework.prefer", "fastjson2");
Assertions.assertTrue(
JsonUtils.getJson().isJson("{\"title\":\"Java Programming\",\"author\":\"John Doe\",\"pages\":300}"));
Assertions.assertFalse(JsonUtils.getJson().isJson("This is not a JSON string"));
Assertions.assertTrue(
JsonUtils.getJson().isJson("[{\"title\":\"Java Programming\"}, {\"title\":\"Python Programming\"}]"));
System.clearProperty("dubbo.json-framework.prefer");

// prefer use fastjson
JsonUtils.setJson(null);
System.setProperty("dubbo.json-framework.prefer", "fastjson");
Assertions.assertTrue(
JsonUtils.getJson().isJson("{\"title\":\"Java Programming\",\"author\":\"John Doe\",\"pages\":300}"));
Assertions.assertFalse(JsonUtils.getJson().isJson("This is not a JSON string"));
Assertions.assertTrue(
JsonUtils.getJson().isJson("[{\"title\":\"Java Programming\"}, {\"title\":\"Python Programming\"}]"));
System.clearProperty("dubbo.json-framework.prefer");

// prefer use gson
JsonUtils.setJson(null);
System.setProperty("dubbo.json-framework.prefer", "gson");
Assertions.assertTrue(
JsonUtils.getJson().isJson("{\"title\":\"Java Programming\",\"author\":\"John Doe\",\"pages\":300}"));
Assertions.assertFalse(JsonUtils.getJson().isJson("This is not a JSON string"));
Assertions.assertTrue(
JsonUtils.getJson().isJson("[{\"title\":\"Java Programming\"}, {\"title\":\"Python Programming\"}]"));
System.clearProperty("dubbo.json-framework.prefer");

// prefer use jackson
JsonUtils.setJson(null);
System.setProperty("dubbo.json-framework.prefer", "jackson");
Assertions.assertTrue(
JsonUtils.getJson().isJson("{\"title\":\"Java Programming\",\"author\":\"John Doe\",\"pages\":300}"));
Assertions.assertFalse(JsonUtils.getJson().isJson("This is not a JSON string"));
Assertions.assertTrue(
JsonUtils.getJson().isJson("[{\"title\":\"Java Programming\"}, {\"title\":\"Python Programming\"}]"));
System.clearProperty("dubbo.json-framework.prefer");
}

@Test
void testGetJson1() {
Assertions.assertNotNull(JsonUtils.getJson());
Expand Down
walkinggo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
import org.apache.dubbo.common.utils.ConcurrentHashSet;
import org.apache.dubbo.common.utils.JsonUtils;
import org.apache.dubbo.common.utils.UrlUtils;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.support.CacheableFailbackRegistry;
Expand All @@ -46,6 +47,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.CHECK_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_ERROR_DESERIALIZE;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.REGISTRY_ZOOKEEPER_EXCEPTION;
import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.CONSUMERS_CATEGORY;
Expand Down Expand Up @@ -201,7 +203,15 @@ public void doSubscribe(final URL url, final NotifyListener listener) {
ChildListener zkListener = ConcurrentHashMapUtils.computeIfAbsent(
listeners, listener, k -> (parentPath, currentChildren) -> {
for (String child : currentChildren) {
child = URL.decode(child);
try {
child = URL.decode(child);
if (!(JsonUtils.checkJson(child))) {
throw new Exception("dubbo-admin subscribe " + child + " failed,beacause "
+ child + "is root path in " + url);
}
} catch (Exception e) {
logger.warn(PROTOCOL_ERROR_DESERIALIZE, "", "", e.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance log content here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should i output more information here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for some human friendly comment

}
if (!anyServices.contains(child)) {
anyServices.add(child);
subscribe(
Expand Down