I did sample code below to check JsonPath version 2.9.0 thread safe or not, I realized it did not. Is that the issue of JsonPath or there is any way to cope it? Or any alternative solution? Please help
@Test
public void testJsonPath() throws IOException {
String jsonString = "{\"test\":1,\"person\":{\"name\":\"John\",\"age\":50}}";
String jsonString2 ="{\"test\":1,\"person\":{\"name\":\"Babara\",\"age\":48}}";
Thread thread1 = new Thread(createRunnable(jsonString));
Thread thread2 = new Thread(createRunnable(jsonString2));
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private Runnable createRunnable(String json) {
return () -> {
int count = 10;
try {
while (count > 0) {
count--;
Thread.sleep(1000);
String value = JsonPath.read(json, "concat($.person.name, $.person.age)");
if (value.equals("John48") || value.equals("Babara50")) {
System.out.println("Wrong value returned: " + value);
}
System.out.printf("Thread-[%d], Value: [%s]%n", Thread.currentThread().getId(), value);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}
The result showed that JsonPath return wrong values, such as: Babara50, John48
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [**Babara50**]
Thread-[31], Value: [**John48**]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [**Babara50**]
Thread-[30], Value: [John50]
Thread-[31], Value: [**John48**]
Thread-[31], Value: [**John48**]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [John50]
This is caused by this bug https://github.com/json-path/JsonPath/issues/975 as Vova Bilychat pointed out in a comment on the question.
The jayway
JsonPath implementation uses a cache which is maintained across calls to JsonPath.read
.
You can disable the cache by setting a dummy cache which never finds anything:
CacheProvider.setCache(new NOOPCache());