Skip to content

Commit fca10e3

Browse files
committed
1 parent eca5eb3 commit fca10e3

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

release-notes/CREDITS

+4
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ Shay Banon
3939
Alex Soto: (lordofthejars@github)
4040
* Reported #173: An exception is thrown for a valid JsonPointer expression
4141

42+
Derek Clarkson (drekka@github)
43+
* Reported #184: WRITE_NUMBERS_AS_STRINGS disables WRITE_BIGDECIMAL_AS_PLAIN
44+
(2.4.6 / 2.5.2)
45+

release-notes/VERSION

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ json-specificity (naming due to historical reasons).
1313
=== Releases ===
1414
------------------------------------------------------------------------
1515

16+
2.4.6 (not released yet)
17+
18+
#184: WRITE_NUMBERS_AS_STRINGS disables WRITE_BIGDECIMAL_AS_PLAIN
19+
(reported by Derek C)
20+
1621
2.4.5 (13-Jan-2015)
1722

1823
No changes since 2.4.4.

src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.math.BigInteger;
66

77
import com.fasterxml.jackson.core.*;
8+
import com.fasterxml.jackson.core.JsonGenerator.Feature;
89
import com.fasterxml.jackson.core.io.*;
910

1011
public class UTF8JsonGenerator
@@ -844,7 +845,7 @@ public void writeNumber(BigInteger value)
844845
if (value == null) {
845846
_writeNull();
846847
} else if (_cfgNumbersAsStrings) {
847-
_writeQuotedRaw(value);
848+
_writeQuotedRaw(value.toString());
848849
} else {
849850
writeRaw(value.toString());
850851
}
@@ -892,7 +893,8 @@ public void writeNumber(BigDecimal value)
892893
if (value == null) {
893894
_writeNull();
894895
} else if (_cfgNumbersAsStrings) {
895-
_writeQuotedRaw(value);
896+
String raw = isEnabled(Feature.WRITE_BIGDECIMAL_AS_PLAIN) ? value.toPlainString() : value.toString();
897+
_writeQuotedRaw(raw);
896898
} else if (isEnabled(Feature.WRITE_BIGDECIMAL_AS_PLAIN)) {
897899
writeRaw(value.toPlainString());
898900
} else {
@@ -912,13 +914,13 @@ public void writeNumber(String encodedValue)
912914
}
913915
}
914916

915-
private final void _writeQuotedRaw(Object value) throws IOException
917+
private final void _writeQuotedRaw(String value) throws IOException
916918
{
917919
if (_outputTail >= _outputEnd) {
918920
_flushBuffer();
919921
}
920922
_outputBuffer[_outputTail++] = BYTE_QUOTE;
921-
writeRaw(value.toString());
923+
writeRaw(value);
922924
if (_outputTail >= _outputEnd) {
923925
_flushBuffer();
924926
}

src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ public void writeNumber(BigInteger value) throws IOException
642642
if (value == null) {
643643
_writeNull();
644644
} else if (_cfgNumbersAsStrings) {
645-
_writeQuotedRaw(value);
645+
_writeQuotedRaw(value.toString());
646646
} else {
647647
writeRaw(value.toString());
648648
}
@@ -685,7 +685,8 @@ public void writeNumber(BigDecimal value) throws IOException
685685
if (value == null) {
686686
_writeNull();
687687
} else if (_cfgNumbersAsStrings) {
688-
_writeQuotedRaw(value);
688+
String raw = isEnabled(Feature.WRITE_BIGDECIMAL_AS_PLAIN) ? value.toPlainString() : value.toString();
689+
_writeQuotedRaw(raw);
689690
} else if (isEnabled(Feature.WRITE_BIGDECIMAL_AS_PLAIN)) {
690691
writeRaw(value.toPlainString());
691692
} else {
@@ -704,13 +705,13 @@ public void writeNumber(String encodedValue) throws IOException
704705
}
705706
}
706707

707-
private void _writeQuotedRaw(Object value) throws IOException
708+
private void _writeQuotedRaw(String value) throws IOException
708709
{
709710
if (_outputTail >= _outputEnd) {
710711
_flushBuffer();
711712
}
712713
_outputBuffer[_outputTail++] = '"';
713-
writeRaw(value.toString());
714+
writeRaw(value);
714715
if (_outputTail >= _outputEnd) {
715716
_flushBuffer();
716717
}

src/test/java/com/fasterxml/jackson/core/json/TestJsonGeneratorFeatures.java

+22
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ public void testBigDecimalAsPlain() throws IOException
8484
jg.close();
8585
assertEquals("100", sw.toString());
8686
}
87+
88+
// [issue#184]
89+
public void testBigDecimalAsPlainString() throws Exception
90+
{
91+
JsonFactory jf = new JsonFactory();
92+
BigDecimal ENG = new BigDecimal("1E+2");
93+
jf.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
94+
jf.enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);
95+
96+
StringWriter sw = new StringWriter();
97+
JsonGenerator jg = jf.createGenerator(sw);
98+
jg.writeNumber(ENG);
99+
jg.close();
100+
assertEquals(quote("100"), sw.toString());
101+
102+
// also, as bytes
103+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
104+
jg = jf.createGenerator(bos);
105+
jg.writeNumber(ENG);
106+
jg.close();
107+
assertEquals(quote("100"), bos.toString("UTF-8"));
108+
}
87109

88110
private String _writeNumbers(JsonFactory jf) throws IOException
89111
{

0 commit comments

Comments
 (0)