Skip to content

Commit

Permalink
Add tests wrt #795
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 18, 2015
1 parent f92a6b3 commit e412926
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.databind.convert;

import java.math.BigDecimal;
import java.util.*;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
Expand Down Expand Up @@ -97,7 +98,23 @@ static class LowerCaseTextArray {
@JsonDeserialize(contentConverter=LowerCaser.class)
public String[] texts;
}


// for [databind#795]

static class ToNumberConverter extends StdConverter<String,Number>
{
@Override
public Number convert(String value) {
return new BigDecimal(value);
}
}

static class Issue795Bean
{
@JsonDeserialize(converter=ToNumberConverter.class)
public Number value;
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -180,4 +197,15 @@ public void testPropertyAnnotationForMaps() throws Exception
assertEquals(1, p.x);
assertEquals(2, p.y);
}

// [databind#795]
public void testConvertToAbstract() throws Exception
{
Issue795Bean bean = objectReader(Issue795Bean.class)
.readValue("{\"value\":\"1.25\"}");
assertNotNull(bean.value);
assertTrue("Type not BigDecimal but "+bean.value.getClass(),
bean.value instanceof BigDecimal);
assertEquals(new BigDecimal("1.25"), bean.value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.fasterxml.jackson.failing;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.*;
import com.fasterxml.jackson.databind.util.StdConverter;

// for [databind#795]
public class ConvertingAbstractSerializer795Test extends BaseMapTest
{
public static abstract class AbstractCustomType {
final String value;
public AbstractCustomType(String v) {
this.value = v;
}
}

public static class ConcreteCustomType extends AbstractCustomType {
public ConcreteCustomType(String v) {
super(v);
}
}

public static class AbstractCustomTypeDeserializationConverter extends StdConverter<String, AbstractCustomType>{

@Override
public AbstractCustomType convert(String arg) {
return new ConcreteCustomType(arg);
}
}

public static class AbstractCustomTypeUser {
@JsonProperty
@JsonDeserialize(converter = AbstractCustomTypeDeserializationConverter.class)
private final AbstractCustomType customField;

@JsonCreator AbstractCustomTypeUser(@JsonProperty("customField")
AbstractCustomType customField) {
this.customField = customField;
}
}

public static class NonAbstractCustomType {
final String value;
public NonAbstractCustomType(String v) {
this.value = v;
}
}


public static class NonAbstractCustomTypeDeserializationConverter extends StdConverter<String, NonAbstractCustomType>{

@Override
public NonAbstractCustomType convert(String arg) {
return new NonAbstractCustomType(arg);
}
}


public static class NonAbstractCustomTypeUser {
@JsonProperty
@JsonDeserialize(converter = NonAbstractCustomTypeDeserializationConverter.class)
private final NonAbstractCustomType customField;

@JsonCreator NonAbstractCustomTypeUser(@JsonProperty("customField") NonAbstractCustomType customField) {
this.customField = customField;
}
}

private static final ObjectMapper JSON_MAPPER = new ObjectMapper();

public void testAbstractTypeDeserialization() throws Exception {
String test="{\"customField\": \"customString\"}";
AbstractCustomTypeUser cu = JSON_MAPPER.readValue(test, AbstractCustomTypeUser.class);
assertNotNull(cu);
}

public void testNonAbstractDeserialization() throws Exception {
String test="{\"customField\": \"customString\"}";
NonAbstractCustomTypeUser cu = JSON_MAPPER.readValue(test, NonAbstractCustomTypeUser.class);
assertNotNull(cu);
}
}

0 comments on commit e412926

Please sign in to comment.