2525import com .jayway .jsonpath .TypeRef ;
2626import com .jayway .jsonpath .spi .cache .Cache ;
2727import com .jayway .jsonpath .spi .cache .CacheProvider ;
28+
2829import org .slf4j .Logger ;
2930import org .slf4j .LoggerFactory ;
3031
@@ -74,21 +75,7 @@ public String jsonString() {
7475 @ Override
7576 public <T > T read (String path , Predicate ... filters ) {
7677 notEmpty (path , "path can not be null or empty" );
77- Cache cache = CacheProvider .getCache ();
78-
79- path = path .trim ();
80- LinkedList filterStack = new LinkedList <Predicate >(asList (filters ));
81- String cacheKey = Utils .concat (path , filterStack .toString ());
82-
83- JsonPath jsonPath = cache .get (cacheKey );
84- if (jsonPath != null ){
85- return read (jsonPath );
86- } else {
87- jsonPath = compile (path , filters );
88- cache .put (cacheKey , jsonPath );
89- return read (jsonPath );
90- }
91-
78+ return read (pathFromCache (path , filters ));
9279 }
9380
9481 @ Override
@@ -117,32 +104,33 @@ public <T> T read(String path, TypeRef<T> type) {
117104 return convert (read (path ), type , configuration );
118105 }
119106
120- public ReadContext limit (int maxResults ){
107+ @ Override
108+ public ReadContext limit (int maxResults ) {
121109 return withListeners (new LimitingEvaluationListener (maxResults ));
122110 }
123111
124- public ReadContext withListeners (EvaluationListener ... listener ){
112+ @ Override
113+ public ReadContext withListeners (EvaluationListener ... listener ) {
125114 return new JsonContext (json , configuration .setEvaluationListeners (listener ));
126115 }
127116
128-
129- private <T > T convert (Object obj , Class <T > targetType , Configuration configuration ){
117+ private <T > T convert (Object obj , Class <T > targetType , Configuration configuration ) {
130118 return configuration .mappingProvider ().map (obj , targetType , configuration );
131119 }
132120
133- private <T > T convert (Object obj , TypeRef <T > targetType , Configuration configuration ){
121+ private <T > T convert (Object obj , TypeRef <T > targetType , Configuration configuration ) {
134122 return configuration .mappingProvider ().map (obj , targetType , configuration );
135123 }
136124
137125 @ Override
138126 public DocumentContext set (String path , Object newValue , Predicate ... filters ) {
139- return set (compile (path , filters ), newValue );
127+ return set (pathFromCache (path , filters ), newValue );
140128 }
141129
142130 @ Override
143- public DocumentContext set (JsonPath path , Object newValue ){
131+ public DocumentContext set (JsonPath path , Object newValue ) {
144132 List <String > modified = path .set (json , newValue , configuration .addOptions (Option .AS_PATH_LIST ));
145- if (logger .isDebugEnabled ()){
133+ if (logger .isDebugEnabled ()) {
146134 for (String p : modified ) {
147135 logger .debug ("Set path {} new value {}" , p , newValue );
148136 }
@@ -152,7 +140,7 @@ public DocumentContext set(JsonPath path, Object newValue){
152140
153141 @ Override
154142 public DocumentContext map (String path , MapFunction mapFunction , Predicate ... filters ) {
155- map (compile (path , filters ), mapFunction );
143+ map (pathFromCache (path , filters ), mapFunction );
156144 return this ;
157145 }
158146
@@ -164,29 +152,29 @@ public DocumentContext map(JsonPath path, MapFunction mapFunction) {
164152
165153 @ Override
166154 public DocumentContext delete (String path , Predicate ... filters ) {
167- return delete (compile (path , filters ));
155+ return delete (pathFromCache (path , filters ));
168156 }
169157
170158 @ Override
171159 public DocumentContext delete (JsonPath path ) {
172160 List <String > modified = path .delete (json , configuration .addOptions (Option .AS_PATH_LIST ));
173- if (logger .isDebugEnabled ()){
161+ if (logger .isDebugEnabled ()) {
174162 for (String p : modified ) {
175- logger .debug ("Delete path {}" );
163+ logger .debug ("Delete path {}" , p );
176164 }
177165 }
178166 return this ;
179167 }
180168
181169 @ Override
182- public DocumentContext add (String path , Object value , Predicate ... filters ){
183- return add (compile (path , filters ), value );
170+ public DocumentContext add (String path , Object value , Predicate ... filters ) {
171+ return add (pathFromCache (path , filters ), value );
184172 }
185173
186174 @ Override
187- public DocumentContext add (JsonPath path , Object value ){
188- List <String > modified = path .add (json , value , configuration .addOptions (Option .AS_PATH_LIST ));
189- if (logger .isDebugEnabled ()){
175+ public DocumentContext add (JsonPath path , Object value ) {
176+ List <String > modified = path .add (json , value , configuration .addOptions (Option .AS_PATH_LIST ));
177+ if (logger .isDebugEnabled ()) {
190178 for (String p : modified ) {
191179 logger .debug ("Add path {} new value {}" , p , value );
192180 }
@@ -195,38 +183,48 @@ public DocumentContext add(JsonPath path, Object value){
195183 }
196184
197185 @ Override
198- public DocumentContext put (String path , String key , Object value , Predicate ... filters ){
199- return put (compile (path , filters ), key , value );
186+ public DocumentContext put (String path , String key , Object value , Predicate ... filters ) {
187+ return put (pathFromCache (path , filters ), key , value );
200188 }
201189
202190 @ Override
203191 public DocumentContext renameKey (String path , String oldKeyName , String newKeyName , Predicate ... filters ) {
204- return renameKey (compile (path , filters ), oldKeyName , newKeyName );
192+ return renameKey (pathFromCache (path , filters ), oldKeyName , newKeyName );
205193 }
206194
207195 @ Override
208196 public DocumentContext renameKey (JsonPath path , String oldKeyName , String newKeyName ) {
209- List <String > modified = path .renameKey (json , oldKeyName , newKeyName , configuration .addOptions (Option .AS_PATH_LIST ));
210- if (logger .isDebugEnabled ()){
197+ List <String > modified = path .renameKey (json , oldKeyName , newKeyName , configuration .addOptions (Option .AS_PATH_LIST ));
198+ if (logger .isDebugEnabled ()) {
211199 for (String p : modified ) {
212200 logger .debug ("Rename path {} new value {}" , p , newKeyName );
213201 }
214202 }
215203 return this ;
216204 }
217205
218-
219206 @ Override
220- public DocumentContext put (JsonPath path , String key , Object value ){
207+ public DocumentContext put (JsonPath path , String key , Object value ) {
221208 List <String > modified = path .put (json , key , value , configuration .addOptions (Option .AS_PATH_LIST ));
222- if (logger .isDebugEnabled ()){
209+ if (logger .isDebugEnabled ()) {
223210 for (String p : modified ) {
224211 logger .debug ("Put path {} key {} value {}" , p , key , value );
225212 }
226213 }
227214 return this ;
228215 }
229216
217+ private JsonPath pathFromCache (String path , Predicate [] filters ) {
218+ Cache cache = CacheProvider .getCache ();
219+ String cacheKey = Utils .concat (path , new LinkedList <Predicate >(asList (filters )).toString ());
220+ JsonPath jsonPath = cache .get (cacheKey );
221+ if (jsonPath == null ) {
222+ jsonPath = compile (path , filters );
223+ cache .put (cacheKey , jsonPath );
224+ }
225+ return jsonPath ;
226+ }
227+
230228 private final static class LimitingEvaluationListener implements EvaluationListener {
231229 final int limit ;
232230
@@ -236,11 +234,12 @@ private LimitingEvaluationListener(int limit) {
236234
237235 @ Override
238236 public EvaluationContinuation resultFound (FoundResult found ) {
239- if (found .index () == limit - 1 ){
237+ if (found .index () == limit - 1 ) {
240238 return EvaluationContinuation .ABORT ;
241239 } else {
242240 return EvaluationContinuation .CONTINUE ;
243241 }
244242 }
245243 }
244+
246245}
0 commit comments