From 80a3357db22e878c9120a850caa7f3268a23dd8a Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Tue, 21 Nov 2023 11:12:45 +0800 Subject: [PATCH] up rj --- rxlib/src/main/java/org/rx/core/Sys.java | 48 ++++++++++--------- rxlib/src/test/java/org/rx/util/TestUtil.java | 20 ++++++-- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/rxlib/src/main/java/org/rx/core/Sys.java b/rxlib/src/main/java/org/rx/core/Sys.java index 0f432808..ecf0714b 100644 --- a/rxlib/src/main/java/org/rx/core/Sys.java +++ b/rxlib/src/main/java/org/rx/core/Sys.java @@ -505,6 +505,9 @@ public static String cacheKey(String region, Object... args) { } //region json + public static T readJsonValue(Object json, String path) { + return readJsonValue(json, path, null, true); + } /** * @param json @@ -514,9 +517,8 @@ public static String cacheKey(String region, Object... args) { * @param * @return */ - public static T readJsonValue(@NonNull Object json, String path, - BiFunc childSelect, - boolean throwOnEmptyChild) { + public static T readJsonValue(@NonNull Object json, @NonNull String path, + BiFunc childSelect, boolean throwOnEmptyChild) { if (json instanceof Map) { Map jObj = (Map) json; Object cur = jObj.get(path); @@ -540,7 +542,7 @@ public static T readJsonValue(@NonNull Object json, String path, continue; } - cur = visitJson(cur, path, i, c, buf.isEmpty() ? null : buf.toString(), max, childSelect, throwOnEmptyChild); + cur = visitJson(cur, path, i, c, buf.toString(), max, childSelect, throwOnEmptyChild); buf.setLength(0); } if (!buf.isEmpty()) { @@ -553,17 +555,17 @@ public static T readJsonValue(@NonNull Object json, String path, static Object visitJson(Object cur, String path, AtomicInteger i, char c, String visitor, int max, BiFunc childSelect, boolean throwOnEmptyChild) { - if (visitor != null) { + if (!visitor.isEmpty()) { if (cur instanceof Map) { Map obj = (Map) cur; cur = obj.get(visitor); } else if (cur instanceof Iterable) { - System.out.println(cur); +// System.out.println(cur); } else { try { cur = Reflects.readField(cur, visitor); } catch (Throwable e) { - throw new InvalidException("Object {} is not a map type or not found field with path {}", cur, visitor, e); + throw new InvalidException("Object \"{}\" is not a map or not found field with path {}", cur, visitor, e); } } } @@ -583,23 +585,25 @@ static Object visitJson(Object cur, String path, AtomicInteger i, char c, String idx = Integer.parseInt(idxBuf.toString()); visitor = String.format("%s[%s]", visitor, idxBuf); } catch (Throwable e) { - throw new InvalidException("Index {} is not a int type", idxBuf, e); + throw new InvalidException("Index \"{}\" is not a number", idxBuf, e); } - if (cur instanceof Iterable) { - try { - cur = IterableUtils.get((Iterable) cur, idx); - } catch (IndexOutOfBoundsException e) { - throw new InvalidException("Array {} is index out of bounds with path {}", cur, visitor, e); - } - } else if (cur != null && cur.getClass().isArray()) { - try { - cur = Array.get(cur, idx); - } catch (ArrayIndexOutOfBoundsException e) { - throw new InvalidException("Array {} is index out of bounds with path {}", cur, visitor, e); + if (cur != null) { + if (cur instanceof Iterable) { + try { + cur = IterableUtils.get((Iterable) cur, idx); + } catch (IndexOutOfBoundsException e) { + throw new InvalidException("Array \"{}\" is index out of bounds with path {}", cur, visitor, e); + } + } else if (cur.getClass().isArray()) { + try { + cur = Array.get(cur, idx); + } catch (ArrayIndexOutOfBoundsException e) { + throw new InvalidException("Array \"{}\" is index out of bounds with path {}", cur, visitor, e); + } + } else { + throw new InvalidException("Object \"{}\" is not a array with path {}", cur, visitor); } - } else { - throw new InvalidException("Object {} is not a array type with path {}", cur, visitor); } } if (cur != null && childSelect != null) { @@ -607,7 +611,7 @@ static Object visitJson(Object cur, String path, AtomicInteger i, char c, String } if (i.get() < max && cur == null) { if (throwOnEmptyChild) { - throw new InvalidException("Get empty sub object by path {}", visitor); + throw new InvalidException("Get empty child by path {}", visitor); } return null; } diff --git a/rxlib/src/test/java/org/rx/util/TestUtil.java b/rxlib/src/test/java/org/rx/util/TestUtil.java index 805cd565..880d2fca 100644 --- a/rxlib/src/test/java/org/rx/util/TestUtil.java +++ b/rxlib/src/test/java/org/rx/util/TestUtil.java @@ -15,6 +15,7 @@ import org.rx.annotation.Subscribe; import org.rx.bean.*; import org.rx.core.*; +import org.rx.exception.InvalidException; import org.rx.test.UserStruct; import org.rx.third.guava.CaseFormat; @@ -349,21 +350,32 @@ public void other() { " }\n" + " ]\n" + "]"); - v = Sys.readJsonValue(jArr, "[1]", null, true); + v = Sys.readJsonValue(jArr, "[1]"); System.out.println(v); assert eq(v, 1); - v = Sys.readJsonValue(jArr, "[12][0]", null, true); + v = Sys.readJsonValue(jArr, "[12][0]"); System.out.println(v); assert eq(v, 2); - v = Sys.readJsonValue(jArr, "[12][2].name", null, true); + v = Sys.readJsonValue(jArr, "[12][2].name"); System.out.println(v); assert eq(v, "李四"); - v = Sys.readJsonValue(jArr, "[12][2].mark[1][0]", null, true); + v = Sys.readJsonValue(jArr, "[12][2].mark[1][0]"); System.out.println(v); assert eq(v, 1); + + try { + Sys.readJsonValue(jArr, "[12][2].mark2[1][0]"); + } catch (InvalidException e) { + e.printStackTrace(); + } + try { + Sys.readJsonValue(jArr, "[12][3].mark2[1][0]"); + } catch (InvalidException e) { + e.printStackTrace(); + } } @Test