Skip to content

Commit

Permalink
readJsonValue
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Nov 20, 2023
1 parent aa7ebf5 commit 60d9d63
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 128 deletions.
80 changes: 34 additions & 46 deletions rxlib/src/main/java/org/rx/core/Sys.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,27 @@ public static String cacheKey(String region, Object... args) {
}

//region json
public static <T> T readJsonValue(@NonNull Map<String, ?> json, String path,

/**
* @param json
* @param path
* @param childSelect !Reflects.isBasicType(cur.getClass())
* @param throwOnEmptyChild
* @param <T>
* @return
*/
public static <T> T readJsonValue(@NonNull Object json, String path,
BiFunc<Object, ?> childSelect,
boolean throwOnEmptyChild) {
Object child = json.get(path);
if (child != null) {
if (childSelect != null) {
child = childSelect.apply(child);
if (json instanceof Map) {
Map<String, Object> jObj = (Map<String, Object>) json;
Object cur = jObj.get(path);
if (cur != null) {
if (childSelect != null) {
cur = childSelect.apply(cur);
}
return (T) cur;
}
return (T) child;
}

Object cur = json;
Expand All @@ -528,54 +540,31 @@ public static <T> T readJsonValue(@NonNull Map<String, ?> json, String path,
continue;
}

cur = visitJson(cur, path, i, c, buf, max, childSelect, throwOnEmptyChild);
cur = visitJson(cur, path, i, c, buf.isEmpty() ? null : buf.toString(), max, childSelect, throwOnEmptyChild);
buf.setLength(0);
}
if (!buf.isEmpty()) {
cur = visitJson(cur, path, i, objKey, buf, max, childSelect, throwOnEmptyChild);
cur = visitJson(cur, path, i, objKey, buf.toString(), max, childSelect, throwOnEmptyChild);
}
return (T) cur;

// String[] paths = Strings.split(path, ".");
// if (paths.length == 0) {
// return null;
// }
//
// int last = paths.length - 1;
// Map<String, ?> tmp = json;
// for (int i = 0; i < last; i++) {
// child = tmp.get(paths[i]);
// if (childSelect != null) {
// child = childSelect.apply(child);
// }
// if ((tmp = as(child, Map.class)) == null) {
// if (throwOnEmptyChild) {
// throw new InvalidException("Get empty sub object by path {}", paths[i]);
// }
// return null;
// }
// }
// child = tmp.get(paths[last]);
// if (child != null
// && childSelect != null && !Reflects.isBasicType(child.getClass())) {
// child = childSelect.apply(child);
// }
// return (T) child;
}

static final char objKey = '.', arrBeginKey = '[', arrEndKey = ']';

static Object visitJson(Object cur, String path, AtomicInteger i, char c, StringBuilder buf,
static Object visitJson(Object cur, String path, AtomicInteger i, char c, String visitor,
int max, BiFunc<Object, ?> childSelect, boolean throwOnEmptyChild) {
String visitor = buf.toString();
if (cur instanceof Map) {
Map<String, ?> obj = (Map<String, ?>) cur;
cur = obj.get(visitor);
} 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);
if (visitor != null) {
if (cur instanceof Map) {
Map<String, ?> obj = (Map<String, ?>) cur;
cur = obj.get(visitor);
} else if (cur instanceof Iterable) {
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);
}
}
}

Expand All @@ -587,7 +576,6 @@ static Object visitJson(Object cur, String path, AtomicInteger i, char c, String
idxBuf.append(ic);
continue;
}
i.incrementAndGet();
break;
}
int idx;
Expand All @@ -614,7 +602,7 @@ static Object visitJson(Object cur, String path, AtomicInteger i, char c, String
throw new InvalidException("Object {} is not a array type with path {}", cur, visitor);
}
}
if (childSelect != null) {
if (cur != null && childSelect != null) {
cur = childSelect.apply(cur);
}
if (i.get() < max && cur == null) {
Expand Down
157 changes: 75 additions & 82 deletions rxlib/src/test/java/org/rx/util/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,104 +273,97 @@ public void other() {
System.out.println(Strings.subStringByByteLen(ce, 78));

String json = "{\n" +
" \"data\": [\n" +
" {\n" +
" \"peoNo\": \"229527513620001\",\n" +
" \"skuAndSpus\": [\n" +
" \"data\": {\n" +
" \"name\": \"张三\",\n" +
" \"age\": 10,\n" +
" \"mark\": [\n" +
" 0,1,2,3,4,5,6,7,8,9,10,11,\n" +
" [\n" +
" 2,\n" +
" 3,\n" +
" {\n" +
" \"resSku\": {\n" +
" \"appId\": \"GJHY\",\n" +
" \"createBy\": \"linxi\",\n" +
" \"customData\": \"\",\n" +
" \"enable\": 1,\n" +
" \"extendMap\": {\n" +
" \"inquiryDuration\": 0.5,\n" +
" \"activityId\": 35497,\n" +
" \"itemId\": 22486569827767537,\n" +
" \"couponIds\": [\n" +
" 9169767537\n" +
" ]\n" +
" },\n" +
" \"gmtCreate\": \"2023-10-31 14:54:07\",\n" +
" \"gmtUpdate\": \"2023-10-31 14:54:07\",\n" +
" \"intro\": \"社医付费问诊场景\",\n" +
" \"name\": \"复诊续方\",\n" +
" \"peoId\": 1548942540449906736,\n" +
" \"price\": 0,\n" +
" \"priceTypeCode\": \"GJHIS\",\n" +
" \"providerId\": \"1548952654418411543\",\n" +
" \"skuCode\": \"22488697939067537\",\n" +
" \"skuId\": 1719246353046908962,\n" +
" \"spuId\": 1542023119361933110,\n" +
" \"status\": 0,\n" +
" \"stock\": 9999,\n" +
" \"updateBy\": \"linxi\",\n" +
" \"version\": 0\n" +
" },\n" +
" \"resSpu\": {\n" +
" \"appId\": \"GJHY\",\n" +
" \"createBy\": \"多牌照管理员\",\n" +
" \"enable\": 1,\n" +
" \"extendMap\": {\n" +
" \"goodsNo\": 8400003,\n" +
" \"inquiryType\": 0,\n" +
" \"pricingMethod\": \"platform\",\n" +
" \"responseTime\": 2880,\n" +
" \"listingPermission\": \"user\",\n" +
" \"price\": 0,\n" +
" \"showPriceSwitch\": true,\n" +
" \"minPrice\": 0,\n" +
" \"serviceDuration\": 2880,\n" +
" \"imUserPermission\": \"1\",\n" +
" \"maxPrice\": 0,\n" +
" \"iconUrl\": \"https://gj-prod-1256038144.cos.ap-beijing.myqcloud.com/common/1619404699492/video.png\",\n" +
" \"showUpLoSwitch\": true,\n" +
" \"showService\": true\n" +
" },\n" +
" \"gmtCreate\": \"2023-06-09 11:34:25\",\n" +
" \"gmtUpdate\": \"2023-11-09 15:07:46\",\n" +
" \"intro\": \"社医付费问诊场景\",\n" +
" \"name\": \"复诊续方\",\n" +
" \"serviceCode\": \"A006\",\n" +
" \"spuCategory\": \"1\",\n" +
" \"spuCategoryName\": \"图文问诊\",\n" +
" \"spuCode\": \"38792\",\n" +
" \"spuId\": 1542023119361933110,\n" +
" \"spuOrgCategory\": 1542348617615015958,\n" +
" \"status\": 0,\n" +
" \"updateBy\": \"卢昭\",\n" +
" \"version\": 0\n" +
" }\n" +
" \"name\": \"李四\",\n" +
" \"mark\": [\n" +
" 0,\n" +
" [\n" +
" 1\n" +
" ]\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" ],\n" +
" \"status\": 0\n" +
" \"arr\": [0,1]" +
" ]\n" +
" },\n" +
" \"status\": 200\n" +
"}";
JSONObject jObj = toJsonObject(json);
Object v;
v = Sys.readJsonValue(jObj, "data", null, true);
// v = Sys.readJsonValue(jObj, "data", null, true);
// System.out.println(v);
// assert v instanceof JSONObject;
//
// v = Sys.readJsonValue(jObj, "data.mark", null, true);
// System.out.println(v);
// assert v instanceof JSONArray;
//
// v = Sys.readJsonValue(jObj, "status", null, true);
// System.out.println(v);
// assert eq(v, 200);
//
// v = Sys.readJsonValue(jObj, "data.name", null, true);
// System.out.println(v);
// assert eq(v, "张三");
//
// v = Sys.readJsonValue(jObj, "data.mark[1]", null, true);
// System.out.println(v);
// assert eq(v, 1);
//
// v = Sys.readJsonValue(jObj, "data.mark[11]", null, true);
// System.out.println(v);
// assert eq(v, 11);
//
// v = Sys.readJsonValue(jObj, "data.mark[12][1]", null, true);
// System.out.println(v);
// assert eq(v, 3);

v = Sys.readJsonValue(jObj, "data.mark[12][2].name", null, true);
System.out.println(v);
assert v instanceof JSONArray;
assert eq(v, "李四");

v = Sys.readJsonValue(jObj, "data[0].peoNo", null, true);
v = Sys.readJsonValue(jObj, "data.mark[12][2].mark[1][0]", null, true);
System.out.println(v);
assert eq(v, "229527513620001");
assert eq(v, 1);

v = Sys.readJsonValue(jObj, "data[0].skuAndSpus", null, true);
JSONArray jArr = toJsonArray("[\n" +
" 0,1,2,3,4,5,6,7,8,9,10,11,\n" +
" [\n" +
" 2,\n" +
" 3,\n" +
" {\n" +
" \"name\": \"李四\",\n" +
" \"mark\": [\n" +
" 0,\n" +
" [\n" +
" 1\n" +
" ]\n" +
" ]\n" +
" }\n" +
" ]\n" +
"]");
v = Sys.readJsonValue(jArr, "[1]", null, true);
System.out.println(v);
assert v instanceof JSONArray;
assert eq(v, 1);

v = Sys.readJsonValue(jObj, "data[0].skuAndSpus[0].resSpu.createBy", null, true);
v = Sys.readJsonValue(jArr, "[12][0]", null, true);
System.out.println(v);
assert eq(v, "多牌照管理员");
assert eq(v, 2);

v = Sys.readJsonValue(jObj, "status", null, true);
v = Sys.readJsonValue(jArr, "[12][2].name", null, true);
System.out.println(v);
assert eq(v, 0);
assert eq(v, "李四");

v = Sys.readJsonValue(toJsonArray("[0,1]"),"[1]",null,true);
v = Sys.readJsonValue(jArr, "[12][2].mark[1][0]", null, true);
System.out.println(v);
assert eq(v, 1);
}

@Test
Expand Down

0 comments on commit 60d9d63

Please sign in to comment.