-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Some tweaks for reading/writing #1609
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,7 +79,7 @@ Reader::Reader() : features_(Features::all()) {} | |
|
|
||
| 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) { | ||
| document_.assign(document.begin(), document.end()); | ||
| const char* begin = document_.c_str(); | ||
|
|
@@ -1998,6 +1998,15 @@ bool parseFromStream(CharReader::Factory const& fact, IStream& sin, Value* root, | |
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think it's fine to have Seems clear enough to me. |
||
| } | ||
|
|
||
| IStream& operator>>(IStream& sin, Value& root) { | ||
| CharReaderBuilder b; | ||
| String errs; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1170,18 +1170,30 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const { | |
| Value& StreamWriterBuilder::operator[](const String& key) { | ||
| return settings_[key]; | ||
| } | ||
| // static | ||
| void StreamWriterBuilder::setDefaults(Json::Value* settings) { | ||
|
|
||
| static Json::Value& global_settings_ = *new Json::Value([] { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is declaring a new global static, which may have implications for some libraries around storage and memory. For example, in Chrome, global statics like this aren't usually allowed. Is there a performance or other benefit causing this change? I'm not aware of any. |
||
| //! [StreamWriterBuilderDefaults] | ||
| (*settings)["commentStyle"] = "All"; | ||
| (*settings)["indentation"] = "\t"; | ||
| (*settings)["enableYAMLCompatibility"] = false; | ||
| (*settings)["dropNullPlaceholders"] = false; | ||
| (*settings)["useSpecialFloats"] = false; | ||
| (*settings)["emitUTF8"] = false; | ||
| (*settings)["precision"] = 17; | ||
| (*settings)["precisionType"] = "significant"; | ||
| 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 | ||
| void StreamWriterBuilder::setDefaults(Json::Value* settings) { | ||
| *settings = global_settings_; | ||
| } | ||
|
|
||
| // static | ||
| void StreamWriterBuilder::updateDefaults(const Json::Value& settings) { | ||
| global_settings_ = settings; | ||
| } | ||
|
|
||
| String writeString(StreamWriter::Factory const& factory, Value const& root) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes a compile time error in the build since we don't support string_view currently. I believe JsonCpp is on C++11? And
std::string_viewwas added in C++17.We have been talking about a major version change so it's quite possible bumping the standard could be part of that version change. But right now I don't think we can land this specific part of the change unfortunately.