-
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.
Add something like
@JsonSerialize
(#282)
* start * add tests * rename and javadoc * catch an edge case * Format, extract methods * Remove isGeneric() from CustomAdapter --------- Co-authored-by: Rob Bygrave <[email protected]>
- Loading branch information
Showing
15 changed files
with
259 additions
and
31 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
blackbox-test/src/main/java/org/example/other/custom/serializer/CustomExample.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,11 @@ | ||
package org.example.other.custom.serializer; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import io.avaje.jsonb.Json; | ||
|
||
@Json | ||
public record CustomExample( | ||
@Json.Serializer(MoneySerializer.class) | ||
BigDecimal amountOwed, | ||
BigDecimal somethingElse) {} |
26 changes: 26 additions & 0 deletions
26
blackbox-test/src/main/java/org/example/other/custom/serializer/MoneySerializer.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,26 @@ | ||
package org.example.other.custom.serializer; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
import io.avaje.jsonb.CustomAdapter; | ||
import io.avaje.jsonb.JsonAdapter; | ||
import io.avaje.jsonb.JsonReader; | ||
import io.avaje.jsonb.JsonWriter; | ||
import io.avaje.jsonb.Jsonb; | ||
|
||
@CustomAdapter(global = false) | ||
public class MoneySerializer implements JsonAdapter<BigDecimal> { | ||
|
||
public MoneySerializer(Jsonb jsonb) {} | ||
|
||
@Override | ||
public BigDecimal fromJson(JsonReader reader) { | ||
return reader.readDecimal().setScale(2, RoundingMode.DOWN); | ||
} | ||
|
||
@Override | ||
public void toJson(JsonWriter writer, BigDecimal value) { | ||
writer.value(value.setScale(2, RoundingMode.DOWN)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
blackbox-test/src/test/java/org/example/other/custom/serializer/CustomExampleTest.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,29 @@ | ||
package org.example.other.custom.serializer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.avaje.jsonb.JsonType; | ||
import io.avaje.jsonb.Jsonb; | ||
|
||
class TestSelectiveSerializer { | ||
|
||
Jsonb jsonb = Jsonb.builder().build(); | ||
JsonType<CustomExample> jsonType = jsonb.type(CustomExample.class); | ||
|
||
@Test | ||
void toFromJson() { | ||
final var bean = new CustomExample(new BigDecimal("100.95630"), new BigDecimal("100.95630")); | ||
|
||
final String asJson = jsonType.toJson(bean); | ||
assertThat(asJson).isEqualTo("{\"amountOwed\":100.95,\"somethingElse\":100.95630}"); | ||
|
||
final var fromJson = jsonType.fromJson(asJson); | ||
assertThat(fromJson.amountOwed()).isEqualTo(new BigDecimal("100.95")); | ||
assertThat(fromJson.somethingElse()).isEqualTo(new BigDecimal("100.95630")); | ||
assertThat(fromJson).isNotEqualTo(bean); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
jsonb-generator/src/test/java/io/avaje/jsonb/generator/models/valid/MoneySerializer.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,24 @@ | ||
package io.avaje.jsonb.generator.models.valid; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import io.avaje.jsonb.CustomAdapter; | ||
import io.avaje.jsonb.JsonAdapter; | ||
import io.avaje.jsonb.JsonReader; | ||
import io.avaje.jsonb.JsonWriter; | ||
import io.avaje.jsonb.Jsonb; | ||
|
||
@CustomAdapter(global = false) | ||
public class MoneySerializer implements JsonAdapter<BigDecimal> { | ||
|
||
public MoneySerializer(Jsonb jsonb) {} | ||
|
||
@Override | ||
public void toJson(JsonWriter writer, BigDecimal value) {} | ||
|
||
@Override | ||
public BigDecimal fromJson(JsonReader reader) { | ||
|
||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
jsonb-generator/src/test/java/io/avaje/jsonb/generator/models/valid/SerializerTest.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,23 @@ | ||
package io.avaje.jsonb.generator.models.valid; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import io.avaje.jsonb.Json; | ||
|
||
@Json | ||
public class SerializerTest { | ||
|
||
@Json.Serializer(MoneySerializer.class) | ||
BigDecimal amount; | ||
|
||
@Json.Serializer(MoneySerializer.class) | ||
BigDecimal amountReturned; | ||
|
||
BigDecimal somethingElse; | ||
|
||
@Json.Property("methodCustom") | ||
@Json.Serializer(MoneySerializer.class) | ||
BigDecimal methodCustom() { | ||
return null; | ||
} | ||
} |
Oops, something went wrong.