Merge fe2c15d38800966d2df05ee2a893840f652ea3e9 into ca98c98457b1163cca1f7d8db62827c115fec6d1

This commit is contained in:
Marcus Boerger 2025-05-27 21:09:43 +02:00 committed by GitHub
commit b71a6d7baf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 17 deletions

View File

@ -6,10 +6,10 @@
#ifndef JSON_JSON_H_INCLUDED #ifndef JSON_JSON_H_INCLUDED
#define JSON_JSON_H_INCLUDED #define JSON_JSON_H_INCLUDED
#include "config.h" #include "config.h" // IWYU pragma: export
#include "json_features.h" #include "json_features.h" // IWYU pragma: export
#include "reader.h" #include "reader.h" // IWYU pragma: export
#include "value.h" #include "value.h" // IWYU pragma: export
#include "writer.h" #include "writer.h" // IWYU pragma: export
#endif // JSON_JSON_H_INCLUDED #endif // JSON_JSON_H_INCLUDED

View File

@ -74,7 +74,7 @@ public:
* \return \c true if the document was successfully parsed, \c false if an * \return \c true if the document was successfully parsed, \c false if an
* error occurred. * error occurred.
*/ */
bool parse(const std::string& document, Value& root, bool parse(std::string_view document, Value& root,
bool collectComments = true); bool collectComments = true);
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
@ -400,6 +400,9 @@ public:
bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root, bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
String* errs); String* errs);
bool JSON_API parseFromString(CharReader::Factory const&, std::string_view,
Value* root, JSONCPP_STRING* errs);
/** \brief Read from 'sin' into 'root'. /** \brief Read from 'sin' into 'root'.
* *
* Always keep comments from the input JSON. * Always keep comments from the input JSON.

View File

@ -143,6 +143,7 @@ public:
* \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
*/ */
static void setDefaults(Json::Value* settings); static void setDefaults(Json::Value* settings);
static void updateDefaults(const Json::Value& settings);
}; };
/** \brief Abstract class for writers. /** \brief Abstract class for writers.

View File

@ -78,7 +78,7 @@ Reader::Reader() : features_(Features::all()) {}
Reader::Reader(const Features& features) : features_(features) {} Reader::Reader(const Features& features) : features_(features) {}
bool Reader::parse(const std::string& document, Value& root, bool Reader::parse(std::string_view document, Value& root,
bool collectComments) { bool collectComments) {
document_.assign(document.begin(), document.end()); document_.assign(document.begin(), document.end());
const char* begin = document_.c_str(); const char* begin = document_.c_str();
@ -1992,6 +1992,15 @@ bool parseFromStream(CharReader::Factory const& fact, IStream& sin, Value* root,
return reader->parse(begin, end, root, errs); return reader->parse(begin, end, root, errs);
} }
bool parseFromString(
CharReader::Factory const& fact, std::string_view doc, Value* root, JSONCPP_STRING* errs) {
char const* begin = doc.data();
char const* end = begin + doc.size();
// Note that we do not actually need a null-terminator.
CharReaderPtr const reader(fact.newCharReader());
return reader->parse(begin, end, root, errs);
}
IStream& operator>>(IStream& sin, Value& root) { IStream& operator>>(IStream& sin, Value& root) {
CharReaderBuilder b; CharReaderBuilder b;
String errs; String errs;

View File

@ -1170,18 +1170,30 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const {
Value& StreamWriterBuilder::operator[](const String& key) { Value& StreamWriterBuilder::operator[](const String& key) {
return settings_[key]; return settings_[key];
} }
static Json::Value& global_settings_ = *new Json::Value([] {
//! [StreamWriterBuilderDefaults]
Json::Value settings;
settings["commentStyle"] = "All";
settings["indentation"] = "\t";
settings["enableYAMLCompatibility"] = false;
settings["dropNullPlaceholders"] = false;
settings["useSpecialFloats"] = false;
settings["emitUTF8"] = false;
settings["precision"] = 17;
settings["precisionType"] = "significant";
//! [StreamWriterBuilderDefaults]
return settings;
}());
// static // static
void StreamWriterBuilder::setDefaults(Json::Value* settings) { void StreamWriterBuilder::setDefaults(Json::Value* settings) {
//! [StreamWriterBuilderDefaults] *settings = global_settings_;
(*settings)["commentStyle"] = "All"; }
(*settings)["indentation"] = "\t";
(*settings)["enableYAMLCompatibility"] = false; // static
(*settings)["dropNullPlaceholders"] = false; void StreamWriterBuilder::updateDefaults(const Json::Value& settings) {
(*settings)["useSpecialFloats"] = false; global_settings_ = settings;
(*settings)["emitUTF8"] = false;
(*settings)["precision"] = 17;
(*settings)["precisionType"] = "significant";
//! [StreamWriterBuilderDefaults]
} }
String writeString(StreamWriter::Factory const& factory, Value const& root) { String writeString(StreamWriter::Factory const& factory, Value const& root) {