Skip to content

Commit

Permalink
up rj
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Nov 21, 2023
1 parent 60d9d63 commit 80a3357
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
48 changes: 26 additions & 22 deletions rxlib/src/main/java/org/rx/core/Sys.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ public static String cacheKey(String region, Object... args) {
}

//region json
public static <T> T readJsonValue(Object json, String path) {
return readJsonValue(json, path, null, true);
}

/**
* @param json
Expand All @@ -514,9 +517,8 @@ public static String cacheKey(String region, Object... args) {
* @param <T>
* @return
*/
public static <T> T readJsonValue(@NonNull Object json, String path,
BiFunc<Object, ?> childSelect,
boolean throwOnEmptyChild) {
public static <T> T readJsonValue(@NonNull Object json, @NonNull String path,
BiFunc<Object, ?> childSelect, boolean throwOnEmptyChild) {
if (json instanceof Map) {
Map<String, Object> jObj = (Map<String, Object>) json;
Object cur = jObj.get(path);
Expand All @@ -540,7 +542,7 @@ public static <T> 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()) {
Expand All @@ -553,17 +555,17 @@ public static <T> T readJsonValue(@NonNull Object json, String path,

static Object visitJson(Object cur, String path, AtomicInteger i, char c, String visitor,
int max, BiFunc<Object, ?> childSelect, boolean throwOnEmptyChild) {
if (visitor != null) {
if (!visitor.isEmpty()) {
if (cur instanceof Map) {
Map<String, ?> obj = (Map<String, ?>) 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);
}
}
}
Expand All @@ -583,31 +585,33 @@ 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) {
cur = childSelect.apply(cur);
}
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;
}
Expand Down
20 changes: 16 additions & 4 deletions rxlib/src/test/java/org/rx/util/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 80a3357

Please sign in to comment.