File tree Expand file tree Collapse file tree 6 files changed +43
-5
lines changed
main/java/io/avaje/json/simple
test/java/io/avaje/json/simple
main/java/io/avaje/json/node
test/java/io/avaje/json/node Expand file tree Collapse file tree 6 files changed +43
-5
lines changed Original file line number Diff line number Diff line change 9
9
10
10
import java .util .List ;
11
11
import java .util .Map ;
12
+ import java .util .function .Function ;
12
13
13
14
final class DSimpleMapper implements SimpleMapper {
14
15
@@ -34,6 +35,11 @@ public <T> Type<T> type(JsonAdapter<T> myAdapter) {
34
35
return new DTypeMapper <>(myAdapter , jsonStream );
35
36
}
36
37
38
+ @ Override
39
+ public <T > Type <T > type (Function <SimpleMapper , JsonAdapter <T >> adapterFunction ) {
40
+ return type (adapterFunction .apply (this ));
41
+ }
42
+
37
43
@ Override
38
44
public Type <Object > object () {
39
45
return objectType ;
Original file line number Diff line number Diff line change 12
12
import java .io .Writer ;
13
13
import java .util .List ;
14
14
import java .util .Map ;
15
+ import java .util .function .Function ;
15
16
16
17
/**
17
18
* A mapper for mapping to basic Java types.
@@ -136,6 +137,18 @@ static Builder builder() {
136
137
*/
137
138
<T > Type <T > type (JsonAdapter <T > customAdapter );
138
139
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
+
139
152
default JsonExtract extract (Map <String , Object > map ) {
140
153
return new DExtract (map );
141
154
}
Original file line number Diff line number Diff line change @@ -21,13 +21,15 @@ class CustomAdapterTest {
21
21
22
22
@ Test
23
23
void mapUsingCustomAdapter () {
24
+ SimpleMapper mapper = SimpleMapper .builder ().build ();
25
+ SimpleMapper .Type <MyCustomType > myType = mapper .type (MyAdapter ::new );
24
26
25
27
MyCustomType source = new MyCustomType ();
26
28
source .foo = "hi" ;
27
29
source .bar = 42 ;
28
- String asJson = type .toJson (source );
30
+ String asJson = myType .toJson (source );
29
31
30
- MyCustomType fromJson = type .fromJson (asJson );
32
+ MyCustomType fromJson = myType .fromJson (asJson );
31
33
32
34
assertThat (fromJson .foo ).isEqualTo (source .foo );
33
35
assertThat (fromJson .bar ).isEqualTo (source .bar );
Original file line number Diff line number Diff line change 13
13
import java .io .Reader ;
14
14
import java .io .Writer ;
15
15
import java .lang .reflect .Type ;
16
+ import java .util .function .Function ;
16
17
17
18
/**
18
19
* Provide JsonAdapters for the JsonNode types.
@@ -204,6 +205,18 @@ static Builder builder() {
204
205
*/
205
206
<T > SimpleMapper .Type <T > type (JsonAdapter <T > customAdapter );
206
207
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
+
207
220
/**
208
221
* Build the JsonNodeMapper.
209
222
*/
Original file line number Diff line number Diff line change 9
9
import io .avaje .json .stream .JsonStream ;
10
10
11
11
import java .lang .reflect .Type ;
12
+ import java .util .function .Function ;
12
13
13
14
final class DJsonNodeMapper implements JsonNodeMapper {
14
15
@@ -42,6 +43,11 @@ public <T> SimpleMapper.Type<T> type(JsonAdapter<T> customAdapter) {
42
43
return new DMapper <>(customAdapter , jsonStream );
43
44
}
44
45
46
+ @ Override
47
+ public <T > SimpleMapper .Type <T > type (Function <JsonNodeMapper , JsonAdapter <T >> adapterFunction ) {
48
+ return type (adapterFunction .apply (this ));
49
+ }
50
+
45
51
@ Override
46
52
public SimpleMapper .Type <JsonNode > nodeMapper () {
47
53
return new DMapper <>(nodeAdapter , jsonStream );
Original file line number Diff line number Diff line change @@ -21,9 +21,7 @@ class CustomAdapterTest {
21
21
22
22
@ Test
23
23
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 );
27
25
28
26
var source = as ("a" , 1 );
29
27
String asJson = type .toJson (source );
You can’t perform that action at this time.
0 commit comments