From 38488afa4fe7b036478d56d5e417472e8e26500c Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 1 Feb 2018 16:09:30 +0800 Subject: [PATCH] Add helper method for type casting --- .../src/test/java/org/jsfr/json/Book.java | 10 +++++ .../org/jsfr/json/JsonSimpleParserTest.java | 7 ++++ .../java/org/jsfr/json/JsonSurferTest.java | 39 ++++++++++++++++--- .../java/org/jsfr/json/ParsingContext.java | 8 ++++ .../java/org/jsfr/json/SurfingContext.java | 5 +++ 5 files changed, 64 insertions(+), 5 deletions(-) diff --git a/jsurfer-all/src/test/java/org/jsfr/json/Book.java b/jsurfer-all/src/test/java/org/jsfr/json/Book.java index 4a6943a..4f5ddd0 100644 --- a/jsurfer-all/src/test/java/org/jsfr/json/Book.java +++ b/jsurfer-all/src/test/java/org/jsfr/json/Book.java @@ -32,6 +32,8 @@ public class Book { private Double price; + private String isbn; + public String getCategory() { return category; } @@ -63,4 +65,12 @@ public Double getPrice() { public void setPrice(Double price) { this.price = price; } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } } diff --git a/jsurfer-all/src/test/java/org/jsfr/json/JsonSimpleParserTest.java b/jsurfer-all/src/test/java/org/jsfr/json/JsonSimpleParserTest.java index 6d45056..d0be08b 100644 --- a/jsurfer-all/src/test/java/org/jsfr/json/JsonSimpleParserTest.java +++ b/jsurfer-all/src/test/java/org/jsfr/json/JsonSimpleParserTest.java @@ -16,6 +16,13 @@ public void setUp() throws Exception { surfer = new JsonSurfer(JsonSimpleParser.INSTANCE, provider); } + @Ignore + @Override + public void testTypeCasting() throws Exception { + // ignore + } + + @Ignore @Override public void testTypeBindingOne() throws Exception { // ignore diff --git a/jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java b/jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java index ffcc96d..ba1f119 100644 --- a/jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java +++ b/jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java @@ -38,7 +38,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -64,6 +64,35 @@ public void onValue(Object value, ParsingContext context) { } }; + @Test + public void testTypeCasting() throws Exception { + surfer.configBuilder() + .bind("$.store.book[*]", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) { + assertNotNull(context.cast(value, Book.class)); + } + }) + .bind("$.expensive", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) { + assertEquals(10L, context.cast(value, Long.class).longValue()); + } + }) + .bind("$.store.book[0].price", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) { + assertEquals(8.95d, context.cast(value, Double.class), 0); + } + }) + .bind("$.store.book[1].title", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) { + assertEquals("Sword of Honour", context.cast(value, String.class)); + } + }).buildAndSurf(read("sample.json")); + } + @Test public void testWildcardAtRoot() throws Exception { Collection collection = surfer.collectAll("[\n" + @@ -90,8 +119,8 @@ public void testTypeBindingOne() throws Exception { @Test public void testTypeBindingCollection() throws Exception { Reader reader = read("sample.json"); - Collection book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]")); - assertEquals(2, book.size()); + Collection book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[*]")); + assertEquals(4, book.size()); assertEquals("Nigel Rees", book.iterator().next().getAuthor()); } @@ -393,11 +422,11 @@ public void testAnyIndex() throws Exception { } protected Reader read(String resourceName) throws IOException { - return new InputStreamReader(Resources.getResource(resourceName).openStream(), StandardCharsets.UTF_8); + return new InputStreamReader(Resources.getResource(resourceName).openStream(), Charset.forName("UTF-8")); } protected String readAsString(String resourceName) throws IOException { - return Resources.toString(Resources.getResource(resourceName), StandardCharsets.UTF_8); + return Resources.toString(Resources.getResource(resourceName), Charset.forName("UTF-8")); } @Test diff --git a/jsurfer-core/src/main/java/org/jsfr/json/ParsingContext.java b/jsurfer-core/src/main/java/org/jsfr/json/ParsingContext.java index 8e7e3ec..2e122d2 100644 --- a/jsurfer-core/src/main/java/org/jsfr/json/ParsingContext.java +++ b/jsurfer-core/src/main/java/org/jsfr/json/ParsingContext.java @@ -85,4 +85,12 @@ public interface ParsingContext { */ T load(String key, Class tClass); + /** + * @param object object to cast + * @param tClass type + * @param type + * @return casted object + */ + T cast(Object object, Class tClass); + } diff --git a/jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java b/jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java index 090b1e2..cc482ff 100644 --- a/jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java +++ b/jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java @@ -276,6 +276,11 @@ public T load(String key, Class tClass) { return this.transientMap != null ? tClass.cast(this.transientMap.get(key)) : null; } + @Override + public T cast(Object object, Class tClass) { + return (T) this.config.getJsonProvider().cast(object, tClass); + } + public boolean shouldBreak() { return this.stopped || this.paused; }