Skip to content

feat(JSON): Add precision control parameters to toStyledString #1617

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,8 @@ class JSON_API Value {
/// Include delimiters and embedded newlines.
String getComment(CommentPlacement placement) const;

String toStyledString() const;
String toStyledString(const StreamWriterBuilder& builder = StreamWriterBuilder()) const;
String toStyledString(int precision, PrecisionType precisionType = PrecisionType::decimalPlaces) const;

const_iterator begin() const;
const_iterator end() const;
Expand Down
12 changes: 8 additions & 4 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1559,16 +1559,20 @@ ptrdiff_t Value::getOffsetStart() const { return start_; }

ptrdiff_t Value::getOffsetLimit() const { return limit_; }

String Value::toStyledString() const {
StreamWriterBuilder builder;

String Value::toStyledString(const StreamWriterBuilder& builder) const {
String out = this->hasComment(commentBefore) ? "\n" : "";
out += Json::writeString(builder, *this);
out += '\n';

return out;
}

String Value::toStyledString(int precision, PrecisionType precisionType) const {
StreamWriterBuilder builder;
precisionType == PrecisionType::significantDigits?builder.settings_["precisionType"] = "significant":builder.settings_["precisionType"] = "decimal";
builder.settings_["precision"] = std::min(precision, 17);
return toStyledString(builder);
}

Value::const_iterator Value::begin() const {
switch (type()) {
case arrayValue:
Expand Down