mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-08-06 11:36:37 -04:00
Add whitespaceOptions: colonBraceSameLine to settings
This commit is contained in:
parent
69098a18b9
commit
ab44e97d90
@ -124,6 +124,22 @@ enum CommentPlacement {
|
|||||||
numberOfCommentPlacement
|
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 void operator|=(
|
||||||
|
WhitespaceOptions& left, WhitespaceOptions right) {
|
||||||
|
left = left | right;
|
||||||
|
}
|
||||||
|
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.
|
/** \brief Type of precision for formatting of real values.
|
||||||
*/
|
*/
|
||||||
enum PrecisionType {
|
enum PrecisionType {
|
||||||
|
@ -94,25 +94,28 @@ public:
|
|||||||
/** Configuration of this builder.
|
/** Configuration of this builder.
|
||||||
* Available settings (case-sensitive):
|
* Available settings (case-sensitive):
|
||||||
* - "commentStyle": "None" or "All"
|
* - "commentStyle": "None" or "All"
|
||||||
* - "indentation": "<anything>".
|
* - "indentation": "<anything>".
|
||||||
* - Setting this to an empty string also omits newline characters.
|
* Setting this to an empty string also omits newline characters.
|
||||||
* - "enableYAMLCompatibility": false or true
|
* - "enableYAMLCompatibility": false or true
|
||||||
* - slightly change the whitespace around colons
|
* - 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
|
* - "dropNullPlaceholders": false or true
|
||||||
* - Drop the "null" string from the writer's output for nullValues.
|
* Drop the "null" string from the writer's output for nullValues.
|
||||||
* Strictly speaking, this is not valid JSON. But when the output is being
|
* 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
|
* fed to a browser's JavaScript, it makes for smaller output and the
|
||||||
* browser can handle the output just fine.
|
* browser can handle the output just fine.
|
||||||
* - "useSpecialFloats": false or true
|
* - "useSpecialFloats": false or true
|
||||||
* - If true, outputs non-finite floating point values in the following way:
|
* If true, outputs non-finite floating point values in the following way:
|
||||||
* NaN values as "NaN", positive infinity as "Infinity", and negative
|
* NaN values as "NaN", positive infinity as "Infinity", and negative
|
||||||
* infinity as "-Infinity".
|
* infinity as "-Infinity".
|
||||||
* - "precision": int
|
* - "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"
|
* - "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
|
* - "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
|
* You can examine 'settings_` yourself
|
||||||
* to see the defaults. You can also write and read them just like any
|
* to see the defaults. You can also write and read them just like any
|
||||||
|
@ -878,9 +878,10 @@ struct CommentStyle {
|
|||||||
struct BuiltStyledStreamWriter : public StreamWriter {
|
struct BuiltStyledStreamWriter : public StreamWriter {
|
||||||
BuiltStyledStreamWriter(String indentation, CommentStyle::Enum cs,
|
BuiltStyledStreamWriter(String indentation, CommentStyle::Enum cs,
|
||||||
String colonSymbol, String nullSymbol,
|
String colonSymbol, String nullSymbol,
|
||||||
String endingLineFeedSymbol, bool useSpecialFloats,
|
String endingLineFeedSymbol,
|
||||||
bool emitUTF8, unsigned int precision,
|
WhitespaceOptions whitespaceOptions,
|
||||||
PrecisionType precisionType);
|
bool useSpecialFloats, bool emitUTF8,
|
||||||
|
unsigned int precision, PrecisionType precisionType);
|
||||||
int write(Value const& root, OStream* sout) override;
|
int write(Value const& root, OStream* sout) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -906,6 +907,7 @@ private:
|
|||||||
String colonSymbol_;
|
String colonSymbol_;
|
||||||
String nullSymbol_;
|
String nullSymbol_;
|
||||||
String endingLineFeedSymbol_;
|
String endingLineFeedSymbol_;
|
||||||
|
WhitespaceOptions whitespaceOptions_;
|
||||||
bool addChildValues_ : 1;
|
bool addChildValues_ : 1;
|
||||||
bool indented_ : 1;
|
bool indented_ : 1;
|
||||||
bool useSpecialFloats_ : 1;
|
bool useSpecialFloats_ : 1;
|
||||||
@ -915,11 +917,13 @@ private:
|
|||||||
};
|
};
|
||||||
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
|
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
|
||||||
String indentation, CommentStyle::Enum cs, String colonSymbol,
|
String indentation, CommentStyle::Enum cs, String colonSymbol,
|
||||||
String nullSymbol, String endingLineFeedSymbol, bool useSpecialFloats,
|
String nullSymbol, String endingLineFeedSymbol,
|
||||||
bool emitUTF8, unsigned int precision, PrecisionType precisionType)
|
WhitespaceOptions whitespaceOptions, bool useSpecialFloats, bool emitUTF8,
|
||||||
|
unsigned int precision, PrecisionType precisionType)
|
||||||
: rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
|
: rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
|
||||||
colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
|
colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
|
||||||
endingLineFeedSymbol_(std::move(endingLineFeedSymbol)),
|
endingLineFeedSymbol_(std::move(endingLineFeedSymbol)),
|
||||||
|
whitespaceOptions_(whitespaceOptions),
|
||||||
addChildValues_(false), indented_(false),
|
addChildValues_(false), indented_(false),
|
||||||
useSpecialFloats_(useSpecialFloats), emitUTF8_(emitUTF8),
|
useSpecialFloats_(useSpecialFloats), emitUTF8_(emitUTF8),
|
||||||
precision_(precision), precisionType_(precisionType) {}
|
precision_(precision), precisionType_(precisionType) {}
|
||||||
@ -986,7 +990,11 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
|
|||||||
writeWithIndent(
|
writeWithIndent(
|
||||||
valueToQuotedStringN(name.data(), name.length(), emitUTF8_));
|
valueToQuotedStringN(name.data(), name.length(), emitUTF8_));
|
||||||
*sout_ << colonSymbol_;
|
*sout_ << colonSymbol_;
|
||||||
|
if (whitespaceOptions_ & WhitespaceOptions::colonBraceSameLine)
|
||||||
|
indented_ = true;
|
||||||
writeValue(childValue);
|
writeValue(childValue);
|
||||||
|
if (whitespaceOptions_ & WhitespaceOptions::colonBraceSameLine)
|
||||||
|
indented_ = false;
|
||||||
if (++it == members.end()) {
|
if (++it == members.end()) {
|
||||||
writeCommentAfterValueOnSameLine(childValue);
|
writeCommentAfterValueOnSameLine(childValue);
|
||||||
break;
|
break;
|
||||||
@ -1164,6 +1172,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
|
|||||||
const String cs_str = settings_["commentStyle"].asString();
|
const String cs_str = settings_["commentStyle"].asString();
|
||||||
const String pt_str = settings_["precisionType"].asString();
|
const String pt_str = settings_["precisionType"].asString();
|
||||||
const bool eyc = settings_["enableYAMLCompatibility"].asBool();
|
const bool eyc = settings_["enableYAMLCompatibility"].asBool();
|
||||||
|
const Value& wo_array = settings_["whitespaceOptions"];
|
||||||
const bool dnp = settings_["dropNullPlaceholders"].asBool();
|
const bool dnp = settings_["dropNullPlaceholders"].asBool();
|
||||||
const bool usf = settings_["useSpecialFloats"].asBool();
|
const bool usf = settings_["useSpecialFloats"].asBool();
|
||||||
const bool emitUTF8 = settings_["emitUTF8"].asBool();
|
const bool emitUTF8 = settings_["emitUTF8"].asBool();
|
||||||
@ -1190,6 +1199,15 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
|
|||||||
} else if (indentation.empty()) {
|
} else if (indentation.empty()) {
|
||||||
colonSymbol = ":";
|
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";
|
String nullSymbol = "null";
|
||||||
if (dnp) {
|
if (dnp) {
|
||||||
nullSymbol.clear();
|
nullSymbol.clear();
|
||||||
@ -1198,8 +1216,8 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
|
|||||||
pre = 17;
|
pre = 17;
|
||||||
String endingLineFeedSymbol;
|
String endingLineFeedSymbol;
|
||||||
return new BuiltStyledStreamWriter(indentation, cs, colonSymbol, nullSymbol,
|
return new BuiltStyledStreamWriter(indentation, cs, colonSymbol, nullSymbol,
|
||||||
endingLineFeedSymbol, usf, emitUTF8, pre,
|
endingLineFeedSymbol, whitespaceOptions,
|
||||||
precisionType);
|
usf, emitUTF8, pre, precisionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StreamWriterBuilder::validate(Json::Value* invalid) const {
|
bool StreamWriterBuilder::validate(Json::Value* invalid) const {
|
||||||
@ -1207,6 +1225,7 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const {
|
|||||||
"indentation",
|
"indentation",
|
||||||
"commentStyle",
|
"commentStyle",
|
||||||
"enableYAMLCompatibility",
|
"enableYAMLCompatibility",
|
||||||
|
"whitespaceOptions",
|
||||||
"dropNullPlaceholders",
|
"dropNullPlaceholders",
|
||||||
"useSpecialFloats",
|
"useSpecialFloats",
|
||||||
"emitUTF8",
|
"emitUTF8",
|
||||||
@ -1234,6 +1253,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings) {
|
|||||||
(*settings)["commentStyle"] = "All";
|
(*settings)["commentStyle"] = "All";
|
||||||
(*settings)["indentation"] = "\t";
|
(*settings)["indentation"] = "\t";
|
||||||
(*settings)["enableYAMLCompatibility"] = false;
|
(*settings)["enableYAMLCompatibility"] = false;
|
||||||
|
(*settings)["whitespaceOptions"] = Value(arrayValue);
|
||||||
(*settings)["dropNullPlaceholders"] = false;
|
(*settings)["dropNullPlaceholders"] = false;
|
||||||
(*settings)["useSpecialFloats"] = false;
|
(*settings)["useSpecialFloats"] = false;
|
||||||
(*settings)["emitUTF8"] = false;
|
(*settings)["emitUTF8"] = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user