Skip to content

Commit 4c61985

Browse files
Jingfu Yancursoragent
andcommitted
feat: add toStyledString overloads for custom builder and precision
Add Value::toStyledString(const StreamWriterBuilder&) and Value::toStyledString(unsigned int, PrecisionType) while keeping the existing no-argument overload for ABI compatibility. Skip the leading newline when the builder suppresses comments. Fixes open-source-parsers#1616 Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 3347a4b commit 4c61985

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎include/json/value.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,11 @@ class JSON_API Value {
677677
String getComment(CommentPlacement placement) const;
678678

679679
String toStyledString() const;
680+
/// Serialize using the settings of \p builder.
681+
String toStyledString(const StreamWriterBuilder& builder) const;
682+
/// Serialize with the given floating-point precision (capped at 17).
683+
String toStyledString(unsigned int precision,
684+
PrecisionType precisionType) const;
680685

681686
const_iterator begin() const;
682687
const_iterator end() const;

‎src/lib_json/json_value.cpp‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,14 +1556,29 @@ ptrdiff_t Value::getOffsetLimit() const { return limit_; }
15561556

15571557
String Value::toStyledString() const {
15581558
StreamWriterBuilder builder;
1559+
return toStyledString(builder);
1560+
}
15591561

1560-
String out = this->hasComment(commentBefore) ? "\n" : "";
1562+
String Value::toStyledString(const StreamWriterBuilder& builder) const {
1563+
const bool emitComments =
1564+
builder.settings_["commentStyle"].asString() != "None";
1565+
String out = (emitComments && this->hasComment(commentBefore)) ? "\n" : "";
15611566
out += Json::writeString(builder, *this);
15621567
out += '\n';
15631568

15641569
return out;
15651570
}
15661571

1572+
String Value::toStyledString(unsigned int precision,
1573+
PrecisionType precisionType) const {
1574+
StreamWriterBuilder builder;
1575+
builder.settings_["precisionType"] =
1576+
precisionType == PrecisionType::significantDigits ? "significant"
1577+
: "decimal";
1578+
builder.settings_["precision"] = precision;
1579+
return toStyledString(builder);
1580+
}
1581+
15671582
Value::const_iterator Value::begin() const {
15681583
switch (type()) {
15691584
case arrayValue:

‎src/test_lib_json/main.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,32 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, CommentBefore) {
20932093
}
20942094
}
20952095

2096+
JSONTEST_FIXTURE_LOCAL(ValueTest, toStyledStringWithBuilder) {
2097+
Json::Value val(1);
2098+
val.setComment(Json::String("// comment"), Json::commentBefore);
2099+
Json::StreamWriterBuilder wbuilder;
2100+
JSONTEST_ASSERT_STRING_EQUAL("\n// comment\n1\n",
2101+
val.toStyledString(wbuilder));
2102+
wbuilder.settings_["commentStyle"] = "None";
2103+
JSONTEST_ASSERT_STRING_EQUAL("1\n", val.toStyledString(wbuilder));
2104+
2105+
Json::Value obj;
2106+
obj["a"] = 1;
2107+
wbuilder.settings_["indentation"] = "";
2108+
JSONTEST_ASSERT_STRING_EQUAL("{\"a\":1}\n", obj.toStyledString(wbuilder));
2109+
}
2110+
2111+
JSONTEST_FIXTURE_LOCAL(ValueTest, toStyledStringWithPrecision) {
2112+
Json::Value val(3.14159265);
2113+
JSONTEST_ASSERT_STRING_EQUAL(
2114+
"3.14\n", val.toStyledString(2, Json::PrecisionType::decimalPlaces));
2115+
JSONTEST_ASSERT_STRING_EQUAL(
2116+
"3.1\n", val.toStyledString(2, Json::PrecisionType::significantDigits));
2117+
JSONTEST_ASSERT_STRING_EQUAL(
2118+
val.toStyledString(17, Json::PrecisionType::significantDigits),
2119+
val.toStyledString(100, Json::PrecisionType::significantDigits));
2120+
}
2121+
20962122
JSONTEST_FIXTURE_LOCAL(ValueTest, zeroes) {
20972123
char const cstr[] = "h\0i";
20982124
Json::String binary(cstr, sizeof(cstr)); // include trailing 0

0 commit comments

Comments
 (0)