From 16c02c76c0379796e739bc0b7320e538b0e7ffcb Mon Sep 17 00:00:00 2001 From: CindyChow123 Date: Sat, 24 Apr 2021 20:46:53 +0800 Subject: [PATCH] fix 656 2.0 --- .../jayway/jsonpath/internal/JsonContext.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) 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 9d6766c5..4ad3fb55 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 @@ -25,6 +25,7 @@ import com.jayway.jsonpath.ReadContext; import com.jayway.jsonpath.TypeRef; import com.jayway.jsonpath.spi.cache.Cache; import com.jayway.jsonpath.spi.cache.CacheProvider; +import com.jayway.jsonpath.spi.json.JsonProvider; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -55,19 +56,26 @@ public class JsonContext implements DocumentContext { this.configuration = configuration; //CS304 Issue link: https://github.com/json-path/JsonPath/issues/656 Set set = new HashSet<>(); - Collection properties = configuration.jsonProvider().getPropertyKeys(json); - for (String property : properties) { - Object propertyModel = configuration.jsonProvider().getMapValue(json,property); - if(!set.contains(propertyModel)){ - set.add(propertyModel); - }else{ - ObjectNode obj_cast = (ObjectNode) propertyModel; - ObjectNode newobj = obj_cast.deepCopy(); - ObjectNode objects_cast = (ObjectNode) json; - objects_cast.replace(property,newobj); + if (json instanceof ObjectNode){ + Collection properties = configuration.jsonProvider().getPropertyKeys(json); + for (String property : properties) { + Object propertyModel = configuration.jsonProvider().getMapValue(json, property); + if (!set.contains(propertyModel)) { + set.add(propertyModel); + } else { + if(propertyModel instanceof ObjectNode){ + ObjectNode obj_cast = (ObjectNode) propertyModel; + ObjectNode newobj = obj_cast.deepCopy(); + ObjectNode objects_cast = (ObjectNode) json; + objects_cast.replace(property, newobj); + } + + } } + this.json = json; + } else { + this.json = json; } - this.json = json; }