Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit a893c4c

Browse files
committed
Merge pull request #24 from whiteops-dot-com/master
fix nested map serialization
2 parents dc17146 + 38dffda commit a893c4c

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/main/java/com/fasterxml/jackson/dataformat/avro/ser/MapWriteContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final AvroWriteContext createChildArrayContext() {
4646
public final AvroWriteContext createChildObjectContext() throws JsonMappingException
4747
{
4848
_verifyValueWrite();
49-
AvroWriteContext child = _createObjectContext(_schema.getElementType());
49+
AvroWriteContext child = _createObjectContext(_schema.getValueType());
5050
_data.put(_currentName, child.rawValue());
5151
return child;
5252
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)