Skip to content

Commit

Permalink
[core] Add type() method to SimpleMapper that takes a function that c…
Browse files Browse the repository at this point in the history
…reates 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);
  • Loading branch information
rbygrave committed Jan 14, 2025
1 parent bf6ce76 commit 5de5d44
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.Function;

final class DSimpleMapper implements SimpleMapper {

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

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

@Override
public Type<Object> object() {
return objectType;
Expand Down
13 changes: 13 additions & 0 deletions json-core/src/main/java/io/avaje/json/simple/SimpleMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

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

/**
* 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.
*
* @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);

default JsonExtract extract(Map<String, Object> map) {
return new DExtract(map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ class CustomAdapterTest {

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

MyCustomType source = new MyCustomType();
source.foo = "hi";
source.bar = 42;
String asJson = type.toJson(source);
String asJson = myType.toJson(source);

MyCustomType fromJson = type.fromJson(asJson);
MyCustomType fromJson = myType.fromJson(asJson);

assertThat(fromJson.foo).isEqualTo(source.foo);
assertThat(fromJson.bar).isEqualTo(source.bar);
Expand Down
13 changes: 13 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonNodeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.function.Function;

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

/**
* 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 JsonNodeMapper 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> SimpleMapper.Type<T> type(Function<JsonNodeMapper, JsonAdapter<T>> adapterFunction);

/**
* Build the JsonNodeMapper.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.avaje.json.stream.JsonStream;

import java.lang.reflect.Type;
import java.util.function.Function;

final class DJsonNodeMapper implements JsonNodeMapper {

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

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

@Override
public SimpleMapper.Type<JsonNode> nodeMapper() {
return new DMapper<>(nodeAdapter, jsonStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class CustomAdapterTest {

@Test
void propertyNames() {

var adapter = new MyAdapter2(mapper);
SimpleMapper.Type<MyCustomType> type = mapper.type(adapter);
SimpleMapper.Type<MyCustomType> type = mapper.type(MyAdapter2::new);

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

0 comments on commit 5de5d44

Please sign in to comment.