From 4bfd0b9766348beffdbf8a508557cc4975dc2b96 Mon Sep 17 00:00:00 2001 From: ERICXUCHI Date: Wed, 25 May 2022 22:16:46 +0800 Subject: [PATCH] update Issue 515 --- .../com/jayway/jsonpath/DocumentContext.java | 4 +- .../jayway/jsonpath/internal/JsonContext.java | 26 +++- json-path/src/test/java/Issue_515.java | 123 ++++++++++++++++++ 3 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 json-path/src/test/java/Issue_515.java diff --git a/json-path/src/main/java/com/jayway/jsonpath/DocumentContext.java b/json-path/src/main/java/com/jayway/jsonpath/DocumentContext.java index 4604b444..d380ea95 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/DocumentContext.java +++ b/json-path/src/main/java/com/jayway/jsonpath/DocumentContext.java @@ -14,5 +14,7 @@ */ package com.jayway.jsonpath; -public interface DocumentContext extends ReadContext, WriteContext { +import java.io.Externalizable; + +public interface DocumentContext extends ReadContext, WriteContext, Externalizable { } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java index 76985f78..ab70be10 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java @@ -29,6 +29,10 @@ import com.jayway.jsonpath.spi.cache.CacheProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -37,11 +41,12 @@ import static com.jayway.jsonpath.internal.Utils.notEmpty; import static com.jayway.jsonpath.internal.Utils.notNull; public class JsonContext implements DocumentContext { + public static List configurationList=new ArrayList<>(); private static final Logger logger = LoggerFactory.getLogger(JsonContext.class); - private final Configuration configuration; - private final Object json; + private Configuration configuration; + private Object json; JsonContext(Object json, Configuration configuration) { notNull(json, "json can not be null"); @@ -50,7 +55,9 @@ public class JsonContext implements DocumentContext { this.json = json; } + public JsonContext() { + } @Override public Configuration configuration() { return configuration; @@ -216,7 +223,7 @@ public class JsonContext implements DocumentContext { private JsonPath pathFromCache(String path, Predicate[] filters) { Cache cache = CacheProvider.getCache(); String cacheKey = filters == null || filters.length == 0 - ? path : Utils.concat(path, Arrays.toString(filters)); + ? path : Utils.concat(path, Arrays.toString(filters)); JsonPath jsonPath = cache.get(cacheKey); if (jsonPath == null) { jsonPath = compile(path, filters); @@ -225,6 +232,19 @@ public class JsonContext implements DocumentContext { return jsonPath; } + @Override + public void writeExternal( ObjectOutput objectOutput ) throws IOException { + configurationList.add(this.configuration); + objectOutput.writeObject(json); + objectOutput.writeInt(configurationList.size()-1); + } + + @Override + public void readExternal( ObjectInput objectInput ) throws IOException, ClassNotFoundException { + json=objectInput.readObject(); + configuration=configurationList.get(objectInput.readInt()); + } + private final static class LimitingEvaluationListener implements EvaluationListener { final int limit; diff --git a/json-path/src/test/java/Issue_515.java b/json-path/src/test/java/Issue_515.java new file mode 100644 index 00000000..487d3972 --- /dev/null +++ b/json-path/src/test/java/Issue_515.java @@ -0,0 +1,123 @@ +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import org.junit.Assert; +import org.junit.Test; + +import java.io.*; +import java.lang.reflect.Field; + +import static org.junit.Assert.fail; + +public class Issue_515 { + @Test + public void Test_DocumentContextSerializable1(){ + String json = "{\n" + + " \"store\": [\n" + + " {\n" + + " \"books\": [\n" + + " {\n" + + " \"category\": \"reference\",\n" + + " \"author\": \"Nigel Rees\",\n" + + " \"title\": \"Sayings of the Century\",\n" + + " \"price\": 8.95\n" + + " }\n" + + " ],\n" + + " \"address\": [\n" + + " {\n" + + " \"city\": \"New York\"\n" + + " },\n" + + " {\n" + + " \"city\": \"Paris\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"books\": [\n" + + " {\n" + + " \"category\": \"fiction\",\n" + + " \"author\": \"Evelyn Waugh\",\n" + + " \"title\": \"Sword of Honour\",\n" + + " \"price\": 12.99\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}"; + DocumentContext originContext= JsonPath.parse(json); + boolean equal=true; + try { + FileOutputStream fos=new FileOutputStream("obj.txt"); + ObjectOutputStream outputStream=new ObjectOutputStream(fos); + outputStream.writeObject(originContext); + outputStream.close(); + fos.close(); + FileInputStream ios=new FileInputStream("obj.txt"); + ObjectInputStream inputStream=new ObjectInputStream(ios); + DocumentContext inputObj=(DocumentContext) inputStream.readObject(); + Field[] inputFields=inputObj.getClass().getDeclaredFields(),originFields=originContext.getClass().getDeclaredFields(); + ios.close(); + inputStream.close(); + if(inputFields.length==originFields.length){ + for(int i=0;i