Skip to content

[core] Refactor rename SimpleMapper to JsonMapper (+ package simple -> mapper) #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions json-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Provides the core API including JsonAdapter, JsonReader, JsonWriter, JsonStream
</dependency>
```

## SimpleMapper
## JsonMapper

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

### Create a SimpleMapper
### Create a JsonMapper

static final SimpleMapper mapper = SimpleMapper.builder().build()
static final JsonMapper mapper = JsonMapper.builder().build()


### Map example
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.json.simple;
package io.avaje.json.mapper;

import java.util.Map;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.json.simple;
package io.avaje.json.mapper;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
Expand All @@ -11,14 +11,14 @@
import java.util.Map;
import java.util.function.Function;

final class DSimpleMapper implements SimpleMapper {
final class DJsonMapper implements JsonMapper {

private final JsonStream jsonStream;
private final Type<Object> objectType;
private final Type<Map<String,Object>> mapType;
private final Type<List<Object>> listType;

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

@Override
public <T> Type<T> type(Function<SimpleMapper, JsonAdapter<T>> adapterFunction) {
public <T> Type<T> type(Function<JsonMapper, JsonAdapter<T>> adapterFunction) {
return type(adapterFunction.apply(this));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.avaje.json.simple;
package io.avaje.json.mapper;

import io.avaje.json.core.CoreTypes;
import io.avaje.json.stream.JsonStream;

final class DSimpleMapperBuilder implements SimpleMapper.Builder {
final class DJsonMapperBuilder implements JsonMapper.Builder {

private JsonStream jsonStream;

@Override
public SimpleMapper.Builder jsonStream(JsonStream jsonStream) {
public JsonMapper.Builder jsonStream(JsonStream jsonStream) {
this.jsonStream = jsonStream;
return this;
}

@Override
public SimpleMapper build() {
public JsonMapper build() {
final var stream = jsonStream != null ? jsonStream : JsonStream.builder().build();
final var coreAdapters = CoreTypes.createCoreAdapters();
return new DSimpleMapper(stream, coreAdapters);
return new DJsonMapper(stream, coreAdapters);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.json.simple;
package io.avaje.json.mapper;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonException;
Expand All @@ -13,7 +13,7 @@
import java.util.List;
import java.util.Map;

final class DTypeMapper<T> implements SimpleMapper.Type<T> {
final class DTypeMapper<T> implements JsonMapper.Type<T> {

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

@Override
public SimpleMapper.Type<List<T>> list() {
public JsonMapper.Type<List<T>> list() {
final JsonAdapter<List<T>> list = CoreTypes.createList(adapter);
return new DTypeMapper<>(list, jsonStream);
}

@Override
public SimpleMapper.Type<Map<String, T>> map() {
public JsonMapper.Type<Map<String, T>> map() {
final JsonAdapter<Map<String, T>> map = CoreTypes.createMap(adapter);
return new DTypeMapper<>(map, jsonStream);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.json.simple;
package io.avaje.json.mapper;

import java.util.Map;
import java.util.Optional;
Expand All @@ -15,9 +15,9 @@
* <pre>{@code
*
* String json = "{\"name\":\"Rob\",\"score\":4.5,\"whenActive\":\"2025-10-20\",\"address\":{\"street\":\"Pall Mall\"}}";
* Map<String, Object> mapFromJson = simpleMapper.fromJsonObject(json);
* Map<String, Object> mapFromJson = jsonMapper.fromJsonObject(json);
*
* JsonExtract jsonExtract = simpleMapper.extract(mapFromJson);
* JsonExtract jsonExtract = jsonMapper.extract(mapFromJson);
*
* String name = jsonExtract.extract("name");
* double score = jsonExtract.extract("score", -1D);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.json.simple;
package io.avaje.json.mapper;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
Expand All @@ -20,28 +20,32 @@
* This supports the basic Java types of String, Boolean, Integer, Long, Double and
* Maps and List of these.
* <p>
* If avaje-jsonb is available then you will use that and NOT use this JsonMapper at all.
* This JsonMapper is intended to be used by code that ONLY wants to depend on avaje-json-core
* and process the basic Java types or provide its own JsonAdapters.
* <p>
* For full support with more types and binding to custom types use avaje-jsonb instead.
*
* <h3>Example</h3>
* <pre>{@code
*
* static final SimpleMapper simpleMapper = SimpleMapper.builder().build();
* static final JsonMapper jsonMapper = JsonMapper.builder().build();
*
* Map<String, Long> map = new LinkedHashMap<>();
* map.put("one", 45L);
* map.put("two", 93L);
*
* String asJson = simpleMapper.toJson(map);
* String asJson = jsonMapper.toJson(map);
*
* }</pre>
*/
public interface SimpleMapper {
public interface JsonMapper {

/**
* Create a new builder for SimpleMapper.
* Create a new builder for JsonMapper.
*/
static Builder builder() {
return new DSimpleMapperBuilder();
return new DJsonMapperBuilder();
}

/**
Expand Down Expand Up @@ -141,14 +145,17 @@ static Builder builder() {
* Return a Type specific mapper using a function that creates a JsonAdapter.
* <p>
* Often the adapterFunction is the constructor of the custom JsonAdapter where
* the constructor takes SimpleMapper as the only argument.
* the constructor takes JsonMapper as the only argument.
*
* @param adapterFunction The function that creates a JsonAdapter.
* @param <T> The type of the class to map to/from json.
* @return The Type specific mapper.
*/
<T> Type<T> type(Function<SimpleMapper, JsonAdapter<T>> adapterFunction);
<T> Type<T> type(Function<JsonMapper, JsonAdapter<T>> adapterFunction);

/**
* Return the map wrapped via JsonExtract to make extracting values easier.
*/
default JsonExtract extract(Map<String, Object> map) {
return new DExtract(map);
}
Expand All @@ -170,7 +177,7 @@ interface Builder {
/**
* Build and return the JsonNodeMapper.
*/
SimpleMapper build();
JsonMapper build();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion json-core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module io.avaje.json {

exports io.avaje.json;
exports io.avaje.json.simple;
exports io.avaje.json.stream;
exports io.avaje.json.view;
exports io.avaje.json.core to io.avaje.jsonb, io.avaje.json.node;
exports io.avaje.json.mapper;

requires static io.helidon.webserver;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.json.simple;
package io.avaje.json.mapper;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
Expand All @@ -10,15 +10,15 @@

class CustomAdapter2Test {

static final SimpleMapper simpleMapper = SimpleMapper.builder().build();
static final JsonMapper mapper = JsonMapper.builder().build();

@Test
void mapUsingCustomAdapter() {

PropertyNames names = simpleMapper.properties("foo", "bar");
PropertyNames names = mapper.properties("foo", "bar");
MyAdapterUsingRaw myAdapter = new MyAdapterUsingRaw(names);

SimpleMapper.Type<MyOtherType> type = simpleMapper.type(myAdapter);
JsonMapper.Type<MyOtherType> type = mapper.type(myAdapter);

MyOtherType source = new MyOtherType();
source.foo = "hi";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.json.simple;
package io.avaje.json.mapper;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
Expand All @@ -15,14 +15,14 @@
class CustomAdapterTest {

static final JsonStream jsonStream = JsonStream.builder().build();
static final SimpleMapper simpleMapper = SimpleMapper.builder().jsonStream(jsonStream).build();
static final MyAdapter myAdapter = new MyAdapter(simpleMapper);
static final SimpleMapper.Type<MyCustomType> type = simpleMapper.type(myAdapter);
static final JsonMapper mapper = JsonMapper.builder().jsonStream(jsonStream).build();
static final MyAdapter myAdapter = new MyAdapter(mapper);
static final JsonMapper.Type<MyCustomType> type = mapper.type(myAdapter);

@Test
void mapUsingCustomAdapter() {
SimpleMapper mapper = SimpleMapper.builder().build();
SimpleMapper.Type<MyCustomType> myType = mapper.type(MyAdapter::new);
JsonMapper mapper = JsonMapper.builder().build();
JsonMapper.Type<MyCustomType> myType = mapper.type(MyAdapter::new);

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

@Test
void list() {
SimpleMapper.Type<List<MyCustomType>> listType = type.list();
JsonMapper.Type<List<MyCustomType>> listType = type.list();

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

@Test
void map() {
SimpleMapper.Type<Map<String, MyCustomType>> mapType = type.map();
JsonMapper.Type<Map<String, MyCustomType>> mapType = type.map();

var v0 = as("a", 1);
var v1 = as("b", 2);
Expand Down Expand Up @@ -90,10 +90,10 @@ public int hashCode() {

static class MyAdapter implements JsonAdapter<MyCustomType> {

private final SimpleMapper.Type<Map<String, Object>> map;
private final JsonMapper.Type<Map<String, Object>> map;

public MyAdapter(SimpleMapper simpleMapper) {
this.map = simpleMapper.map();
public MyAdapter(JsonMapper mapper) {
this.map = mapper.map();
}

@Override
Expand Down
Loading