Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from jochenberger/support-tapestry-json
add support for Tapestry JSON
- Loading branch information
Showing
6 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
json-path/src/main/java/com/jayway/jsonpath/spi/json/TapestryJsonProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 55 additions & 0 deletions
55
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/TapestryMappingProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
json-path/src/test/java/com/jayway/jsonpath/TapestryJsonProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |