Skip to content
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

[core] Add type() method to SimpleMapper that takes a function that creates JsonAdapter #328

Merged
merged 3 commits into from
Jan 14, 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
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
Loading