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

fix nested map serialization #24

Merged
merged 1 commit into from
Nov 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final AvroWriteContext createChildArrayContext() {
public final AvroWriteContext createChildObjectContext() throws JsonMappingException
{
_verifyValueWrite();
AvroWriteContext child = _createObjectContext(_schema.getElementType());
AvroWriteContext child = _createObjectContext(_schema.getValueType());
_data.put(_currentName, child.rawValue());
return child;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fasterxml.jackson.dataformat.avro;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.Map;

import org.apache.avro.Schema;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator;


public class NestedMapTest {

public static class Nester {
@JsonProperty
public Map<String,Map<String,Integer>> nested;
}

@Test
public void testSerialization() throws IOException {


Nester fromJson = new ObjectMapper().readValue(
"{\"nested\": {\"map\":{\"value\":1}}}"
, Nester.class);

AvroMapper mapper = new AvroMapper();
//Generate schema from class
AvroSchemaGenerator gen = new AvroSchemaGenerator();
mapper.acceptJsonFormatVisitor(Nester.class, gen);
Schema schema = gen.getGeneratedSchema().getAvroSchema();


//Serialize
byte[] avroData = mapper.writer(new AvroSchema(schema))
.writeValueAsBytes(fromJson);

//Deserialize
Nester nester = mapper.readerFor(Nester.class)
.with(new AvroSchema(schema))
.readValue(avroData);
int val = nester.nested.get("map").get("value");
assertEquals(1, val);

}
}