Skip to content

Commit a867395

Browse files
authored
[core] Add type() method to SimpleMapper that takes a function that creates JsonAdapter (#328)
* Add pom descriptions for json-core and json-node + more consistent pom names * [core] Add type() method to SimpleMapper that takes a function that creates JsonAdapter Typically, the function is the constructor of the JsonAdapter. This makes it slightly cleaner to create the SimpleMapper.Type, for example: SimpleMapper mapper = SimpleMapper.builder().build(); SimpleMapper.Type<MyCustomType> myType = mapper.type(MyAdapter::new);
1 parent e43581b commit a867395

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

json-core/src/main/java/io/avaje/json/simple/DSimpleMapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.function.Function;
1213

1314
final class DSimpleMapper implements SimpleMapper {
1415

@@ -34,6 +35,11 @@ public <T> Type<T> type(JsonAdapter<T> myAdapter) {
3435
return new DTypeMapper<>(myAdapter, jsonStream);
3536
}
3637

38+
@Override
39+
public <T> Type<T> type(Function<SimpleMapper, JsonAdapter<T>> adapterFunction) {
40+
return type(adapterFunction.apply(this));
41+
}
42+
3743
@Override
3844
public Type<Object> object() {
3945
return objectType;

json-core/src/main/java/io/avaje/json/simple/SimpleMapper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.Writer;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.function.Function;
1516

1617
/**
1718
* A mapper for mapping to basic Java types.
@@ -136,6 +137,18 @@ static Builder builder() {
136137
*/
137138
<T> Type<T> type(JsonAdapter<T> customAdapter);
138139

140+
/**
141+
* Return a Type specific mapper using a function that creates a JsonAdapter.
142+
* <p>
143+
* Often the adapterFunction is the constructor of the custom JsonAdapter where
144+
* the constructor takes SimpleMapper as the only argument.
145+
*
146+
* @param adapterFunction The function that creates a JsonAdapter.
147+
* @param <T> The type of the class to map to/from json.
148+
* @return The Type specific mapper.
149+
*/
150+
<T> Type<T> type(Function<SimpleMapper, JsonAdapter<T>> adapterFunction);
151+
139152
default JsonExtract extract(Map<String, Object> map) {
140153
return new DExtract(map);
141154
}

json-core/src/test/java/io/avaje/json/simple/CustomAdapterTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class CustomAdapterTest {
2121

2222
@Test
2323
void mapUsingCustomAdapter() {
24+
SimpleMapper mapper = SimpleMapper.builder().build();
25+
SimpleMapper.Type<MyCustomType> myType = mapper.type(MyAdapter::new);
2426

2527
MyCustomType source = new MyCustomType();
2628
source.foo = "hi";
2729
source.bar = 42;
28-
String asJson = type.toJson(source);
30+
String asJson = myType.toJson(source);
2931

30-
MyCustomType fromJson = type.fromJson(asJson);
32+
MyCustomType fromJson = myType.fromJson(asJson);
3133

3234
assertThat(fromJson.foo).isEqualTo(source.foo);
3335
assertThat(fromJson.bar).isEqualTo(source.bar);

json-node/src/main/java/io/avaje/json/node/JsonNodeMapper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.Reader;
1414
import java.io.Writer;
1515
import java.lang.reflect.Type;
16+
import java.util.function.Function;
1617

1718
/**
1819
* Provide JsonAdapters for the JsonNode types.
@@ -204,6 +205,18 @@ static Builder builder() {
204205
*/
205206
<T> SimpleMapper.Type<T> type(JsonAdapter<T> customAdapter);
206207

208+
/**
209+
* Return a Type specific mapper using a function that creates a JsonAdapter.
210+
* <p>
211+
* Often the adapterFunction is the constructor of the custom JsonAdapter where
212+
* the constructor takes JsonNodeMapper as the only argument.
213+
*
214+
* @param adapterFunction The function that creates a JsonAdapter.
215+
* @param <T> The type of the class to map to/from json.
216+
* @return The Type specific mapper.
217+
*/
218+
<T> SimpleMapper.Type<T> type(Function<JsonNodeMapper, JsonAdapter<T>> adapterFunction);
219+
207220
/**
208221
* Build the JsonNodeMapper.
209222
*/

json-node/src/main/java/io/avaje/json/node/adapter/DJsonNodeMapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.avaje.json.stream.JsonStream;
1010

1111
import java.lang.reflect.Type;
12+
import java.util.function.Function;
1213

1314
final class DJsonNodeMapper implements JsonNodeMapper {
1415

@@ -42,6 +43,11 @@ public <T> SimpleMapper.Type<T> type(JsonAdapter<T> customAdapter) {
4243
return new DMapper<>(customAdapter, jsonStream);
4344
}
4445

46+
@Override
47+
public <T> SimpleMapper.Type<T> type(Function<JsonNodeMapper, JsonAdapter<T>> adapterFunction) {
48+
return type(adapterFunction.apply(this));
49+
}
50+
4551
@Override
4652
public SimpleMapper.Type<JsonNode> nodeMapper() {
4753
return new DMapper<>(nodeAdapter, jsonStream);

json-node/src/test/java/io/avaje/json/node/CustomAdapterTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class CustomAdapterTest {
2121

2222
@Test
2323
void propertyNames() {
24-
25-
var adapter = new MyAdapter2(mapper);
26-
SimpleMapper.Type<MyCustomType> type = mapper.type(adapter);
24+
SimpleMapper.Type<MyCustomType> type = mapper.type(MyAdapter2::new);
2725

2826
var source = as("a", 1);
2927
String asJson = type.toJson(source);

0 commit comments

Comments
 (0)