Skip to content
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
5 changes: 5 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,11 @@ class JSON_API Value {
String getComment(CommentPlacement placement) const;

String toStyledString() const;
/// Serialize using the settings of \p builder.
String toStyledString(const StreamWriterBuilder& builder) const;
/// Serialize with the given floating-point precision (capped at 17).
String toStyledString(unsigned int precision,
PrecisionType precisionType = PrecisionType::decimalPlaces) const;

const_iterator begin() const;
const_iterator end() const;
Expand Down
17 changes: 16 additions & 1 deletion src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,14 +1556,29 @@ ptrdiff_t Value::getOffsetLimit() const { return limit_; }

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

String out = this->hasComment(commentBefore) ? "\n" : "";
String Value::toStyledString(const StreamWriterBuilder& builder) const {
Comment thread
greptile-apps[bot] marked this conversation as resolved.
const bool emitComments =
builder.settings_["commentStyle"].asString() != "None";
String out = (emitComments && this->hasComment(commentBefore)) ? "\n" : "";
out += Json::writeString(builder, *this);
out += '\n';

return out;
}

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

Value::const_iterator Value::begin() const {
switch (type()) {
case arrayValue:
Expand Down
26 changes: 26 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2093,6 +2093,32 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, CommentBefore) {
}
}

JSONTEST_FIXTURE_LOCAL(ValueTest, toStyledStringWithBuilder) {
Json::Value val(1);
val.setComment(Json::String("// comment"), Json::commentBefore);
Json::StreamWriterBuilder wbuilder;
JSONTEST_ASSERT_STRING_EQUAL("\n// comment\n1\n",
val.toStyledString(wbuilder));
wbuilder.settings_["commentStyle"] = "None";
JSONTEST_ASSERT_STRING_EQUAL("1\n", val.toStyledString(wbuilder));

Json::Value obj;
obj["a"] = 1;
wbuilder.settings_["indentation"] = "";
JSONTEST_ASSERT_STRING_EQUAL("{\"a\":1}\n", obj.toStyledString(wbuilder));
}

JSONTEST_FIXTURE_LOCAL(ValueTest, toStyledStringWithPrecision) {
Json::Value val(3.14159265);
JSONTEST_ASSERT_STRING_EQUAL(
"3.14\n", val.toStyledString(2, Json::PrecisionType::decimalPlaces));
JSONTEST_ASSERT_STRING_EQUAL(
"3.1\n", val.toStyledString(2, Json::PrecisionType::significantDigits));
JSONTEST_ASSERT_STRING_EQUAL(
val.toStyledString(17, Json::PrecisionType::significantDigits),
val.toStyledString(100, Json::PrecisionType::significantDigits));
}

JSONTEST_FIXTURE_LOCAL(ValueTest, zeroes) {
char const cstr[] = "h\0i";
Json::String binary(cstr, sizeof(cstr)); // include trailing 0
Expand Down