Skip to content

Commit e1ee31f

Browse files
authored
[core] Refactor rename SimpleMapper to JsonMapper (+ package simple -> mapper) (#329)
* [core] Refactor rename SimpleMapper to JsonMapper (+ package simple -> mapper) * [jsonb] Change JsonB JsonType to also extends JsonMapper.Type * [core] Update javadoc and readme + rename internal DSimpleMapper
1 parent a867395 commit e1ee31f

File tree

18 files changed

+125
-117
lines changed

18 files changed

+125
-117
lines changed

json-core/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Provides the core API including JsonAdapter, JsonReader, JsonWriter, JsonStream
1212
</dependency>
1313
```
1414

15-
## SimpleMapper
15+
## JsonMapper
1616

1717
If you only have simple use cases you can use avaje-json
1818
without avaje-jsonb.
@@ -26,9 +26,9 @@ For use cases that only want to map to the following types:
2626
- Map<String, Object>
2727
- List<Object>
2828

29-
### Create a SimpleMapper
29+
### Create a JsonMapper
3030

31-
static final SimpleMapper mapper = SimpleMapper.builder().build()
31+
static final JsonMapper mapper = JsonMapper.builder().build()
3232

3333

3434
### Map example

json-core/src/main/java/io/avaje/json/simple/DExtract.java renamed to json-core/src/main/java/io/avaje/json/mapper/DExtract.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.json.simple;
1+
package io.avaje.json.mapper;
22

33
import java.util.Map;
44
import java.util.Optional;

json-core/src/main/java/io/avaje/json/simple/DSimpleMapper.java renamed to json-core/src/main/java/io/avaje/json/mapper/DJsonMapper.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.json.simple;
1+
package io.avaje.json.mapper;
22

33
import io.avaje.json.JsonAdapter;
44
import io.avaje.json.JsonReader;
@@ -11,14 +11,14 @@
1111
import java.util.Map;
1212
import java.util.function.Function;
1313

14-
final class DSimpleMapper implements SimpleMapper {
14+
final class DJsonMapper implements JsonMapper {
1515

1616
private final JsonStream jsonStream;
1717
private final Type<Object> objectType;
1818
private final Type<Map<String,Object>> mapType;
1919
private final Type<List<Object>> listType;
2020

21-
DSimpleMapper(JsonStream jsonStream, CoreTypes.CoreAdapters adapters) {
21+
DJsonMapper(JsonStream jsonStream, CoreTypes.CoreAdapters adapters) {
2222
this.jsonStream = jsonStream;
2323
this.objectType = new DTypeMapper<>(adapters.objectAdapter(), jsonStream);
2424
this.mapType = new DTypeMapper<>(adapters.mapAdapter(), jsonStream);
@@ -36,7 +36,7 @@ public <T> Type<T> type(JsonAdapter<T> myAdapter) {
3636
}
3737

3838
@Override
39-
public <T> Type<T> type(Function<SimpleMapper, JsonAdapter<T>> adapterFunction) {
39+
public <T> Type<T> type(Function<JsonMapper, JsonAdapter<T>> adapterFunction) {
4040
return type(adapterFunction.apply(this));
4141
}
4242

Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
package io.avaje.json.simple;
1+
package io.avaje.json.mapper;
22

33
import io.avaje.json.core.CoreTypes;
44
import io.avaje.json.stream.JsonStream;
55

6-
final class DSimpleMapperBuilder implements SimpleMapper.Builder {
6+
final class DJsonMapperBuilder implements JsonMapper.Builder {
77

88
private JsonStream jsonStream;
99

1010
@Override
11-
public SimpleMapper.Builder jsonStream(JsonStream jsonStream) {
11+
public JsonMapper.Builder jsonStream(JsonStream jsonStream) {
1212
this.jsonStream = jsonStream;
1313
return this;
1414
}
1515

1616
@Override
17-
public SimpleMapper build() {
17+
public JsonMapper build() {
1818
final var stream = jsonStream != null ? jsonStream : JsonStream.builder().build();
1919
final var coreAdapters = CoreTypes.createCoreAdapters();
20-
return new DSimpleMapper(stream, coreAdapters);
20+
return new DJsonMapper(stream, coreAdapters);
2121
}
2222
}

json-core/src/main/java/io/avaje/json/simple/DTypeMapper.java renamed to json-core/src/main/java/io/avaje/json/mapper/DTypeMapper.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.json.simple;
1+
package io.avaje.json.mapper;
22

33
import io.avaje.json.JsonAdapter;
44
import io.avaje.json.JsonException;
@@ -13,7 +13,7 @@
1313
import java.util.List;
1414
import java.util.Map;
1515

16-
final class DTypeMapper<T> implements SimpleMapper.Type<T> {
16+
final class DTypeMapper<T> implements JsonMapper.Type<T> {
1717

1818
private final JsonAdapter<T> adapter;
1919
private final JsonStream jsonStream;
@@ -24,13 +24,13 @@ final class DTypeMapper<T> implements SimpleMapper.Type<T> {
2424
}
2525

2626
@Override
27-
public SimpleMapper.Type<List<T>> list() {
27+
public JsonMapper.Type<List<T>> list() {
2828
final JsonAdapter<List<T>> list = CoreTypes.createList(adapter);
2929
return new DTypeMapper<>(list, jsonStream);
3030
}
3131

3232
@Override
33-
public SimpleMapper.Type<Map<String, T>> map() {
33+
public JsonMapper.Type<Map<String, T>> map() {
3434
final JsonAdapter<Map<String, T>> map = CoreTypes.createMap(adapter);
3535
return new DTypeMapper<>(map, jsonStream);
3636
}

json-core/src/main/java/io/avaje/json/simple/JsonExtract.java renamed to json-core/src/main/java/io/avaje/json/mapper/JsonExtract.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.json.simple;
1+
package io.avaje.json.mapper;
22

33
import java.util.Map;
44
import java.util.Optional;
@@ -15,9 +15,9 @@
1515
* <pre>{@code
1616
*
1717
* String json = "{\"name\":\"Rob\",\"score\":4.5,\"whenActive\":\"2025-10-20\",\"address\":{\"street\":\"Pall Mall\"}}";
18-
* Map<String, Object> mapFromJson = simpleMapper.fromJsonObject(json);
18+
* Map<String, Object> mapFromJson = jsonMapper.fromJsonObject(json);
1919
*
20-
* JsonExtract jsonExtract = simpleMapper.extract(mapFromJson);
20+
* JsonExtract jsonExtract = jsonMapper.extract(mapFromJson);
2121
*
2222
* String name = jsonExtract.extract("name");
2323
* double score = jsonExtract.extract("score", -1D);

json-core/src/main/java/io/avaje/json/simple/SimpleMapper.java renamed to json-core/src/main/java/io/avaje/json/mapper/JsonMapper.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.json.simple;
1+
package io.avaje.json.mapper;
22

33
import io.avaje.json.JsonAdapter;
44
import io.avaje.json.JsonReader;
@@ -20,28 +20,32 @@
2020
* This supports the basic Java types of String, Boolean, Integer, Long, Double and
2121
* Maps and List of these.
2222
* <p>
23+
* If avaje-jsonb is available then you will use that and NOT use this JsonMapper at all.
24+
* This JsonMapper is intended to be used by code that ONLY wants to depend on avaje-json-core
25+
* and process the basic Java types or provide its own JsonAdapters.
26+
* <p>
2327
* For full support with more types and binding to custom types use avaje-jsonb instead.
2428
*
2529
* <h3>Example</h3>
2630
* <pre>{@code
2731
*
28-
* static final SimpleMapper simpleMapper = SimpleMapper.builder().build();
32+
* static final JsonMapper jsonMapper = JsonMapper.builder().build();
2933
*
3034
* Map<String, Long> map = new LinkedHashMap<>();
3135
* map.put("one", 45L);
3236
* map.put("two", 93L);
3337
*
34-
* String asJson = simpleMapper.toJson(map);
38+
* String asJson = jsonMapper.toJson(map);
3539
*
3640
* }</pre>
3741
*/
38-
public interface SimpleMapper {
42+
public interface JsonMapper {
3943

4044
/**
41-
* Create a new builder for SimpleMapper.
45+
* Create a new builder for JsonMapper.
4246
*/
4347
static Builder builder() {
44-
return new DSimpleMapperBuilder();
48+
return new DJsonMapperBuilder();
4549
}
4650

4751
/**
@@ -141,14 +145,17 @@ static Builder builder() {
141145
* Return a Type specific mapper using a function that creates a JsonAdapter.
142146
* <p>
143147
* Often the adapterFunction is the constructor of the custom JsonAdapter where
144-
* the constructor takes SimpleMapper as the only argument.
148+
* the constructor takes JsonMapper as the only argument.
145149
*
146150
* @param adapterFunction The function that creates a JsonAdapter.
147151
* @param <T> The type of the class to map to/from json.
148152
* @return The Type specific mapper.
149153
*/
150-
<T> Type<T> type(Function<SimpleMapper, JsonAdapter<T>> adapterFunction);
154+
<T> Type<T> type(Function<JsonMapper, JsonAdapter<T>> adapterFunction);
151155

156+
/**
157+
* Return the map wrapped via JsonExtract to make extracting values easier.
158+
*/
152159
default JsonExtract extract(Map<String, Object> map) {
153160
return new DExtract(map);
154161
}
@@ -170,7 +177,7 @@ interface Builder {
170177
/**
171178
* Build and return the JsonNodeMapper.
172179
*/
173-
SimpleMapper build();
180+
JsonMapper build();
174181
}
175182

176183
/**
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module io.avaje.json {
22

33
exports io.avaje.json;
4-
exports io.avaje.json.simple;
54
exports io.avaje.json.stream;
65
exports io.avaje.json.view;
76
exports io.avaje.json.core to io.avaje.jsonb, io.avaje.json.node;
7+
exports io.avaje.json.mapper;
88

99
requires static io.helidon.webserver;
1010
}

json-core/src/test/java/io/avaje/json/simple/CustomAdapter2Test.java renamed to json-core/src/test/java/io/avaje/json/mapper/CustomAdapter2Test.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.json.simple;
1+
package io.avaje.json.mapper;
22

33
import io.avaje.json.JsonAdapter;
44
import io.avaje.json.JsonReader;
@@ -10,15 +10,15 @@
1010

1111
class CustomAdapter2Test {
1212

13-
static final SimpleMapper simpleMapper = SimpleMapper.builder().build();
13+
static final JsonMapper mapper = JsonMapper.builder().build();
1414

1515
@Test
1616
void mapUsingCustomAdapter() {
1717

18-
PropertyNames names = simpleMapper.properties("foo", "bar");
18+
PropertyNames names = mapper.properties("foo", "bar");
1919
MyAdapterUsingRaw myAdapter = new MyAdapterUsingRaw(names);
2020

21-
SimpleMapper.Type<MyOtherType> type = simpleMapper.type(myAdapter);
21+
JsonMapper.Type<MyOtherType> type = mapper.type(myAdapter);
2222

2323
MyOtherType source = new MyOtherType();
2424
source.foo = "hi";

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.json.simple;
1+
package io.avaje.json.mapper;
22

33
import io.avaje.json.JsonAdapter;
44
import io.avaje.json.JsonReader;
@@ -15,14 +15,14 @@
1515
class CustomAdapterTest {
1616

1717
static final JsonStream jsonStream = JsonStream.builder().build();
18-
static final SimpleMapper simpleMapper = SimpleMapper.builder().jsonStream(jsonStream).build();
19-
static final MyAdapter myAdapter = new MyAdapter(simpleMapper);
20-
static final SimpleMapper.Type<MyCustomType> type = simpleMapper.type(myAdapter);
18+
static final JsonMapper mapper = JsonMapper.builder().jsonStream(jsonStream).build();
19+
static final MyAdapter myAdapter = new MyAdapter(mapper);
20+
static final JsonMapper.Type<MyCustomType> type = mapper.type(myAdapter);
2121

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

2727
MyCustomType source = new MyCustomType();
2828
source.foo = "hi";
@@ -37,7 +37,7 @@ void mapUsingCustomAdapter() {
3737

3838
@Test
3939
void list() {
40-
SimpleMapper.Type<List<MyCustomType>> listType = type.list();
40+
JsonMapper.Type<List<MyCustomType>> listType = type.list();
4141

4242
var v0 = as("a", 1);
4343
var v1 = as("b", 2);
@@ -51,7 +51,7 @@ void list() {
5151

5252
@Test
5353
void map() {
54-
SimpleMapper.Type<Map<String, MyCustomType>> mapType = type.map();
54+
JsonMapper.Type<Map<String, MyCustomType>> mapType = type.map();
5555

5656
var v0 = as("a", 1);
5757
var v1 = as("b", 2);
@@ -90,10 +90,10 @@ public int hashCode() {
9090

9191
static class MyAdapter implements JsonAdapter<MyCustomType> {
9292

93-
private final SimpleMapper.Type<Map<String, Object>> map;
93+
private final JsonMapper.Type<Map<String, Object>> map;
9494

95-
public MyAdapter(SimpleMapper simpleMapper) {
96-
this.map = simpleMapper.map();
95+
public MyAdapter(JsonMapper mapper) {
96+
this.map = mapper.map();
9797
}
9898

9999
@Override

0 commit comments

Comments
 (0)