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

Add whitespaceOptions: colonBraceSameLine to settings #1337

Closed
Closed
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
17 changes: 17 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ enum CommentPlacement {
numberOfCommentPlacement
};

enum WhitespaceOptions {
colonBraceSameLine = 1 << 0, ///< braces just after colons on the same line
};
inline WhitespaceOptions operator|(
WhitespaceOptions left, WhitespaceOptions right) {
return static_cast<WhitespaceOptions>(static_cast<int>(left) | static_cast<int>(right));
}
inline WhitespaceOptions& operator|=(
WhitespaceOptions& left, WhitespaceOptions right) {
left = left | right;
return left;
}
inline WhitespaceOptions operator&(
WhitespaceOptions left, WhitespaceOptions right) {
return static_cast<WhitespaceOptions>(static_cast<int>(left) & static_cast<int>(right));
}

/** \brief Type of precision for formatting of real values.
*/
enum PrecisionType {
Expand Down
27 changes: 15 additions & 12 deletions include/json/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,28 @@ class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
/** Configuration of this builder.
* Available settings (case-sensitive):
* - "commentStyle": "None" or "All"
* - "indentation": "<anything>".
* - Setting this to an empty string also omits newline characters.
* - "indentation": "<anything>".
* Setting this to an empty string also omits newline characters.
* - "enableYAMLCompatibility": false or true
* - slightly change the whitespace around colons
* - "whitespaceOptions": array of any combination of the following:
* "colonBraceSameLine" - when an object or an array appears as a value
* inside an object, keep the opening brace on the same line
* - "dropNullPlaceholders": false or true
* - Drop the "null" string from the writer's output for nullValues.
* Strictly speaking, this is not valid JSON. But when the output is being
* fed to a browser's JavaScript, it makes for smaller output and the
* browser can handle the output just fine.
* Drop the "null" string from the writer's output for nullValues.
* Strictly speaking, this is not valid JSON. But when the output is being
* fed to a browser's JavaScript, it makes for smaller output and the
* browser can handle the output just fine.
* - "useSpecialFloats": false or true
* - If true, outputs non-finite floating point values in the following way:
* NaN values as "NaN", positive infinity as "Infinity", and negative
* infinity as "-Infinity".
* If true, outputs non-finite floating point values in the following way:
* NaN values as "NaN", positive infinity as "Infinity", and negative
* infinity as "-Infinity".
* - "precision": int
* - Number of precision digits for formatting of real values.
* Number of precision digits for formatting of real values.
* - "precisionType": "significant"(default) or "decimal"
* - Type of precision for formatting of real values.
* Type of precision for formatting of real values.
* - "emitUTF8": false or true
* - If true, outputs raw UTF8 strings instead of escaping them.
* If true, outputs raw UTF8 strings instead of escaping them.

* You can examine 'settings_` yourself
* to see the defaults. You can also write and read them just like any
Expand Down
34 changes: 27 additions & 7 deletions src/lib_json/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,10 @@ struct CommentStyle {
struct BuiltStyledStreamWriter : public StreamWriter {
BuiltStyledStreamWriter(String indentation, CommentStyle::Enum cs,
String colonSymbol, String nullSymbol,
String endingLineFeedSymbol, bool useSpecialFloats,
bool emitUTF8, unsigned int precision,
PrecisionType precisionType);
String endingLineFeedSymbol,
WhitespaceOptions whitespaceOptions,
bool useSpecialFloats, bool emitUTF8,
unsigned int precision, PrecisionType precisionType);
int write(Value const& root, OStream* sout) override;

private:
Expand All @@ -911,6 +912,7 @@ struct BuiltStyledStreamWriter : public StreamWriter {
String colonSymbol_;
String nullSymbol_;
String endingLineFeedSymbol_;
WhitespaceOptions whitespaceOptions_;
bool addChildValues_ : 1;
bool indented_ : 1;
bool useSpecialFloats_ : 1;
Expand All @@ -920,11 +922,13 @@ struct BuiltStyledStreamWriter : public StreamWriter {
};
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
String indentation, CommentStyle::Enum cs, String colonSymbol,
String nullSymbol, String endingLineFeedSymbol, bool useSpecialFloats,
bool emitUTF8, unsigned int precision, PrecisionType precisionType)
String nullSymbol, String endingLineFeedSymbol,
WhitespaceOptions whitespaceOptions, bool useSpecialFloats, bool emitUTF8,
unsigned int precision, PrecisionType precisionType)
: rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
endingLineFeedSymbol_(std::move(endingLineFeedSymbol)),
whitespaceOptions_(whitespaceOptions),
addChildValues_(false), indented_(false),
useSpecialFloats_(useSpecialFloats), emitUTF8_(emitUTF8),
precision_(precision), precisionType_(precisionType) {}
Expand Down Expand Up @@ -991,7 +995,11 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
writeWithIndent(
valueToQuotedStringN(name.data(), name.length(), emitUTF8_));
*sout_ << colonSymbol_;
if (whitespaceOptions_ & WhitespaceOptions::colonBraceSameLine)
indented_ = true;
writeValue(childValue);
if (whitespaceOptions_ & WhitespaceOptions::colonBraceSameLine)
indented_ = false;
if (++it == members.end()) {
writeCommentAfterValueOnSameLine(childValue);
break;
Expand Down Expand Up @@ -1169,6 +1177,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
const String cs_str = settings_["commentStyle"].asString();
const String pt_str = settings_["precisionType"].asString();
const bool eyc = settings_["enableYAMLCompatibility"].asBool();
const Value& wo_array = settings_["whitespaceOptions"];
const bool dnp = settings_["dropNullPlaceholders"].asBool();
const bool usf = settings_["useSpecialFloats"].asBool();
const bool emitUTF8 = settings_["emitUTF8"].asBool();
Expand All @@ -1195,6 +1204,15 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
} else if (indentation.empty()) {
colonSymbol = ":";
}
WhitespaceOptions whitespaceOptions = static_cast<WhitespaceOptions>(0);
for (auto wi = wo_array.begin(); wi != wo_array.end(); ++wi) {
const String& option = wi->asString();
if (option == "colonBraceSameLine") {
whitespaceOptions |= WhitespaceOptions::colonBraceSameLine;
} else {
throwRuntimeError("unknown option passed to whitespaceOptions");
}
}
String nullSymbol = "null";
if (dnp) {
nullSymbol.clear();
Expand All @@ -1203,15 +1221,16 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
pre = 17;
String endingLineFeedSymbol;
return new BuiltStyledStreamWriter(indentation, cs, colonSymbol, nullSymbol,
endingLineFeedSymbol, usf, emitUTF8, pre,
precisionType);
endingLineFeedSymbol, whitespaceOptions,
usf, emitUTF8, pre, precisionType);
}

bool StreamWriterBuilder::validate(Json::Value* invalid) const {
static const auto& valid_keys = *new std::set<String>{
"indentation",
"commentStyle",
"enableYAMLCompatibility",
"whitespaceOptions",
"dropNullPlaceholders",
"useSpecialFloats",
"emitUTF8",
Expand Down Expand Up @@ -1239,6 +1258,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings) {
(*settings)["commentStyle"] = "All";
(*settings)["indentation"] = "\t";
(*settings)["enableYAMLCompatibility"] = false;
(*settings)["whitespaceOptions"] = Value(arrayValue);
(*settings)["dropNullPlaceholders"] = false;
(*settings)["useSpecialFloats"] = false;
(*settings)["emitUTF8"] = false;
Expand Down
Loading