Skip to content

Commit fe2c15d

Browse files
committed
Some tweaks:
* Make `json.h` an IWYU import header. * Change `Reader::parse` to take its `document` parameter as `std::string_view`. * Add `static void StreamWriterBuilder::updateDefaults(const Json::Value& settings);` * Allows to set the global configuration.
1 parent ca98c98 commit fe2c15d

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

include/json/json.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#ifndef JSON_JSON_H_INCLUDED
77
#define JSON_JSON_H_INCLUDED
88

9-
#include "config.h"
10-
#include "json_features.h"
11-
#include "reader.h"
12-
#include "value.h"
13-
#include "writer.h"
9+
#include "config.h" // IWYU pragma: export
10+
#include "json_features.h" // IWYU pragma: export
11+
#include "reader.h" // IWYU pragma: export
12+
#include "value.h" // IWYU pragma: export
13+
#include "writer.h" // IWYU pragma: export
1414

1515
#endif // JSON_JSON_H_INCLUDED

include/json/reader.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class JSON_API Reader {
7474
* \return \c true if the document was successfully parsed, \c false if an
7575
* error occurred.
7676
*/
77-
bool parse(const std::string& document, Value& root,
77+
bool parse(std::string_view document, Value& root,
7878
bool collectComments = true);
7979

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

403+
bool JSON_API parseFromString(CharReader::Factory const&, std::string_view,
404+
Value* root, JSONCPP_STRING* errs);
405+
403406
/** \brief Read from 'sin' into 'root'.
404407
*
405408
* Always keep comments from the input JSON.

include/json/writer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
143143
* \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
144144
*/
145145
static void setDefaults(Json::Value* settings);
146+
static void updateDefaults(const Json::Value& settings);
146147
};
147148

148149
/** \brief Abstract class for writers.

src/lib_json/json_reader.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Reader::Reader() : features_(Features::all()) {}
7878

7979
Reader::Reader(const Features& features) : features_(features) {}
8080

81-
bool Reader::parse(const std::string& document, Value& root,
81+
bool Reader::parse(std::string_view document, Value& root,
8282
bool collectComments) {
8383
document_.assign(document.begin(), document.end());
8484
const char* begin = document_.c_str();
@@ -1992,6 +1992,15 @@ bool parseFromStream(CharReader::Factory const& fact, IStream& sin, Value* root,
19921992
return reader->parse(begin, end, root, errs);
19931993
}
19941994

1995+
bool parseFromString(
1996+
CharReader::Factory const& fact, std::string_view doc, Value* root, JSONCPP_STRING* errs) {
1997+
char const* begin = doc.data();
1998+
char const* end = begin + doc.size();
1999+
// Note that we do not actually need a null-terminator.
2000+
CharReaderPtr const reader(fact.newCharReader());
2001+
return reader->parse(begin, end, root, errs);
2002+
}
2003+
19952004
IStream& operator>>(IStream& sin, Value& root) {
19962005
CharReaderBuilder b;
19972006
String errs;

src/lib_json/json_writer.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,18 +1170,30 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const {
11701170
Value& StreamWriterBuilder::operator[](const String& key) {
11711171
return settings_[key];
11721172
}
1173-
// static
1174-
void StreamWriterBuilder::setDefaults(Json::Value* settings) {
1173+
1174+
static Json::Value& global_settings_ = *new Json::Value([] {
11751175
//! [StreamWriterBuilderDefaults]
1176-
(*settings)["commentStyle"] = "All";
1177-
(*settings)["indentation"] = "\t";
1178-
(*settings)["enableYAMLCompatibility"] = false;
1179-
(*settings)["dropNullPlaceholders"] = false;
1180-
(*settings)["useSpecialFloats"] = false;
1181-
(*settings)["emitUTF8"] = false;
1182-
(*settings)["precision"] = 17;
1183-
(*settings)["precisionType"] = "significant";
1176+
Json::Value settings;
1177+
settings["commentStyle"] = "All";
1178+
settings["indentation"] = "\t";
1179+
settings["enableYAMLCompatibility"] = false;
1180+
settings["dropNullPlaceholders"] = false;
1181+
settings["useSpecialFloats"] = false;
1182+
settings["emitUTF8"] = false;
1183+
settings["precision"] = 17;
1184+
settings["precisionType"] = "significant";
11841185
//! [StreamWriterBuilderDefaults]
1186+
return settings;
1187+
}());
1188+
1189+
// static
1190+
void StreamWriterBuilder::setDefaults(Json::Value* settings) {
1191+
*settings = global_settings_;
1192+
}
1193+
1194+
// static
1195+
void StreamWriterBuilder::updateDefaults(const Json::Value& settings) {
1196+
global_settings_ = settings;
11851197
}
11861198

11871199
String writeString(StreamWriter::Factory const& factory, Value const& root) {

0 commit comments

Comments
 (0)