Skip to content

Commit 38488af

Browse files
committed
Add helper method for type casting
1 parent f51a6da commit 38488af

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

jsurfer-all/src/test/java/org/jsfr/json/Book.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class Book {
3232

3333
private Double price;
3434

35+
private String isbn;
36+
3537
public String getCategory() {
3638
return category;
3739
}
@@ -63,4 +65,12 @@ public Double getPrice() {
6365
public void setPrice(Double price) {
6466
this.price = price;
6567
}
68+
69+
public String getIsbn() {
70+
return isbn;
71+
}
72+
73+
public void setIsbn(String isbn) {
74+
this.isbn = isbn;
75+
}
6676
}

jsurfer-all/src/test/java/org/jsfr/json/JsonSimpleParserTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public void setUp() throws Exception {
1616
surfer = new JsonSurfer(JsonSimpleParser.INSTANCE, provider);
1717
}
1818

19+
@Ignore
20+
@Override
21+
public void testTypeCasting() throws Exception {
22+
// ignore
23+
}
24+
25+
@Ignore
1926
@Override
2027
public void testTypeBindingOne() throws Exception {
2128
// ignore

jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import java.io.IOException;
3939
import java.io.InputStreamReader;
4040
import java.io.Reader;
41-
import java.nio.charset.StandardCharsets;
41+
import java.nio.charset.Charset;
4242
import java.util.Collection;
4343
import java.util.HashMap;
4444
import java.util.Iterator;
@@ -64,6 +64,35 @@ public void onValue(Object value, ParsingContext context) {
6464
}
6565
};
6666

67+
@Test
68+
public void testTypeCasting() throws Exception {
69+
surfer.configBuilder()
70+
.bind("$.store.book[*]", new JsonPathListener() {
71+
@Override
72+
public void onValue(Object value, ParsingContext context) {
73+
assertNotNull(context.cast(value, Book.class));
74+
}
75+
})
76+
.bind("$.expensive", new JsonPathListener() {
77+
@Override
78+
public void onValue(Object value, ParsingContext context) {
79+
assertEquals(10L, context.cast(value, Long.class).longValue());
80+
}
81+
})
82+
.bind("$.store.book[0].price", new JsonPathListener() {
83+
@Override
84+
public void onValue(Object value, ParsingContext context) {
85+
assertEquals(8.95d, context.cast(value, Double.class), 0);
86+
}
87+
})
88+
.bind("$.store.book[1].title", new JsonPathListener() {
89+
@Override
90+
public void onValue(Object value, ParsingContext context) {
91+
assertEquals("Sword of Honour", context.cast(value, String.class));
92+
}
93+
}).buildAndSurf(read("sample.json"));
94+
}
95+
6796
@Test
6897
public void testWildcardAtRoot() throws Exception {
6998
Collection<Object> collection = surfer.collectAll("[\n" +
@@ -90,8 +119,8 @@ public void testTypeBindingOne() throws Exception {
90119
@Test
91120
public void testTypeBindingCollection() throws Exception {
92121
Reader reader = read("sample.json");
93-
Collection<Book> book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]"));
94-
assertEquals(2, book.size());
122+
Collection<Book> book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[*]"));
123+
assertEquals(4, book.size());
95124
assertEquals("Nigel Rees", book.iterator().next().getAuthor());
96125
}
97126

@@ -393,11 +422,11 @@ public void testAnyIndex() throws Exception {
393422
}
394423

395424
protected Reader read(String resourceName) throws IOException {
396-
return new InputStreamReader(Resources.getResource(resourceName).openStream(), StandardCharsets.UTF_8);
425+
return new InputStreamReader(Resources.getResource(resourceName).openStream(), Charset.forName("UTF-8"));
397426
}
398427

399428
protected String readAsString(String resourceName) throws IOException {
400-
return Resources.toString(Resources.getResource(resourceName), StandardCharsets.UTF_8);
429+
return Resources.toString(Resources.getResource(resourceName), Charset.forName("UTF-8"));
401430
}
402431

403432
@Test

jsurfer-core/src/main/java/org/jsfr/json/ParsingContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ public interface ParsingContext {
8585
*/
8686
<T> T load(String key, Class<T> tClass);
8787

88+
/**
89+
* @param object object to cast
90+
* @param tClass type
91+
* @param <T> type
92+
* @return casted object
93+
*/
94+
<T> T cast(Object object, Class<T> tClass);
95+
8896
}

jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ public <T> T load(String key, Class<T> tClass) {
276276
return this.transientMap != null ? tClass.cast(this.transientMap.get(key)) : null;
277277
}
278278

279+
@Override
280+
public <T> T cast(Object object, Class<T> tClass) {
281+
return (T) this.config.getJsonProvider().cast(object, tClass);
282+
}
283+
279284
public boolean shouldBreak() {
280285
return this.stopped || this.paused;
281286
}

0 commit comments

Comments
 (0)