1919package org .apache .johnzon .mapper ;
2020
2121import static org .junit .Assert .assertEquals ;
22+ import static org .junit .Assert .assertNotNull ;
23+ import static org .junit .Assert .assertNull ;
2224import static org .junit .Assert .assertTrue ;
2325
2426import java .math .BigDecimal ;
27+ import java .math .BigInteger ;
2528
29+ import org .apache .johnzon .mapper .converter .BigDecimalConverter ;
30+ import org .apache .johnzon .mapper .internal .AdapterKey ;
31+ import org .apache .johnzon .mapper .map .LazyConverterMap ;
2632import org .junit .Test ;
2733
2834public class NumberSerializationTest {
@@ -46,11 +52,91 @@ public void numberFromJson() {
4652 mapper .close ();
4753 }
4854
55+ /**
56+ * Bug: BigDecimalConverter used toString() which produces scientific notation
57+ * (e.g. "7.33915E-7"). Should use toPlainString() to produce "0.000000733915".
58+ */
59+ @ Test
60+ public void bigDecimalConverterUsesPlainNotation () {
61+ final BigDecimalConverter converter = new BigDecimalConverter ();
62+ final BigDecimal smallValue = new BigDecimal ("0.000000733915" );
63+ final String result = converter .toString (smallValue );
64+ assertEquals ("BigDecimalConverter should use plain notation, not scientific" ,
65+ "0.000000733915" , result );
66+ }
67+
68+ /**
69+ * Bug fix: useBigDecimalStringAdapter and useBigIntegerStringAdapter flags
70+ * were swapped in LazyConverterMap. Each flag must control its own type.
71+ * Both default to true (string) for I-JSON (RFC 7493) interoperability.
72+ */
73+ @ Test
74+ public void bigDecimalStringAdapterFlagControlsBigDecimal () {
75+ // Default: BigDecimal adapter is ON (useBigDecimalStringAdapter=true)
76+ final LazyConverterMap defaultAdapters = new LazyConverterMap ();
77+ assertNotNull ("BigDecimal adapter should be active by default" ,
78+ defaultAdapters .get (new AdapterKey (BigDecimal .class , String .class )));
79+ // Disabled: BigDecimal adapter is OFF
80+ final LazyConverterMap disabledAdapters = new LazyConverterMap ();
81+ disabledAdapters .setUseBigDecimalStringAdapter (false );
82+ assertNull ("BigDecimal adapter should be null when flag is false" ,
83+ disabledAdapters .get (new AdapterKey (BigDecimal .class , String .class )));
84+ }
85+
86+ @ Test
87+ public void bigIntegerStringAdapterFlagControlsBigInteger () {
88+ // Default: BigInteger adapter is ON (useBigIntegerStringAdapter=true)
89+ final LazyConverterMap adapters = new LazyConverterMap ();
90+ assertNotNull ("BigInteger adapter should be active by default" ,
91+ adapters .get (new AdapterKey (BigInteger .class , String .class )));
92+ // Disabled: BigInteger adapter is OFF
93+ final LazyConverterMap adapters2 = new LazyConverterMap ();
94+ adapters2 .setUseBigIntegerStringAdapter (false );
95+ assertNull ("BigInteger adapter should be null when flag is false" ,
96+ adapters2 .get (new AdapterKey (BigInteger .class , String .class )));
97+ }
98+
99+ /**
100+ * With useBigDecimalStringAdapter=true (default), BigDecimal fields
101+ * should be serialized as JSON strings using plain notation.
102+ */
103+ @ Test
104+ public void bigDecimalDefaultSerializesAsString () {
105+ try (final Mapper mapper = new MapperBuilder ().build ()) {
106+ final BigDecimalHolder holder = new BigDecimalHolder ();
107+ holder .score = new BigDecimal ("0.000000733915" );
108+ final String json = mapper .writeObjectAsString (holder );
109+ // Default: BigDecimal as string with plain notation (I-JSON interoperability)
110+ assertEquals ("{\" score\" :\" 0.000000733915\" }" , json );
111+ }
112+ }
113+
114+ /**
115+ * With useBigDecimalStringAdapter=false, BigDecimal fields should be
116+ * serialized as JSON numbers (strict JSON-B 3.0 / TCK compliance).
117+ */
118+ @ Test
119+ public void bigDecimalWithAdapterDisabledSerializesAsNumber () {
120+ try (final Mapper mapper = new MapperBuilder ()
121+ .setUseBigDecimalStringAdapter (false )
122+ .build ()) {
123+ final BigDecimalHolder holder = new BigDecimalHolder ();
124+ holder .score = new BigDecimal ("0.000000733915" );
125+ final String json = mapper .writeObjectAsString (holder );
126+ // Adapter disabled: BigDecimal as JSON number (scientific notation is valid per RFC 8259)
127+ assertEquals ("{\" score\" :7.33915E-7}" , json );
128+ }
129+ }
130+
49131 public static class Holder {
50132 public long value ;
51133 }
52134
53135 public static class Num {
54136 public Number value ;
55137 }
138+
139+ public static class BigDecimalHolder {
140+ public BigDecimal score ;
141+ }
56142}
0 commit comments