Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Multimap serializer ignores _valueTypeSerializer #74

Merged
merged 1 commit into from
Jul 16, 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 @@ -316,7 +316,11 @@ private final void serializeFields(Multimap<?, ?> mmap, JsonGenerator gen, Seria
serializers = _dynamicValueSerializers;
}
}
valueSer.serialize(vv, gen, provider);
if (_valueTypeSerializer == null) {
valueSer.serialize(vv, gen, provider);
} else {
valueSer.serializeWithType(vv, gen, provider, _valueTypeSerializer);
}
}
gen.writeEndArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.pojo.AddOp;
import com.fasterxml.jackson.datatype.guava.pojo.MathOp;
import com.fasterxml.jackson.datatype.guava.pojo.MulOp;

import com.google.common.collect.*;

Expand All @@ -11,6 +14,7 @@
import java.util.Map;

import static com.google.common.collect.TreeMultimap.create;
import static junit.framework.TestCase.assertEquals;

/**
* Unit tests to verify handling of various {@link Multimap}s.
Expand Down Expand Up @@ -254,4 +258,53 @@ public void testIssue67() throws IOException
assertEquals(Maps.immutableEntry("a", 7), iterator.next());
assertEquals(Maps.immutableEntry("a", 8), iterator.next());
}

public void testPolymorphicValue() throws IOException {
ImmutableMultimapWrapper input = new ImmutableMultimapWrapper(ImmutableMultimap.of("add", new AddOp(3, 2), "mul", new MulOp(4, 6)));

String json = MAPPER.writeValueAsString(input);

ImmutableMultimapWrapper output = MAPPER.readValue(json, ImmutableMultimapWrapper.class);
assertEquals(input, output);
}

public static class ImmutableMultimapWrapper {

private ImmutableMultimap<String, MathOp> multimap;

public ImmutableMultimapWrapper() {
}

public ImmutableMultimapWrapper(ImmutableMultimap<String, MathOp> f) {
this.multimap = f;
}

public ImmutableMultimap<String, MathOp> getMultimap() {
return multimap;
}

public void setMultimap(ImmutableMultimap<String, MathOp> multimap) {
this.multimap = multimap;
}

@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + (this.multimap != null ? this.multimap.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ImmutableMultimapWrapper other = (ImmutableMultimapWrapper) obj;
return !(this.multimap != other.multimap && (this.multimap == null || !this.multimap.equals(other.multimap)));
}

}
}
58 changes: 58 additions & 0 deletions src/test/java/com/fasterxml/jackson/datatype/guava/pojo/AddOp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.fasterxml.jackson.datatype.guava.pojo;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
*
* @author Marcin Kamionowski
*/
public class AddOp implements MathOp {

private final int left;
private final int right;

@JsonCreator
public AddOp(
@JsonProperty("left") int left,
@JsonProperty("right") int right) {
this.left = left;
this.right = right;
}

public int getLeft() {
return left;
}

public int getRight() {
return right;
}

public int value() {
return left + right;
}

@Override
public int hashCode() {
int hash = 3;
hash = 47 * hash + this.left;
hash = 47 * hash + this.right;
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final AddOp other = (AddOp) obj;
if (this.left != other.left) {
return false;
}
return this.right == other.right;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fasterxml.jackson.datatype.guava.pojo;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

/**
*
* @author Marcin Kamionowski
*/
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="_t")
@JsonSubTypes({
@Type(name="add", value=AddOp.class),
@Type(name="mul", value=MulOp.class)})
public interface MathOp {

}
58 changes: 58 additions & 0 deletions src/test/java/com/fasterxml/jackson/datatype/guava/pojo/MulOp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.fasterxml.jackson.datatype.guava.pojo;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
*
* @author Marcin Kamionowski
*/
public class MulOp implements MathOp {

private final int left;
private final int right;

@JsonCreator
public MulOp(
@JsonProperty("left") int left,
@JsonProperty("right") int right) {
this.left = left;
this.right = right;
}

public int getLeft() {
return left;
}

public int getRight() {
return right;
}

public int value() {
return left * right;
}

@Override
public int hashCode() {
int hash = 7;
hash = 67 * hash + this.left;
hash = 67 * hash + this.right;
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final MulOp other = (MulOp) obj;
if (this.left != other.left) {
return false;
}
return this.right == other.right;
}

}