diff --git a/include/json/value.h b/include/json/value.h index 171f20acf..8df88c06c 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -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; diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 1725f771b..81ba90bf1 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -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 { + 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: diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index e09b87b84..04f7a63bc 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -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