Skip to content

Commit

Permalink
Issue json-path/JsonPath#497 fixed, the workaround
Browse files Browse the repository at this point in the history
is not needed any more in jrds-jsonp.
  • Loading branch information
fbacchella committed Nov 15, 2021
1 parent 3e82f7f commit ed282b7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 36 deletions.
22 changes: 4 additions & 18 deletions jrds-jsonp/src/main/java/jrds/jsonp/probe/HttpJson.java
Expand Up @@ -76,7 +76,7 @@ protected Map<String, Number> parseStream(InputStream stream) {

for (Map.Entry<JsonPath, String> e: collectKeys.entrySet()) {
try {
JSONArray v = protectedRead(ctx, e.getKey());
JSONArray v = ctx.read(e.getKey(), JSONArray.class);
if (v.isEmpty()) {
log(Level.ERROR, "Failed to collect data '%s': not found", e.getValue());
break;
Expand Down Expand Up @@ -107,18 +107,18 @@ protected long findUptime(DocumentContext ctx) {
JSONArray v;
long returned;
if (uptimePointer != null) {
v = protectedRead(ctx, uptimePointer);
v = ctx.read(uptimePointer, JSONArray.class);
if (v == null || v.isEmpty()) {
return 0;
}
returned = valueToNum(v.get(0)).longValue();
} else if (startPointer != null) {
v = protectedRead(ctx, startPointer);
v = ctx.read(startPointer, JSONArray.class);
if (v == null || v.isEmpty()) {
return 0;
}
long start = valueToNum(v.get(0)).longValue();
v = protectedRead(ctx, currentTimePointer);
v = ctx.read(currentTimePointer, JSONArray.class);
if (v == null || v.isEmpty()) {
return 0;
}
Expand All @@ -134,18 +134,4 @@ protected long findUptime(DocumentContext ctx) {
}
}

/**
* A protection against issue https://github.com/json-path/JsonPath/issues/497
* @param ctx
* @param path
* @return
*/
private JSONArray protectedRead(DocumentContext ctx, JsonPath path) {
try {
return ctx.read(path, JSONArray.class);
} catch (NullPointerException ex) {
return new JSONArray();
}
}

}
18 changes: 0 additions & 18 deletions jrds-jsonp/src/test/java/jrds/jsonp/probe/JsonProbeTest.java
Expand Up @@ -14,11 +14,6 @@
import org.junit.rules.TemporaryFolder;
import org.slf4j.event.Level;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
import com.jayway.jsonpath.spi.mapper.JsonOrgMappingProvider;

import jrds.HostInfo;
import jrds.JrdsSample;
import jrds.Log4JRule;
Expand Down Expand Up @@ -48,19 +43,6 @@ public void loggers() {
logrule.setLevel(Level.TRACE, "jrds.jsonp.probe.HttpJson", "jrds.jsonp.starter.JsonpProvider");
}

// Expected to fail when https://github.com/json-path/JsonPath/issues/497 will be corrected
@Test(expected=NullPointerException.class)
public void issue497() throws IOException {
String json = "{\"foo\": \"bar\", \"emptyObject\": {}}";

Configuration config = Configuration.defaultConfiguration()
.jsonProvider(new JsonOrgJsonProvider())
.mappingProvider(new JsonOrgMappingProvider());

Object result = JsonPath.using(config).parse(json).read("$..foo");
Assert.assertNotNull(result);
}

@Test
public void parseJson() throws Exception {
PropertiesManager pm = Tools.makePm(testFolder, "timeout=1", "collectorThreads=1");
Expand Down

0 comments on commit ed282b7

Please sign in to comment.