|
| 1 | +package com.fasterxml.jackson.dataformat.xml.deser; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 6 | +import com.fasterxml.jackson.annotation.JsonRootName; |
| 7 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 8 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 9 | +import com.fasterxml.jackson.annotation.JsonUnwrapped; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 13 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 14 | + |
| 15 | +// Tests copied from databind "JDKNumberDeserTest" (only a small subset) |
| 16 | +public class NumberDeserWithXMLTest extends XmlTestBase |
| 17 | +{ |
| 18 | + // [databind#2644] |
| 19 | + @JsonRootName("Root") |
| 20 | + static class NodeRoot2644 { |
| 21 | + public String type; |
| 22 | + |
| 23 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type") |
| 24 | + @JsonSubTypes(value = { |
| 25 | + @JsonSubTypes.Type(value = NodeParent2644.class, name = "NodeParent") |
| 26 | + }) |
| 27 | + public Node2644 node; |
| 28 | + } |
| 29 | + |
| 30 | + public static class NodeParent2644 extends Node2644 { } |
| 31 | + |
| 32 | + public static abstract class Node2644 { |
| 33 | + @JsonProperty("amount") |
| 34 | + BigDecimal val; |
| 35 | + |
| 36 | + public BigDecimal getVal() { |
| 37 | + return val; |
| 38 | + } |
| 39 | + |
| 40 | + public void setVal(BigDecimal val) { |
| 41 | + this.val = val; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // [databind#2784] |
| 46 | + static class BigDecimalHolder2784 { |
| 47 | + public BigDecimal value; |
| 48 | + } |
| 49 | + |
| 50 | + @JsonRootName("Nested") |
| 51 | + static class NestedBigDecimalHolder2784 { |
| 52 | + @JsonUnwrapped |
| 53 | + public BigDecimalHolder2784 holder; |
| 54 | + } |
| 55 | + |
| 56 | + /* |
| 57 | + /********************************************************************** |
| 58 | + /* Test methods |
| 59 | + /********************************************************************** |
| 60 | + */ |
| 61 | + |
| 62 | + private final XmlMapper MAPPER = newMapper(); |
| 63 | + |
| 64 | + // [databind#2644] |
| 65 | + public void testBigDecimalSubtypes() throws Exception |
| 66 | + { |
| 67 | + ObjectMapper mapper = mapperBuilder() |
| 68 | + .registerSubtypes(NodeParent2644.class) |
| 69 | + .build(); |
| 70 | + NodeRoot2644 root = mapper.readValue("<Root>\n" |
| 71 | + +"<type>NodeParent</type>" |
| 72 | + +"<node><amount>9999999999999999.99</amount></node>\n" |
| 73 | + +"</Root>\n", |
| 74 | + NodeRoot2644.class |
| 75 | + ); |
| 76 | + |
| 77 | + assertEquals(new BigDecimal("9999999999999999.99"), root.node.getVal()); |
| 78 | + } |
| 79 | + |
| 80 | + // [databind#2784] |
| 81 | + public void testBigDecimalUnwrapped() throws Exception |
| 82 | + { |
| 83 | + // mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); |
| 84 | + final String DOC = "<Nested><value>5.00</value></Nested>"; |
| 85 | + NestedBigDecimalHolder2784 result = MAPPER.readValue(DOC, NestedBigDecimalHolder2784.class); |
| 86 | + assertEquals(new BigDecimal("5.00"), result.holder.value); |
| 87 | + } |
| 88 | +} |
0 commit comments