Skip to content

Commit

Permalink
Add helper method for type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglingsong committed Feb 1, 2018
1 parent f51a6da commit 38488af
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
10 changes: 10 additions & 0 deletions jsurfer-all/src/test/java/org/jsfr/json/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class Book {

private Double price;

private String isbn;

public String getCategory() {
return category;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 34 additions & 5 deletions jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Object> collection = surfer.collectAll("[\n" +
Expand All @@ -90,8 +119,8 @@ public void testTypeBindingOne() throws Exception {
@Test
public void testTypeBindingCollection() throws Exception {
Reader reader = read("sample.json");
Collection<Book> book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]"));
assertEquals(2, book.size());
Collection<Book> book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[*]"));
assertEquals(4, book.size());
assertEquals("Nigel Rees", book.iterator().next().getAuthor());
}

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions jsurfer-core/src/main/java/org/jsfr/json/ParsingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ public interface ParsingContext {
*/
<T> T load(String key, Class<T> tClass);

/**
* @param object object to cast
* @param tClass type
* @param <T> type
* @return casted object
*/
<T> T cast(Object object, Class<T> tClass);

}
5 changes: 5 additions & 0 deletions jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ public <T> T load(String key, Class<T> tClass) {
return this.transientMap != null ? tClass.cast(this.transientMap.get(key)) : null;
}

@Override
public <T> T cast(Object object, Class<T> tClass) {
return (T) this.config.getJsonProvider().cast(object, tClass);
}

public boolean shouldBreak() {
return this.stopped || this.paused;
}
Expand Down

0 comments on commit 38488af

Please sign in to comment.