|
36 | 36 | import java.io.IOException;
|
37 | 37 | import java.io.InputStream;
|
38 | 38 | import java.io.OutputStream;
|
| 39 | +import java.math.BigDecimal; |
| 40 | +import java.math.RoundingMode; |
39 | 41 | import java.util.Date;
|
40 | 42 | import java.util.Map;
|
41 | 43 |
|
@@ -470,6 +472,42 @@ public XContentBuilder field(XContentBuilderString name, double value) throws IO
|
470 | 472 | return this;
|
471 | 473 | }
|
472 | 474 |
|
| 475 | + public XContentBuilder field(String name, BigDecimal value) throws IOException { |
| 476 | + return field(name, value, value.scale(), RoundingMode.HALF_UP, true); |
| 477 | + } |
| 478 | + |
| 479 | + public XContentBuilder field(XContentBuilderString name, BigDecimal value) throws IOException { |
| 480 | + return field(name, value, value.scale(), RoundingMode.HALF_UP, true); |
| 481 | + } |
| 482 | + |
| 483 | + public XContentBuilder field(String name, BigDecimal value, int scale, RoundingMode rounding, boolean toDouble) throws IOException { |
| 484 | + field(name); |
| 485 | + if (toDouble) { |
| 486 | + try { |
| 487 | + generator.writeNumber(value.setScale(scale, rounding).doubleValue()); |
| 488 | + } catch (ArithmeticException e) { |
| 489 | + generator.writeString(value.toEngineeringString()); |
| 490 | + } |
| 491 | + } else { |
| 492 | + generator.writeString(value.toEngineeringString()); |
| 493 | + } |
| 494 | + return this; |
| 495 | + } |
| 496 | + |
| 497 | + public XContentBuilder field(XContentBuilderString name, BigDecimal value, int scale, RoundingMode rounding, boolean toDouble) throws IOException { |
| 498 | + field(name); |
| 499 | + if (toDouble) { |
| 500 | + try { |
| 501 | + generator.writeNumber(value.setScale(scale, rounding).doubleValue()); |
| 502 | + } catch (ArithmeticException e) { |
| 503 | + generator.writeString(value.toEngineeringString()); |
| 504 | + } |
| 505 | + } else { |
| 506 | + generator.writeString(value.toEngineeringString()); |
| 507 | + } |
| 508 | + return this; |
| 509 | + } |
| 510 | + |
473 | 511 | public XContentBuilder field(String name, BytesReference value) throws IOException {
|
474 | 512 | field(name);
|
475 | 513 | if (!value.hasArray()) {
|
|
0 commit comments