|
| 1 | +package com.fasterxml.jackson.dataformat.avro.interop.annotations; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Objects; |
| 5 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 6 | +import com.fasterxml.jackson.core.JsonParser; |
| 7 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 8 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 9 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 10 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 11 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 12 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 13 | +import com.fasterxml.jackson.dataformat.avro.interop.annotations.UnionTest.Animal; |
| 14 | +import com.fasterxml.jackson.dataformat.avro.interop.annotations.UnionTest.Cat; |
| 15 | +import org.apache.avro.Schema; |
| 16 | +import org.apache.avro.SchemaBuilder; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.getApacheSchema; |
| 20 | +import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonDeserialize; |
| 21 | +import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonSerialize; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +public final class CustomSerializationTest { |
| 25 | + @JsonSerialize(using = House.Serializer.class) |
| 26 | + @JsonDeserialize(using = House.Deserializer.class) |
| 27 | + public final static class House { |
| 28 | + public String ownerName; |
| 29 | + |
| 30 | + public House(final String ownerName) { |
| 31 | + this.ownerName = ownerName; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public boolean equals(final Object other) { |
| 36 | + return other instanceof House && ownerName.equals(((House) other).ownerName); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public int hashCode() { |
| 41 | + return Objects.hash(ownerName); |
| 42 | + } |
| 43 | + |
| 44 | + public static class Serializer extends JsonSerializer<House> { |
| 45 | + @Override |
| 46 | + public void serialize( |
| 47 | + final House house, |
| 48 | + final JsonGenerator generator, |
| 49 | + final SerializerProvider serializers |
| 50 | + ) throws IOException { |
| 51 | + generator.writeStartObject(); |
| 52 | + generator.writeFieldName("owner"); |
| 53 | + generator.writeStartObject(); |
| 54 | + generator.writeFieldName("name"); |
| 55 | + generator.writeObject(house.ownerName); |
| 56 | + generator.writeEndObject(); |
| 57 | + generator.writeEndObject(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static class Deserializer extends JsonDeserializer<House> { |
| 62 | + @Override |
| 63 | + public House deserialize( |
| 64 | + final JsonParser parser, |
| 65 | + final DeserializationContext context |
| 66 | + ) throws IOException { |
| 67 | + // startObject |
| 68 | + parser.nextToken(); |
| 69 | + // fieldName("owner") |
| 70 | + parser.nextToken(); |
| 71 | + // writeStartObject |
| 72 | + parser.nextToken(); |
| 73 | + // fieldName("name") |
| 74 | + parser.nextToken(); |
| 75 | + |
| 76 | + String ownerName = parser.readValueAs(String.class); |
| 77 | + |
| 78 | + // endObject |
| 79 | + parser.nextToken(); |
| 80 | + // endObject |
| 81 | + parser.nextToken(); |
| 82 | + |
| 83 | + return new House(ownerName); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testUnionCustomDeSerialization() throws IOException { |
| 90 | + Schema schema = SchemaBuilder.builder(House.class.getPackage().getName()) |
| 91 | + .record(House.class.getSimpleName()) |
| 92 | + .fields() |
| 93 | + .name("owner") |
| 94 | + .type() |
| 95 | + .optional() |
| 96 | + .record("Owner") |
| 97 | + .fields() |
| 98 | + .name("name") |
| 99 | + .type().nullable().stringType() |
| 100 | + .noDefault() |
| 101 | + .endRecord() |
| 102 | + .endRecord(); |
| 103 | + |
| 104 | + House house = new House("Jackson"); |
| 105 | + |
| 106 | + byte[] bytes = jacksonSerialize(schema, house); |
| 107 | + House result = jacksonDeserialize(schema, House.class, bytes); |
| 108 | + |
| 109 | + assertThat(result).isEqualTo(house); |
| 110 | + } |
| 111 | +} |
0 commit comments