Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapping BigDecimal to double or string in XContentBuilder #2188

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -36,6 +36,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Map;

Expand Down Expand Up @@ -470,6 +472,42 @@ public XContentBuilder field(XContentBuilderString name, double value) throws IO
return this;
}

public XContentBuilder field(String name, BigDecimal value) throws IOException {
return field(name, value, value.scale(), RoundingMode.HALF_UP, true);
}

public XContentBuilder field(XContentBuilderString name, BigDecimal value) throws IOException {
return field(name, value, value.scale(), RoundingMode.HALF_UP, true);
}

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) {
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) {
generator.writeString(value.toEngineeringString());
}
} else {
generator.writeString(value.toEngineeringString());
}
return this;
}

public XContentBuilder field(String name, BytesReference value) throws IOException {
field(name);
if (!value.hasArray()) {
Expand Down