-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[json-node] JsonNodeMapper add type support for customer JsonAdapters (…
…#311) * [json-node] JsonNodeMapper add type support for customer JsonAdapters For cases where we want to manually build a JsonAdapter and then use that via JsonNodeMapper. For example, manually build JsonAdapter using JsonObject.extract() methods to extract out values and deal with number types etc. * [json-node] JsonNodeMapper - fix javadoc
- Loading branch information
Showing
7 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
json-node/src/test/java/io/avaje/json/node/CustomAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.avaje.json.node; | ||
|
||
import io.avaje.json.JsonAdapter; | ||
import io.avaje.json.JsonReader; | ||
import io.avaje.json.JsonWriter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class CustomAdapterTest { | ||
|
||
static final JsonNodeMapper mapper = JsonNodeMapper.builder().build(); | ||
|
||
@Test | ||
void mapUsingCustomAdapter() { | ||
|
||
MyAdapter myAdapter = new MyAdapter(mapper); | ||
|
||
NodeMapper<MyCustomType> typeMapper = mapper.mapper(myAdapter); | ||
|
||
MyCustomType source = new MyCustomType(); | ||
source.foo = "hi"; | ||
source.bar = 42; | ||
String asJson = typeMapper.toJson(source); | ||
|
||
MyCustomType fromJson = typeMapper.fromJson(asJson); | ||
|
||
assertThat(fromJson.foo).isEqualTo(source.foo); | ||
assertThat(fromJson.bar).isEqualTo(source.bar); | ||
} | ||
|
||
static class MyCustomType { | ||
public String foo; | ||
public int bar; | ||
} | ||
|
||
static class MyAdapter implements JsonAdapter<MyCustomType> { | ||
|
||
final NodeMapper<JsonObject> objectMapper; | ||
|
||
public MyAdapter(JsonNodeMapper mapper) { | ||
this.objectMapper = mapper.objectMapper(); | ||
} | ||
|
||
@Override | ||
public void toJson(JsonWriter writer, MyCustomType value) { | ||
var jsonObject = JsonObject.create() | ||
.add("foo", value.foo) | ||
.add("bar", value.bar); | ||
objectMapper.toJson(jsonObject, writer); | ||
} | ||
|
||
@Override | ||
public MyCustomType fromJson(JsonReader reader) { | ||
JsonObject jsonObject = objectMapper.fromJson(reader); | ||
|
||
MyCustomType myCustomType = new MyCustomType(); | ||
myCustomType.foo = jsonObject.extract("foo"); | ||
myCustomType.bar = jsonObject.extract("bar", 0); | ||
return myCustomType; | ||
} | ||
} | ||
} |