|
| 1 | +package fasterxml.jackson.dataformat.avro; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.apache.avro.Schema; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import com.fasterxml.jackson.dataformat.avro.AvroMapper; |
| 14 | +import com.fasterxml.jackson.dataformat.avro.AvroSchema; |
| 15 | +import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator; |
| 16 | + |
| 17 | + |
| 18 | +public class NestedMapTest { |
| 19 | + |
| 20 | + public static class Nester { |
| 21 | + @JsonProperty |
| 22 | + public Map<String,Map<String,Integer>> nested; |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testSerialization() throws IOException { |
| 27 | + |
| 28 | + |
| 29 | + Nester fromJson = new ObjectMapper().readValue( |
| 30 | + "{\"nested\": {\"map\":{\"value\":1}}}" |
| 31 | + , Nester.class); |
| 32 | + |
| 33 | + AvroMapper mapper = new AvroMapper(); |
| 34 | + //Generate schema from class |
| 35 | + AvroSchemaGenerator gen = new AvroSchemaGenerator(); |
| 36 | + mapper.acceptJsonFormatVisitor(Nester.class, gen); |
| 37 | + Schema schema = gen.getGeneratedSchema().getAvroSchema(); |
| 38 | + |
| 39 | + |
| 40 | + //Serialize |
| 41 | + byte[] avroData = mapper.writer(new AvroSchema(schema)) |
| 42 | + .writeValueAsBytes(fromJson); |
| 43 | + |
| 44 | + //Deserialize |
| 45 | + Nester nester = mapper.readerFor(Nester.class) |
| 46 | + .with(new AvroSchema(schema)) |
| 47 | + .readValue(avroData); |
| 48 | + int val = nester.nested.get("map").get("value"); |
| 49 | + assertEquals(1, val); |
| 50 | + |
| 51 | + } |
| 52 | +} |
0 commit comments