Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions include/json/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#ifndef JSON_JSON_H_INCLUDED
#define JSON_JSON_H_INCLUDED

#include "config.h"
#include "json_features.h"
#include "reader.h"
#include "value.h"
#include "writer.h"
#include "config.h" // IWYU pragma: export
#include "json_features.h" // IWYU pragma: export
#include "reader.h" // IWYU pragma: export
#include "value.h" // IWYU pragma: export
#include "writer.h" // IWYU pragma: export

#endif // JSON_JSON_H_INCLUDED
5 changes: 4 additions & 1 deletion include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class JSON_API Reader {
* \return \c true if the document was successfully parsed, \c false if an
* error occurred.
*/
bool parse(const std::string& document, Value& root,
bool parse(std::string_view document, Value& root,
bool collectComments = true);

/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
Expand Down Expand Up @@ -400,6 +400,9 @@ class JSON_API CharReaderBuilder : public CharReader::Factory {
bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
String* errs);

bool JSON_API parseFromString(CharReader::Factory const&, std::string_view,
Value* root, JSONCPP_STRING* errs);

/** \brief Read from 'sin' into 'root'.
*
* Always keep comments from the input JSON.
Expand Down
1 change: 1 addition & 0 deletions include/json/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
* \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
*/
static void setDefaults(Json::Value* settings);
static void updateDefaults(const Json::Value& settings);
};

/** \brief Abstract class for writers.
Expand Down
11 changes: 10 additions & 1 deletion src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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();
Expand Down Expand Up @@ -1992,6 +1992,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);
}

IStream& operator>>(IStream& sin, Value& root) {
CharReaderBuilder b;
String errs;
Expand Down
32 changes: 22 additions & 10 deletions src/lib_json/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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([] {
//! [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) {
Expand Down