Browse Source

add support for Tapestry JSON

pull/158/head
Jochen Berger 9 years ago
parent
commit
4d6d4f46a8
  1. 1
      build.gradle
  2. 9
      json-path/build.gradle
  3. 105
      json-path/src/main/java/com/jayway/jsonpath/spi/json/TapestryJsonProvider.java
  4. 55
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/TapestryMappingProvider.java
  5. 8
      json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
  6. 49
      json-path/src/test/java/com/jayway/jsonpath/TapestryJsonProviderTest.java

1
build.gradle

@ -16,6 +16,7 @@ ext {
jacksonDatabind: 'com.fasterxml.jackson.core:jackson-databind:2.6.3',
gson: 'com.google.code.gson:gson:2.3.1',
jsonOrg: 'org.json:json:20140107',
tapestryJson: 'org.apache.tapestry:tapestry-json:5.4-rc-1',
hamcrestCore: 'org.hamcrest:hamcrest-core:1.3',
hamcrestLibrary: 'org.hamcrest:hamcrest-library:1.3',

9
json-path/build.gradle

@ -19,6 +19,7 @@ dependencies {
compile libs.jacksonDatabind, optional
compile libs.gson, optional
compile libs.jsonOrg, optional
compile libs.tapestryJson, optional
testCompile libs.test
}
@ -51,6 +52,10 @@ task distZip(type: Zip, dependsOn: assemble) {
into 'lib-optional/jsonOrg'
include { it.file.name.contains('json-2') }
}
from(project.configurations.compile) {
into 'lib-optional/jsonOrg'
include { it.file.name.contains('tapestry') }
}
}
task distTar(type: Tar, dependsOn: assemble) {
@ -83,6 +88,10 @@ task distTar(type: Tar, dependsOn: assemble) {
into 'lib-optional/jsonOrg'
include { it.file.name.contains('json-2') }
}
from(project.configurations.compile) {
into 'lib-optional/jsonOrg'
include { it.file.name.contains('tapestry') }
}
}
task dist(){

105
json-path/src/main/java/com/jayway/jsonpath/spi/json/TapestryJsonProvider.java

@ -0,0 +1,105 @@
package com.jayway.jsonpath.spi.json;
import java.io.InputStream;
import java.util.Collection;
import java.util.Scanner;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONCollection;
import org.apache.tapestry5.json.JSONObject;
import com.jayway.jsonpath.InvalidJsonException;
public class TapestryJsonProvider extends AbstractJsonProvider {
public static final TapestryJsonProvider INSTANCE = new TapestryJsonProvider();
@Override
public Object parse(final String json) throws InvalidJsonException {
return new JSONObject(json);
}
@Override
public Object parse(final InputStream jsonStream, final String charset) throws InvalidJsonException {
Scanner sc = null;
try {
sc = new Scanner(jsonStream, charset);
return parse(sc.useDelimiter("\\A").next());
} finally {
if (sc != null) {
sc.close();
}
}
}
@Override
public String toJson(final Object obj) {
return ((JSONCollection) obj).toCompactString();
}
@Override
public Object createArray() {
return new JSONArray();
}
@Override
public Object createMap() {
return new JSONObject();
}
@Override
public void setProperty(final Object obj, final Object key, final Object value) {
Object v = value == null ? JSONObject.NULL : value;
if (isMap(obj)) {
((JSONObject) obj).put(key.toString(), v);
}
}
@Override
public boolean isMap(final Object obj) {
return obj instanceof JSONObject;
}
@Override
public Object getArrayIndex(final Object obj, final int idx) {
return ((JSONArray) obj).get(idx);
}
@Override
public Collection<String> getPropertyKeys(final Object obj) {
return ((JSONObject) obj).keys();
}
@Override
public Object getMapValue(final Object obj, final String key) {
JSONObject json = (JSONObject) obj;
if (!json.has(key)) {
return UNDEFINED;
}
return json.get(key);
}
@Override
public int length(final Object obj) {
if (obj instanceof JSONArray) {
return ((JSONArray) obj).length();
} else if (obj instanceof JSONObject) {
return ((JSONObject) obj).length();
} else {
throw new IllegalArgumentException("Cannot determine length of " + obj + ", unsupported type.");
}
}
@Override
public boolean isArray(final Object obj) {
return (obj instanceof JSONArray);
}
@Override
public void setArrayIndex(final Object array, final int index, final Object newValue) {
Object v = newValue == null ? JSONObject.NULL : newValue;
JSONArray list = (JSONArray) array;
list.put(index, v);
}
}

55
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/TapestryMappingProvider.java

@ -0,0 +1,55 @@
/*
* 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.spi.mapper;
import java.util.ArrayList;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.TypeRef;
public class TapestryMappingProvider implements MappingProvider {
@Override
public <T> T map(final Object source, final Class<T> targetType, final Configuration configuration) {
if (source == null) {
return null;
}
if (targetType.isAssignableFrom(source.getClass())) {
return (T) source;
}
try {
if (targetType.isAssignableFrom(ArrayList.class) && configuration.jsonProvider().isArray(source)) {
int length = configuration.jsonProvider().length(source);
@SuppressWarnings("rawtypes")
ArrayList list = new ArrayList(length);
for (Object o : configuration.jsonProvider().toIterable(source)) {
list.add(o);
}
return (T) list;
}
} catch (Exception e) {
}
throw new MappingException("Cannot convert a " + source.getClass().getName() + " to a " + targetType
+ " use Tapestry's TypeCoercer instead.");
}
@Override
public <T> T map(final Object source, final TypeRef<T> targetType, final Configuration configuration) {
throw new UnsupportedOperationException(
"Tapestry JSON provider does not support TypeRef! Use a Jackson or Gson based provider");
}
}

8
json-path/src/test/java/com/jayway/jsonpath/BaseTest.java

@ -7,10 +7,12 @@ import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider;
import com.jayway.jsonpath.spi.json.TapestryJsonProvider;
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.JsonOrgMappingProvider;
import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider;
import com.jayway.jsonpath.spi.mapper.TapestryMappingProvider;
import java.util.HashMap;
@ -45,6 +47,12 @@ public class BaseTest {
.mappingProvider(new JsonSmartMappingProvider())
.jsonProvider(new JsonSmartJsonProvider())
.build();
public static final Configuration TAPESTRY_JSON_CONFIGURATION = Configuration
.builder()
.mappingProvider(new TapestryMappingProvider())
.jsonProvider(TapestryJsonProvider.INSTANCE)
.build();
public static final String JSON_BOOK_DOCUMENT =
"{ " +

49
json-path/src/test/java/com/jayway/jsonpath/TapestryJsonProviderTest.java

@ -0,0 +1,49 @@
package com.jayway.jsonpath;
import static com.jayway.jsonpath.JsonPath.using;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import java.util.Map;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;
import org.junit.Test;
public class TapestryJsonProviderTest extends BaseTest {
@Test
public void an_object_can_be_read() {
JSONObject book = using(TAPESTRY_JSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[0]");
assertThat(book.get("author").toString()).isEqualTo("Nigel Rees");
}
@Test
public void a_property_can_be_read() {
String category = using(TAPESTRY_JSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[0].category");
assertThat(category).isEqualTo("reference");
}
@Test
public void a_filter_can_be_applied() {
JSONArray fictionBooks = using(TAPESTRY_JSON_CONFIGURATION).parse(JSON_DOCUMENT)
.read("$.store.book[?(@.category == 'fiction')]");
assertThat(fictionBooks.length()).isEqualTo(3);
}
@Test
public void result_can_be_mapped_to_object() {
List<Map<String, Object>> books = using(TAPESTRY_JSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book",
List.class);
assertThat(books.size()).isEqualTo(4);
}
}
Loading…
Cancel
Save