Merge pull request #11790 from jbarthelmes/master

Add a null-check for XContentBuilder#field for BigDecimals
This commit is contained in:
Adrien Grand 2015-06-25 09:45:28 +02:00
commit 7cf3096c3c
1 changed files with 20 additions and 12 deletions

View File

@ -500,28 +500,36 @@ public final class XContentBuilder implements BytesStream, Releasable {
public XContentBuilder field(String name, BigDecimal value, int scale, RoundingMode rounding, boolean toDouble) throws IOException {
field(name);
if (toDouble) {
try {
generator.writeNumber(value.setScale(scale, rounding).doubleValue());
} catch (ArithmeticException e) {
if (value == null) {
generator.writeNull();
} else {
if (toDouble) {
try {
generator.writeNumber(value.setScale(scale, rounding).doubleValue());
} catch (ArithmeticException e) {
generator.writeString(value.toEngineeringString());
}
} else {
generator.writeString(value.toEngineeringString());
}
} else {
generator.writeString(value.toEngineeringString());
}
return this;
}
public XContentBuilder field(XContentBuilderString name, BigDecimal value, int scale, RoundingMode rounding, boolean toDouble) throws IOException {
field(name);
if (toDouble) {
try {
generator.writeNumber(value.setScale(scale, rounding).doubleValue());
} catch (ArithmeticException e) {
if (value == null) {
generator.writeNull();
} else {
if (toDouble) {
try {
generator.writeNumber(value.setScale(scale, rounding).doubleValue());
} catch (ArithmeticException e) {
generator.writeString(value.toEngineeringString());
}
} else {
generator.writeString(value.toEngineeringString());
}
} else {
generator.writeString(value.toEngineeringString());
}
return this;
}