Skip to content

Commit 92bb463

Browse files
committed
fix wildcard on invalid path
1 parent 142fb7e commit 92bb463

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

src/main/java/com/jsoniter/any/ArrayAny.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public Any get(Object[] keys, int idx) {
7070
if (isWildcard(key)) {
7171
ArrayList<Any> result = new ArrayList<Any>();
7272
for (Any element : val) {
73-
result.add(element.get(keys, idx + 1));
73+
Any mapped = element.get(keys, idx + 1);
74+
if (mapped.valueType() != ValueType.INVALID) {
75+
result.add(mapped);
76+
}
7477
}
7578
return Any.wrapAnyList(result);
7679
}

src/main/java/com/jsoniter/any/ArrayLazyAny.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ public Any get(Object[] keys, int idx) {
7676
fillCache();
7777
ArrayList<Any> result = new ArrayList<Any>();
7878
for (Any element : cache) {
79-
result.add(element.get(keys, idx+1));
79+
Any mapped = element.get(keys, idx + 1);
80+
if (mapped.valueType() != ValueType.INVALID) {
81+
result.add(mapped);
82+
}
8083
}
8184
return Any.wrapAnyList(result);
8285
}

src/main/java/com/jsoniter/any/NotFoundAny.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ class NotFoundAny extends Any {
1212
private final JsonException exception;
1313

1414
public NotFoundAny(Object[] keys, int idx, Object obj) {
15-
this.exception = new JsonException(String.format("Value not found: failed to get path %s, because #%s %s not found in %s",
15+
this.exception = new JsonException(String.format("Value not found: failed to get path %s, because #%s section of the path ( %s ) not found in %s",
1616
Arrays.toString(keys), idx, keys[idx], obj));
1717
}
1818

1919
public NotFoundAny(int index, Object obj) {
20-
this.exception = new JsonException(String.format("Value not found: failed to get index %s, because %s not found in %s",
21-
index, index, obj));
20+
this.exception = new JsonException(String.format("Value not found: failed to get index %s from %s",
21+
index, obj));
2222
}
2323

2424
public NotFoundAny(Object key, Object obj) {
25-
this.exception = new JsonException(String.format("Value not found: failed to get key %s, because %s not found in %s",
26-
key, key, obj));
25+
this.exception = new JsonException(String.format("Value not found: failed to get key %s from %s",
26+
key, obj));
2727
}
2828

2929
@Override

src/main/java/com/jsoniter/any/ObjectAny.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public Any get(Object[] keys, int idx) {
7070
if (isWildcard(key)) {
7171
HashMap<String, Any> result = new HashMap<String, Any>();
7272
for (Map.Entry<String, Any> entry : val.entrySet()) {
73-
result.put(entry.getKey(), entry.getValue().get(keys, idx + 1));
73+
Any mapped = entry.getValue().get(keys, idx + 1);
74+
if (mapped.valueType() != ValueType.INVALID) {
75+
result.put(entry.getKey(), mapped);
76+
}
7477
}
7578
return Any.wrapAnyMap(result);
7679
}

src/main/java/com/jsoniter/any/ObjectLazyAny.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ public Any get(Object[] keys, int idx) {
7373
fillCache();
7474
HashMap<String, Any> result = new HashMap<String, Any>();
7575
for (Map.Entry<Object, Any> entry : cache.entrySet()) {
76-
result.put((String) entry.getKey(), entry.getValue().get(keys, idx+1));
76+
Any mapped = entry.getValue().get(keys, idx + 1);
77+
if (mapped.valueType() != ValueType.INVALID) {
78+
result.put((String) entry.getKey(), mapped);
79+
}
7780
}
7881
return Any.wrapAnyMap(result);
7982
}

src/test/java/com/jsoniter/TestNested.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,19 @@ public void test_get_all_object_values_via_any() throws IOException {
4444
result = any.get('*', 1);
4545
assertEquals("{\"field1\":2,\"field2\":4}", result.toString());
4646
}
47+
48+
public void test_get_all_with_some_invalid_path() throws IOException {
49+
Any any = JsonIterator.deserialize(" [ { \"bar\": 1 }, {\"foo\": 3} ]");
50+
Any result = any.get('*', "bar");
51+
assertEquals("[ 1]", result.toString());
52+
any = Any.wrapAnyList(any.asList()); // make it not lazy
53+
result = any.get('*', "bar");
54+
assertEquals("[ 1]", result.toString());
55+
any = JsonIterator.deserialize("{\"field1\":[1,2],\"field2\":[3]}");
56+
result = any.get('*', 1);
57+
assertEquals("{\"field1\":2}", result.toString());
58+
any = Any.wrapAnyMap(any.asMap()); // make it not lazy
59+
result = any.get('*', 1);
60+
assertEquals("{\"field1\":2}", result.toString());
61+
}
4762
}

0 commit comments

Comments
 (0)