-
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-core] SimpleMapper add type support for customer JsonAdapters (#…
…310) For cases where we want to manually build a JsonAdapter and then use that via SimpleMapper
- Loading branch information
Showing
4 changed files
with
154 additions
and
0 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
73 changes: 73 additions & 0 deletions
73
json-core/src/test/java/io/avaje/json/simple/CustomAdapter2Test.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,73 @@ | ||
package io.avaje.json.simple; | ||
|
||
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 CustomAdapter2Test { | ||
|
||
static final SimpleMapper simpleMapper = SimpleMapper.builder().build(); | ||
|
||
@Test | ||
void mapUsingCustomAdapter() { | ||
|
||
MyAdapterUsingRaw myAdapter = new MyAdapterUsingRaw(); | ||
|
||
SimpleMapper.Type<MyOtherType> type = simpleMapper.type(myAdapter); | ||
|
||
MyOtherType source = new MyOtherType(); | ||
source.foo = "hi"; | ||
source.bar = 42; | ||
String asJson = type.toJson(source); | ||
|
||
MyOtherType fromJson = type.fromJson(asJson); | ||
|
||
assertThat(fromJson.foo).isEqualTo(source.foo); | ||
assertThat(fromJson.bar).isEqualTo(source.bar); | ||
} | ||
|
||
static class MyOtherType { | ||
String foo; | ||
int bar; | ||
} | ||
|
||
static class MyAdapterUsingRaw implements JsonAdapter<MyOtherType> { | ||
|
||
@Override | ||
public void toJson(JsonWriter writer, MyOtherType value) { | ||
writer.beginObject(); | ||
writer.name("foo"); | ||
writer.value(value.foo); | ||
writer.name("bar"); | ||
writer.value(value.bar); | ||
writer.endObject(); | ||
} | ||
|
||
@Override | ||
public MyOtherType fromJson(JsonReader reader) { | ||
MyOtherType result = new MyOtherType(); | ||
|
||
reader.beginObject(); | ||
|
||
String key; | ||
while (reader.hasNextField()) { | ||
key = reader.nextField(); | ||
switch (key) { | ||
case "foo": | ||
result.foo = reader.readString(); | ||
break; | ||
case "bar": | ||
result.bar = reader.readInt(); | ||
break; | ||
default: | ||
reader.skipValue(); | ||
} | ||
} | ||
reader.endObject(); | ||
return result; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
json-core/src/test/java/io/avaje/json/simple/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.simple; | ||
|
||
import io.avaje.json.JsonAdapter; | ||
import io.avaje.json.JsonReader; | ||
import io.avaje.json.JsonWriter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class CustomAdapterTest { | ||
|
||
static final SimpleMapper simpleMapper = SimpleMapper.builder().build(); | ||
|
||
@Test | ||
void mapUsingCustomAdapter() { | ||
|
||
MyAdapter myAdapter = new MyAdapter(simpleMapper); | ||
|
||
SimpleMapper.Type<MyCustomType> type = simpleMapper.type(myAdapter); | ||
|
||
MyCustomType source = new MyCustomType(); | ||
source.foo = "hi"; | ||
source.bar = 42; | ||
String asJson = type.toJson(source); | ||
|
||
MyCustomType fromJson = type.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> { | ||
|
||
private final SimpleMapper.Type<Map<String, Object>> map; | ||
|
||
public MyAdapter(SimpleMapper simpleMapper) { | ||
this.map = simpleMapper.map(); | ||
} | ||
|
||
@Override | ||
public void toJson(JsonWriter writer, MyCustomType value) { | ||
Map<String, Object> foo = Map.of("foo", value.foo, "bar", value.bar); | ||
map.toJson(foo, writer); | ||
} | ||
|
||
@Override | ||
public MyCustomType fromJson(JsonReader reader) { | ||
Map<String, Object> mapValue = map.fromJson(reader); | ||
MyCustomType myCustomType = new MyCustomType(); | ||
myCustomType.foo = (String)mapValue.get("foo"); | ||
Number bar = (Number)mapValue.get("bar"); | ||
myCustomType.bar = bar == null ? 0 : bar.intValue(); | ||
return myCustomType; | ||
} | ||
} | ||
} |