|
| 1 | +package com.fasterxml.jackson.jr.failing; |
| 2 | + |
| 3 | +import java.util.LinkedHashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.jr.ob.JSON; |
| 7 | +import com.fasterxml.jackson.jr.ob.TestBase; |
| 8 | + |
| 9 | +public class NullHandling78Test extends TestBase |
| 10 | +{ |
| 11 | + // [jackson-jr#78] |
| 12 | + static class IntegerWrapper { |
| 13 | + public Integer value; |
| 14 | + } |
| 15 | + static class IntPrimitiveWrapper { |
| 16 | + public int value; |
| 17 | + } |
| 18 | + static class LongWrapper { |
| 19 | + public Long value; |
| 20 | + } |
| 21 | + static class LongPrimitiveWrapper { |
| 22 | + public long value; |
| 23 | + } |
| 24 | + static class BooleanWrapper { |
| 25 | + public Boolean value; |
| 26 | + } |
| 27 | + static class BooleanPrimitiveWrapper { |
| 28 | + public boolean value; |
| 29 | + } |
| 30 | + static class DoubleWrapper { |
| 31 | + public Double value; |
| 32 | + } |
| 33 | + static class DoublePrimitiveWrapper { |
| 34 | + public double value; |
| 35 | + } |
| 36 | + |
| 37 | + // Test to verify that outputting of nulls is configurable |
| 38 | + public void testMapNullEntries() throws Exception |
| 39 | + { |
| 40 | + Map<String,Object> map = new LinkedHashMap<String,Object>(); |
| 41 | + map.put("a", 1); |
| 42 | + map.put("b", null); |
| 43 | + // By default we do NOT write null-valued entries: |
| 44 | + assertEquals("{\"a\":1}", JSON.std.asString(map)); |
| 45 | + // but we can disable it easily |
| 46 | + assertEquals("{\"a\":1,\"b\":null}", |
| 47 | + JSON.std.with(JSON.Feature.WRITE_NULL_PROPERTIES).asString(map)); |
| 48 | + } |
| 49 | + |
| 50 | + // [jackson-jr#78], int/Integer |
| 51 | + |
| 52 | + public void testIntPrimitive() throws Exception |
| 53 | + { |
| 54 | + IntPrimitiveWrapper w = JSON.std.beanFrom(IntPrimitiveWrapper.class, |
| 55 | + a2q("{'value':1}")); |
| 56 | + assertEquals(1, w.value); |
| 57 | + |
| 58 | + w = JSON.std.beanFrom(IntPrimitiveWrapper.class, |
| 59 | + a2q("{'value':null}")); |
| 60 | + assertEquals(0, w.value); |
| 61 | + } |
| 62 | + |
| 63 | + public void testIntWrapper() throws Exception |
| 64 | + { |
| 65 | + IntegerWrapper w = JSON.std.beanFrom(IntegerWrapper.class, |
| 66 | + a2q("{'value':1}")); |
| 67 | + assertEquals(Integer.valueOf(1), w.value); |
| 68 | + |
| 69 | + w = JSON.std.beanFrom(IntegerWrapper.class, |
| 70 | + a2q("{'value':null}")); |
| 71 | + assertNull(w.value); |
| 72 | + } |
| 73 | + |
| 74 | + // [jackson-jr#78], long/Long |
| 75 | + |
| 76 | + public void testLongPrimitive() throws Exception |
| 77 | + { |
| 78 | + LongPrimitiveWrapper w = JSON.std.beanFrom(LongPrimitiveWrapper.class, |
| 79 | + a2q("{'value':2}")); |
| 80 | + assertEquals(2L, w.value); |
| 81 | + |
| 82 | + w = JSON.std.beanFrom(LongPrimitiveWrapper.class, |
| 83 | + a2q("{'value':null}")); |
| 84 | + assertEquals(0L, w.value); |
| 85 | + } |
| 86 | + |
| 87 | + public void testLongWrapper() throws Exception |
| 88 | + { |
| 89 | + LongWrapper w = JSON.std.beanFrom(LongWrapper.class, |
| 90 | + a2q("{'value':2}")); |
| 91 | + assertEquals(Long.valueOf(2L), w.value); |
| 92 | + |
| 93 | + w = JSON.std.beanFrom(LongWrapper.class, |
| 94 | + a2q("{'value':null}")); |
| 95 | + assertNull(w.value); |
| 96 | + } |
| 97 | + |
| 98 | + // [jackson-jr#78], boolean/Boolean |
| 99 | + |
| 100 | + public void testBooleanPrimitive() throws Exception |
| 101 | + { |
| 102 | + BooleanPrimitiveWrapper w = JSON.std.beanFrom(BooleanPrimitiveWrapper.class, |
| 103 | + a2q("{'value':true}")); |
| 104 | + assertTrue(w.value); |
| 105 | + |
| 106 | + w = JSON.std.beanFrom(BooleanPrimitiveWrapper.class, |
| 107 | + a2q("{'value':null}")); |
| 108 | + assertFalse(w.value); |
| 109 | + } |
| 110 | + |
| 111 | + public void testBooleanWrapper() throws Exception |
| 112 | + { |
| 113 | + BooleanWrapper w = JSON.std.beanFrom(BooleanWrapper.class, |
| 114 | + a2q("{'value':true}")); |
| 115 | + assertEquals(Boolean.TRUE, w.value); |
| 116 | + |
| 117 | + w = JSON.std.beanFrom(BooleanWrapper.class, |
| 118 | + a2q("{'value':null}")); |
| 119 | + assertNull(w.value); |
| 120 | + } |
| 121 | + |
| 122 | + // [jackson-jr#78], boolean/Boolean |
| 123 | + |
| 124 | + public void testDoublePrimitive() throws Exception |
| 125 | + { |
| 126 | + DoublePrimitiveWrapper w = JSON.std.beanFrom(DoublePrimitiveWrapper.class, |
| 127 | + a2q("{'value':0.25}")); |
| 128 | + assertEquals(0.25, w.value); |
| 129 | + |
| 130 | + w = JSON.std.beanFrom(DoublePrimitiveWrapper.class, |
| 131 | + a2q("{'value':null}")); |
| 132 | + // yeah yeah, not kosher wrt epsilon etc but... |
| 133 | + assertEquals(0.0, w.value); |
| 134 | + } |
| 135 | + |
| 136 | + public void testDoubleWrapper() throws Exception |
| 137 | + { |
| 138 | + DoubleWrapper w = JSON.std.beanFrom(DoubleWrapper.class, |
| 139 | + a2q("{'value':0.25}")); |
| 140 | + assertEquals(Double.valueOf(0.25), w.value); |
| 141 | + |
| 142 | + w = JSON.std.beanFrom(DoubleWrapper.class, |
| 143 | + a2q("{'value':null}")); |
| 144 | + assertNull(w.value); |
| 145 | + } |
| 146 | +} |
0 commit comments