Browse Source
Added licence heade Continued SPI work Added File, InputStream, URL readerspull/7/head
Kalle Stenflo
13 years ago
40 changed files with 1027 additions and 479 deletions
@ -1,63 +1,152 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2011 the original author or authors. |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
package com.jayway.jsonpath; |
package com.jayway.jsonpath; |
||||||
|
|
||||||
import com.jayway.jsonpath.spi.JsonProvider; |
import com.jayway.jsonpath.spi.JsonProviderFactory; |
||||||
|
import org.codehaus.jackson.map.ObjectMapper; |
||||||
|
import org.codehaus.jackson.map.type.CollectionType; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by IntelliJ IDEA. |
* @author Kalle Stenflo |
||||||
* User: kallestenflo |
|
||||||
* Date: 11/8/11 |
|
||||||
* Time: 7:40 PM |
|
||||||
*/ |
*/ |
||||||
public class JsonModel { |
public class JsonModel { |
||||||
|
|
||||||
|
private static ObjectMapper objectMapper; |
||||||
|
|
||||||
private Object jsonObject; |
private Object jsonObject; |
||||||
|
|
||||||
private JsonProvider jsonProvider; |
// --------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Constructors
|
||||||
|
//
|
||||||
|
// --------------------------------------------------------
|
||||||
|
|
||||||
public JsonModel(String json) { |
public JsonModel(String json) { |
||||||
this.jsonProvider = JsonProvider.getInstance(); |
this(JsonProviderFactory.getInstance().parse(json)); |
||||||
this.jsonObject = jsonProvider.parse(json); |
|
||||||
} |
} |
||||||
|
|
||||||
private JsonModel(Object jsonObject) { |
private JsonModel(Object jsonObject) { |
||||||
this.jsonProvider = JsonProvider.getInstance(); |
|
||||||
this.jsonObject = jsonObject; |
this.jsonObject = jsonObject; |
||||||
} |
} |
||||||
|
|
||||||
public static JsonModel create(String json) { |
@SuppressWarnings({"unchecked"}) |
||||||
return new JsonModel(json); |
|
||||||
} |
|
||||||
|
|
||||||
public <T> T get(String jsonPath) { |
public <T> T get(String jsonPath) { |
||||||
JsonPath path = JsonPath.compile(jsonProvider, jsonPath); |
JsonPath path = JsonPath.compile(jsonPath); |
||||||
return (T) get(path); |
return (T) get(path); |
||||||
} |
} |
||||||
|
|
||||||
|
@SuppressWarnings({"unchecked"}) |
||||||
public <T> T get(JsonPath jsonPath) { |
public <T> T get(JsonPath jsonPath) { |
||||||
return (T) jsonPath.read(jsonObject); |
return (T) jsonPath.read(jsonObject); |
||||||
} |
} |
||||||
|
|
||||||
|
public String getJson() { |
||||||
|
return JsonProviderFactory.getInstance().toJson(jsonObject); |
||||||
|
} |
||||||
|
|
||||||
|
public String getJson(String jsonPath) { |
||||||
|
return JsonProviderFactory.getInstance().toJson(get(jsonPath)); |
||||||
|
} |
||||||
|
|
||||||
public String getJson(JsonPath jsonPath) { |
public String getJson(JsonPath jsonPath) { |
||||||
return jsonProvider.toJson(jsonPath.read(jsonObject)); |
return JsonProviderFactory.getInstance().toJson(get(jsonPath)); |
||||||
} |
} |
||||||
|
|
||||||
public JsonModel getModel(String jsonPath) { |
|
||||||
JsonPath path = JsonPath.compile(jsonProvider, jsonPath); |
|
||||||
|
|
||||||
return getModel(path); |
public JsonModel getSubModel(String jsonPath) { |
||||||
|
JsonPath path = JsonPath.compile(jsonPath); |
||||||
|
return getSubModel(path); |
||||||
} |
} |
||||||
|
|
||||||
public JsonModel getModel(JsonPath jsonPath) { |
public JsonModel getSubModel(JsonPath jsonPath) { |
||||||
Object subModel = jsonPath.read(jsonObject); |
Object subModel = jsonPath.read(jsonObject); |
||||||
|
|
||||||
return new JsonModel(subModel); |
return new JsonModel(subModel); |
||||||
} |
} |
||||||
|
|
||||||
public JsonProvider getJsonProvider() { |
|
||||||
return jsonProvider; |
public MappingModelReader map(final String jsonPath) { |
||||||
|
|
||||||
|
return new MappingModelReader() { |
||||||
|
|
||||||
|
private ObjectMapper objectMapper = JsonModel.getObjectMapper(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public <T> List<T> toListOf(Class<T> targetClass) { |
||||||
|
Object model = JsonModel.this.get(jsonPath); |
||||||
|
CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(List.class, targetClass); |
||||||
|
return objectMapper.convertValue(model, colType); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public <T> Set<T> toSetOf(Class<T> targetClass) { |
||||||
|
Object model = JsonModel.this.get(jsonPath); |
||||||
|
CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(Set.class, targetClass); |
||||||
|
return objectMapper.convertValue(model, colType); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public <T> T to(Class<T> targetClass) { |
||||||
|
Object model = JsonModel.this.get(jsonPath); |
||||||
|
return objectMapper.convertValue(model, targetClass); |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
// --------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Static factory methods
|
||||||
|
//
|
||||||
|
// --------------------------------------------------------
|
||||||
|
public static JsonModel create(String json) { |
||||||
|
return new JsonModel(json); |
||||||
} |
} |
||||||
|
|
||||||
public String getJson(){ |
public static JsonModel create(Object jsonObject) { |
||||||
return jsonProvider.toJson(jsonObject); |
return new JsonModel(jsonObject); |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Support interfaces
|
||||||
|
//
|
||||||
|
// --------------------------------------------------------
|
||||||
|
public interface MappingModelReader { |
||||||
|
<T> List<T> toListOf(Class<T> targetClass); |
||||||
|
|
||||||
|
<T> Set<T> toSetOf(Class<T> targetClass); |
||||||
|
|
||||||
|
<T> T to(Class<T> targetClass); |
||||||
|
} |
||||||
|
|
||||||
|
// --------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Private helpers
|
||||||
|
//
|
||||||
|
// --------------------------------------------------------
|
||||||
|
private static ObjectMapper getObjectMapper() { |
||||||
|
if (JsonModel.objectMapper == null) { |
||||||
|
synchronized (JsonModel.class) { |
||||||
|
JsonModel.objectMapper = new ObjectMapper(); |
||||||
|
} |
||||||
|
} |
||||||
|
return JsonModel.objectMapper; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
} |
} |
||||||
|
@ -0,0 +1,43 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2011 the original author or authors. |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.jayway.jsonpath.internal; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.internal.filter.Filter; |
||||||
|
import com.jayway.jsonpath.internal.filter.FilterFactory; |
||||||
|
import com.jayway.jsonpath.spi.JsonProvider; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Kalle Stenflo |
||||||
|
*/ |
||||||
|
public class PathToken { |
||||||
|
|
||||||
|
private String fragment; |
||||||
|
|
||||||
|
public PathToken(String fragment) { |
||||||
|
this.fragment = fragment; |
||||||
|
} |
||||||
|
|
||||||
|
public Filter getFilter(){ |
||||||
|
return FilterFactory.createFilter(fragment); |
||||||
|
} |
||||||
|
|
||||||
|
public Object filter(Object model, JsonProvider jsonProvider){ |
||||||
|
return FilterFactory.createFilter(fragment).filter(model, jsonProvider); |
||||||
|
} |
||||||
|
|
||||||
|
public String getFragment() { |
||||||
|
return fragment; |
||||||
|
} |
||||||
|
} |
@ -1,14 +1,25 @@ |
|||||||
package com.jayway.jsonpath.reader.filter; |
/* |
||||||
|
* Copyright 2011 the original author or authors. |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.jayway.jsonpath.internal.filter; |
||||||
|
|
||||||
import com.jayway.jsonpath.spi.JsonProvider; |
import com.jayway.jsonpath.spi.JsonProvider; |
||||||
|
|
||||||
import java.util.List; |
import java.util.List; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by IntelliJ IDEA. |
* @author Kalle Stenflo |
||||||
* User: kallestenflo |
|
||||||
* Date: 11/4/11 |
|
||||||
* Time: 11:25 PM |
|
||||||
*/ |
*/ |
||||||
public class ArrayIndexFilter extends Filter { |
public class ArrayIndexFilter extends Filter { |
||||||
public ArrayIndexFilter(String condition) { |
public ArrayIndexFilter(String condition) { |
@ -0,0 +1,53 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2011 the original author or authors. |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.jayway.jsonpath.internal.filter; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.spi.JsonProvider; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Kalle Stenflo |
||||||
|
*/ |
||||||
|
public abstract class Filter { |
||||||
|
|
||||||
|
final String condition; |
||||||
|
|
||||||
|
Filter(String condition) { |
||||||
|
this.condition = condition; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
String trim(String str, int front, int end) { |
||||||
|
String res = str; |
||||||
|
|
||||||
|
if (front > 0) { |
||||||
|
res = str.substring(front); |
||||||
|
} |
||||||
|
if (end > 0) { |
||||||
|
res = res.substring(0, res.length() - end); |
||||||
|
} |
||||||
|
return res; |
||||||
|
} |
||||||
|
|
||||||
|
public Object filter(Object obj, JsonProvider jsonProvider, boolean inArrayContext){ |
||||||
|
return filter(obj, jsonProvider); |
||||||
|
} |
||||||
|
|
||||||
|
public abstract Object filter(Object obj, JsonProvider jsonProvider); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public abstract boolean isArrayFilter(); |
||||||
|
|
||||||
|
} |
@ -1,10 +1,21 @@ |
|||||||
package com.jayway.jsonpath.reader.filter; |
/* |
||||||
|
* Copyright 2011 the original author or authors. |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.jayway.jsonpath.internal.filter; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by IntelliJ IDEA. |
* @author Kalle Stenflo |
||||||
* User: kallestenflo |
|
||||||
* Date: 11/4/11 |
|
||||||
* Time: 10:13 PM |
|
||||||
*/ |
*/ |
||||||
public class FilterFactory { |
public class FilterFactory { |
||||||
|
|
@ -0,0 +1,40 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2011 the original author or authors. |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.jayway.jsonpath.internal.filter; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.spi.JsonProvider; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Kalle Stenflo |
||||||
|
*/ |
||||||
|
public class PassthroughFilter extends Filter { |
||||||
|
|
||||||
|
|
||||||
|
private boolean isArrayFilter; |
||||||
|
|
||||||
|
public PassthroughFilter(String condition, boolean isArrayFilter) { |
||||||
|
super(condition); |
||||||
|
this.isArrayFilter = isArrayFilter; |
||||||
|
} |
||||||
|
|
||||||
|
public Object filter(Object obj, JsonProvider jsonProvider) { |
||||||
|
return obj; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isArrayFilter() { |
||||||
|
return isArrayFilter; |
||||||
|
} |
||||||
|
} |
@ -1,14 +1,25 @@ |
|||||||
package com.jayway.jsonpath.reader.filter; |
/* |
||||||
|
* Copyright 2011 the original author or authors. |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.jayway.jsonpath.internal.filter; |
||||||
|
|
||||||
import com.jayway.jsonpath.spi.JsonProvider; |
import com.jayway.jsonpath.spi.JsonProvider; |
||||||
|
|
||||||
import java.util.List; |
import java.util.List; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by IntelliJ IDEA. |
* @author Kalle Stenflo |
||||||
* User: kallestenflo |
|
||||||
* Date: 11/7/11 |
|
||||||
* Time: 1:59 PM |
|
||||||
*/ |
*/ |
||||||
public class WildcardFilter extends Filter { |
public class WildcardFilter extends Filter { |
||||||
|
|
@ -1,13 +1,24 @@ |
|||||||
package com.jayway.jsonpath.reader.filter.eval; |
/* |
||||||
|
* Copyright 2011 the original author or authors. |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package com.jayway.jsonpath.internal.filter.eval; |
||||||
|
|
||||||
/** |
/** |
||||||
* User: kalle stenflo |
* @author Kalle Stenflo |
||||||
* Date: 2/4/11 |
|
||||||
* Time: 9:21 PM |
|
||||||
*/ |
*/ |
||||||
public class ExpressionEvaluator { |
public class ExpressionEvaluator { |
||||||
|
|
||||||
|
|
||||||
public static <T> boolean eval(T actual, String comparator, String expected) { |
public static <T> boolean eval(T actual, String comparator, String expected) { |
||||||
|
|
||||||
comparator = comparator.trim(); |
comparator = comparator.trim(); |
@ -1,32 +0,0 @@ |
|||||||
package com.jayway.jsonpath.reader; |
|
||||||
|
|
||||||
import com.jayway.jsonpath.reader.filter.Filter; |
|
||||||
import com.jayway.jsonpath.reader.filter.FilterFactory; |
|
||||||
import com.jayway.jsonpath.spi.JsonProvider; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: kallestenflo |
|
||||||
* Date: 11/4/11 |
|
||||||
* Time: 10:00 PM |
|
||||||
*/ |
|
||||||
public class PathToken { |
|
||||||
|
|
||||||
private String fragment; |
|
||||||
|
|
||||||
public PathToken(String fragment) { |
|
||||||
this.fragment = fragment; |
|
||||||
} |
|
||||||
|
|
||||||
public Filter getFilter(){ |
|
||||||
return FilterFactory.createFilter(fragment); |
|
||||||
} |
|
||||||
|
|
||||||
public Object filter(Object model, JsonProvider jsonProvider){ |
|
||||||
return FilterFactory.createFilter(fragment).filter(model, jsonProvider); |
|
||||||
} |
|
||||||
|
|
||||||
public String getFragment() { |
|
||||||
return fragment; |
|
||||||
} |
|
||||||
} |
|
@ -1,42 +0,0 @@ |
|||||||
package com.jayway.jsonpath.reader.filter; |
|
||||||
|
|
||||||
import com.jayway.jsonpath.spi.JsonProvider; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: kallestenflo |
|
||||||
* Date: 11/4/11 |
|
||||||
* Time: 10:14 PM |
|
||||||
*/ |
|
||||||
public abstract class Filter { |
|
||||||
|
|
||||||
protected final String condition; |
|
||||||
|
|
||||||
public Filter(String condition) { |
|
||||||
this.condition = condition; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
protected String trim(String str, int front, int end) { |
|
||||||
String res = str; |
|
||||||
|
|
||||||
if (front > 0) { |
|
||||||
res = str.substring(front); |
|
||||||
} |
|
||||||
if (end > 0) { |
|
||||||
res = res.substring(0, res.length() - end); |
|
||||||
} |
|
||||||
return res; |
|
||||||
} |
|
||||||
|
|
||||||
public Object filter(Object obj, JsonProvider jsonProvider, boolean inArrayContext){ |
|
||||||
return filter(obj, jsonProvider); |
|
||||||
} |
|
||||||
|
|
||||||
public abstract Object filter(Object obj, JsonProvider jsonProvider); |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public abstract boolean isArrayFilter(); |
|
||||||
|
|
||||||
} |
|
@ -1,29 +0,0 @@ |
|||||||
package com.jayway.jsonpath.reader.filter; |
|
||||||
|
|
||||||
import com.jayway.jsonpath.spi.JsonProvider; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: kallestenflo |
|
||||||
* Date: 11/4/11 |
|
||||||
* Time: 10:15 PM |
|
||||||
*/ |
|
||||||
public class PassthroughFilter extends Filter { |
|
||||||
|
|
||||||
|
|
||||||
private boolean isArrayFilter; |
|
||||||
|
|
||||||
public PassthroughFilter(String condition, boolean isArrayFilter) { |
|
||||||
super(condition); |
|
||||||
this.isArrayFilter = isArrayFilter; |
|
||||||
} |
|
||||||
|
|
||||||
public Object filter(Object obj, JsonProvider jsonProvider) { |
|
||||||
return obj; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean isArrayFilter() { |
|
||||||
return isArrayFilter; |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,17 @@ |
|||||||
|
package com.jayway.jsonpath.spi; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.spi.impl.JsonSmartProvider; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by IntelliJ IDEA. |
||||||
|
* User: kallestenflo |
||||||
|
* Date: 3/2/12 |
||||||
|
* Time: 9:45 PM |
||||||
|
*/ |
||||||
|
public abstract class JsonProviderFactory { |
||||||
|
|
||||||
|
public static JsonProvider getInstance() { |
||||||
|
return new JsonSmartProvider(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,80 @@ |
|||||||
|
package com.jayway.jsonpath.spi.impl; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.spi.JsonProvider; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by IntelliJ IDEA. |
||||||
|
* User: kallestenflo |
||||||
|
* Date: 3/2/12 |
||||||
|
* Time: 9:56 PM |
||||||
|
*/ |
||||||
|
public abstract class AbstractJsonProvider implements JsonProvider { |
||||||
|
|
||||||
|
/** |
||||||
|
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code> |
||||||
|
* |
||||||
|
* @param obj object to check |
||||||
|
* @return true if List or Map |
||||||
|
*/ |
||||||
|
public boolean isContainer(Object obj) { |
||||||
|
return (isList(obj) || isMap(obj)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* checks if object is <code>instanceof</code> <code>java.util.List</code> |
||||||
|
* |
||||||
|
* @param obj object to check |
||||||
|
* @return true if List |
||||||
|
*/ |
||||||
|
public boolean isList(Object obj) { |
||||||
|
return (obj instanceof List); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Converts give object to a List |
||||||
|
* |
||||||
|
* @param list |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
@SuppressWarnings({"unchecked"}) |
||||||
|
public List<Object> toList(Object list) { |
||||||
|
return (List<Object>) list; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Converts given object to a Map |
||||||
|
* |
||||||
|
* @param map |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
@SuppressWarnings({"unchecked"}) |
||||||
|
public Map<String, Object> toMap(Object map) { |
||||||
|
return (Map<String, Object>) map; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Extracts a value from a Map |
||||||
|
* |
||||||
|
* @param map |
||||||
|
* @param key |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Object getMapValue(Object map, String key) { |
||||||
|
return toMap(map).get(key); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* checks if object is <code>instanceof</code> <code>java.util.Map</code> |
||||||
|
* |
||||||
|
* @param obj object to check |
||||||
|
* @return true if Map |
||||||
|
*/ |
||||||
|
public boolean isMap(Object obj) { |
||||||
|
return (obj instanceof Map); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,30 +0,0 @@ |
|||||||
package com.jayway.jsonpath; |
|
||||||
|
|
||||||
import org.junit.Test; |
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse; |
|
||||||
import static org.junit.Assert.assertTrue; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: kallestenflo |
|
||||||
* Date: 2/3/11 |
|
||||||
* Time: 9:58 PM |
|
||||||
*/ |
|
||||||
public class IsDefinitePathTest { |
|
||||||
|
|
||||||
|
|
||||||
@Test |
|
||||||
public void path_is_not_definite() throws Exception { |
|
||||||
assertFalse(JsonPath.compile("$..book[0]").isPathDefinite()); |
|
||||||
assertFalse(JsonPath.compile("$.books[*]").isPathDefinite()); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void path_is_definite() throws Exception { |
|
||||||
assertTrue(JsonPath.compile("$.definite.this.is").isPathDefinite()); |
|
||||||
assertTrue(JsonPath.compile("rows[0].id").isPathDefinite()); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,148 @@ |
|||||||
|
package com.jayway.jsonpath; |
||||||
|
|
||||||
|
import org.codehaus.jackson.map.ObjectMapper; |
||||||
|
import org.codehaus.jackson.type.TypeReference; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by IntelliJ IDEA. |
||||||
|
* User: kallestenflo |
||||||
|
* Date: 3/2/12 |
||||||
|
* Time: 10:50 AM |
||||||
|
*/ |
||||||
|
public class JsonModelMapperTest { |
||||||
|
|
||||||
|
public final static String DOCUMENT = |
||||||
|
"{ \"store\": {\n" + |
||||||
|
" \"book\": [ \n" + |
||||||
|
" { \"category\": \"reference\",\n" + |
||||||
|
" \"author\": \"Nigel Rees\",\n" + |
||||||
|
" \"title\": \"Sayings of the Century\",\n" + |
||||||
|
" \"displayPrice\": 8.95\n" + |
||||||
|
" },\n" + |
||||||
|
" { \"category\": \"fiction\",\n" + |
||||||
|
" \"author\": \"Evelyn Waugh\",\n" + |
||||||
|
" \"title\": \"Sword of Honour\",\n" + |
||||||
|
" \"displayPrice\": 12.99\n" + |
||||||
|
" },\n" + |
||||||
|
" { \"category\": \"fiction\",\n" + |
||||||
|
" \"author\": \"Herman Melville\",\n" + |
||||||
|
" \"title\": \"Moby Dick\",\n" + |
||||||
|
" \"isbn\": \"0-553-21311-3\",\n" + |
||||||
|
" \"displayPrice\": 8.99\n" + |
||||||
|
" },\n" + |
||||||
|
" { \"category\": \"fiction\",\n" + |
||||||
|
" \"author\": \"J. R. R. Tolkien\",\n" + |
||||||
|
" \"title\": \"The Lord of the Rings\",\n" + |
||||||
|
" \"isbn\": \"0-395-19395-8\",\n" + |
||||||
|
" \"displayPrice\": 22.99\n" + |
||||||
|
" }\n" + |
||||||
|
" ],\n" + |
||||||
|
" \"bicycle\": {\n" + |
||||||
|
" \"color\": \"red\",\n" + |
||||||
|
" \"displayPrice\": 19.95,\n" + |
||||||
|
" \"foo:bar\": \"fooBar\",\n" + |
||||||
|
" \"dot.notation\": \"new\",\n" + |
||||||
|
" \"dash-notation\": \"dashes\"\n" + |
||||||
|
" }\n" + |
||||||
|
" }\n" + |
||||||
|
"}"; |
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
public void map_a_json_model() throws Exception { |
||||||
|
|
||||||
|
JsonModel model = JsonModel.create(DOCUMENT); |
||||||
|
|
||||||
|
List<Book> booksList = model.map("$.store.book[0,1]").toListOf(Book.class); |
||||||
|
|
||||||
|
Set<Book> bookSet = model.map("$.store.book[0,1]").toSetOf(Book.class); |
||||||
|
|
||||||
|
Book book = model.map("$.store.book[1]").to(Book.class); |
||||||
|
|
||||||
|
System.out.println("test"); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void a_book_can_be_mapped() throws Exception { |
||||||
|
|
||||||
|
|
||||||
|
//JsonPath.convert(DOCUMENT, "$.store.book[0,1]", List.class).to()
|
||||||
|
|
||||||
|
//List books = JsonPath.read(DOCUMENT, "$.store.book[0,1]", List.class);
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper(); |
||||||
|
|
||||||
|
|
||||||
|
/* |
||||||
|
|
||||||
|
|
||||||
|
//Standard
|
||||||
|
List<Map> res = JsonPath.read(DOCUMENT, "$.store.book[0,1]"); |
||||||
|
//or
|
||||||
|
List<Map> res = JsonPath.read(DOCUMENT, "$.store.book[0,1]", List.class); |
||||||
|
|
||||||
|
|
||||||
|
//POJO Mapping med jackson ObjectMapper
|
||||||
|
List<Book> res = JsonPath.asList().of(Book.class).read(DOCUMENT, "$.store.book[0,1]"); |
||||||
|
|
||||||
|
List<Book> res = JsonPath.asListOf(Book.class).read(DOCUMENT, "$.store.book[0,1]"); |
||||||
|
|
||||||
|
Book res = JsonPath.as(Book.class).read(DOCUMENT, "$.store.book[0]"); |
||||||
|
|
||||||
|
*/ |
||||||
|
} |
||||||
|
|
||||||
|
public static class Book { |
||||||
|
private String category; |
||||||
|
private String author; |
||||||
|
private String title; |
||||||
|
private Double displayPrice; |
||||||
|
|
||||||
|
public Book() { |
||||||
|
} |
||||||
|
|
||||||
|
public String getCategory() { |
||||||
|
return category; |
||||||
|
} |
||||||
|
|
||||||
|
public void setCategory(String category) { |
||||||
|
this.category = category; |
||||||
|
} |
||||||
|
|
||||||
|
public String getAuthor() { |
||||||
|
return author; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAuthor(String author) { |
||||||
|
this.author = author; |
||||||
|
} |
||||||
|
|
||||||
|
public String getTitle() { |
||||||
|
return title; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTitle(String title) { |
||||||
|
this.title = title; |
||||||
|
} |
||||||
|
|
||||||
|
public Double getDisplayPrice() { |
||||||
|
return displayPrice; |
||||||
|
} |
||||||
|
|
||||||
|
public void setDisplayPrice(Double displayPrice) { |
||||||
|
this.displayPrice = displayPrice; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,60 +0,0 @@ |
|||||||
package com.jayway.jsonpath; |
|
||||||
|
|
||||||
import org.junit.Test; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: kallestenflo |
|
||||||
* Date: 11/12/11 |
|
||||||
* Time: 8:25 PM |
|
||||||
*/ |
|
||||||
public class JsonPathExtendedTest { |
|
||||||
|
|
||||||
public final static String DOCUMENT = |
|
||||||
"{ \"store\": {\n" + |
|
||||||
" \"book\": [ \n" + |
|
||||||
" { \"category\": \"reference\",\n" + |
|
||||||
" \"author\": \"Nigel Rees\",\n" + |
|
||||||
" \"title\": \"Sayings of the Century\",\n" + |
|
||||||
" \"price\": 8.95\n" + |
|
||||||
" },\n" + |
|
||||||
" { \"category\": \"fiction\",\n" + |
|
||||||
" \"author\": \"Evelyn Waugh\",\n" + |
|
||||||
" \"title\": \"Sword of Honour\",\n" + |
|
||||||
" \"price\": 12.99\n" + |
|
||||||
" },\n" + |
|
||||||
" { \"category\": \"fiction\",\n" + |
|
||||||
" \"author\": \"Herman Melville\",\n" + |
|
||||||
" \"title\": \"Moby Dick\",\n" + |
|
||||||
" \"isbn\": \"0-553-21311-3\",\n" + |
|
||||||
" \"price\": 8.99\n" + |
|
||||||
" },\n" + |
|
||||||
" { \"category\": \"fiction\",\n" + |
|
||||||
" \"author\": \"J. R. R. Tolkien\",\n" + |
|
||||||
" \"title\": \"The Lord of the Rings\",\n" + |
|
||||||
" \"isbn\": \"0-395-19395-8\",\n" + |
|
||||||
" \"price\": 22.99\n" + |
|
||||||
" }\n" + |
|
||||||
" ],\n" + |
|
||||||
" \"bicycle\": {\n" + |
|
||||||
" \"color\": \"red\",\n" + |
|
||||||
" \"price\": 19.95,\n" + |
|
||||||
" \"foo:bar\": \"fooBar\",\n" + |
|
||||||
" \"dot.notation\": \"new\"\n" + |
|
||||||
" }\n" + |
|
||||||
" }\n" + |
|
||||||
"}"; |
|
||||||
|
|
||||||
|
|
||||||
@Test |
|
||||||
public void test_1() throws Exception { |
|
||||||
|
|
||||||
|
|
||||||
JsonPath path = JsonPath.compile("$.store.book[*].title"); |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,45 +0,0 @@ |
|||||||
package com.jayway.jsonpath; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: kallestenflo |
|
||||||
* Date: 6/25/11 |
|
||||||
* Time: 4:16 PM |
|
||||||
*/ |
|
||||||
public class ParserTest { |
|
||||||
|
|
||||||
/* |
|
||||||
private final static String SINGLE_QUOTE_JSON = "{'lhs': '1.0 U.S. dollar','rhs': 6.39778892, 'error': null, 'icc': true}"; |
|
||||||
private final static String NO_QUOTE_JSON = "{lhs: 1.0 U.S. dollar, rhs: 6.39778892, error: null, icc: true}"; |
|
||||||
|
|
||||||
@Test |
|
||||||
public void default_mode_is_SLACK_MODE() throws Exception { |
|
||||||
assertEquals(JsonPath.SLACK_MODE, JsonPath.getMode()); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void slack_mode_allows_single_quotes() throws Exception { |
|
||||||
assertEquals(JsonPath.read(SINGLE_QUOTE_JSON, "lhs"), "1.0 U.S. dollar"); |
|
||||||
assertEquals(JsonPath.read(SINGLE_QUOTE_JSON, "rhs"), 6.39778892D); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void slack_mode_allows_no_quotes() throws Exception { |
|
||||||
assertEquals(JsonPath.read(NO_QUOTE_JSON, "lhs"), "1.0 U.S. dollar"); |
|
||||||
assertEquals(JsonPath.read(NO_QUOTE_JSON, "rhs"), 6.39778892D); |
|
||||||
} |
|
||||||
|
|
||||||
@Test(expected = ParseException.class) |
|
||||||
public void strict_mode_does_not_accept_single_quotes() throws Exception { |
|
||||||
JsonPath.setMode(JsonPath.STRICT_MODE); |
|
||||||
JsonPath.read(SINGLE_QUOTE_JSON, "lhs"); |
|
||||||
} |
|
||||||
|
|
||||||
@Test(expected = ParseException.class) |
|
||||||
public void strict_mode_does_not_accept_no_quotes() throws Exception { |
|
||||||
JsonPath.setMode(JsonPath.STRICT_MODE); |
|
||||||
JsonPath.read(NO_QUOTE_JSON, "lhs"); |
|
||||||
} |
|
||||||
*/ |
|
||||||
|
|
||||||
} |
|
Loading…
Reference in new issue