Skip to content

Commit 25c5781

Browse files
committed
Add new JSON_USE_NULLREF flag
This patch adds a new flag, JSON_USE_NULLREF, which removes the legacy singletons null, nullRef for consumers that require not having static initialized globals, like Chromium.
1 parent 9ef812a commit 25c5781

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

Diff for: include/json/config.h

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
#define JSON_USE_EXCEPTION 1
3131
#endif
3232

33+
// Temporary, tracked for removal with issue #982.
34+
#ifndef JSON_USE_NULLREF
35+
#define JSON_USE_NULLREF 1
36+
#endif
37+
3338
/// If defined, indicates that the source file is amalgamated
3439
/// to prevent private header inclusion.
3540
/// Remarks: it is automatically defined in the generated amalgamated header.

Diff for: include/json/value.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,14 @@ class JSON_API Value {
193193
// Required for boost integration, e. g. BOOST_TEST
194194
typedef std::string value_type;
195195

196-
static const Value& null; ///< We regret this reference to a global instance;
197-
///< prefer the simpler Value().
198-
static const Value& nullRef; ///< just a kludge for binary-compatibility; same
199-
///< as null
200-
static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
196+
#if JSON_USE_NULLREF
197+
// Binary compatibility kludges, do not use.
198+
static const Value& null;
199+
static const Value& nullRef;
200+
#endif
201+
202+
// null and nullRef are deprecated, use this instead.
203+
static Value const& nullSingleton();
201204

202205
/// Minimum signed integer value that can be stored in a Json::Value.
203206
static const LargestInt minLargestInt;

Diff for: src/lib_json/json_value.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ int JSON_API msvc_pre1900_c99_snprintf(char* outBuf,
5454
#define JSON_ASSERT_UNREACHABLE assert(false)
5555

5656
namespace Json {
57-
5857
template <typename T>
5958
static std::unique_ptr<T> cloneUnique(const std::unique_ptr<T>& p) {
6059
std::unique_ptr<T> r;
@@ -72,21 +71,22 @@ static std::unique_ptr<T> cloneUnique(const std::unique_ptr<T>& p) {
7271
#else
7372
#define ALIGNAS(byte_alignment)
7473
#endif
75-
// static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = { 0 };
76-
// const unsigned char& kNullRef = kNull[0];
77-
// const Value& Value::null = reinterpret_cast<const Value&>(kNullRef);
78-
// const Value& Value::nullRef = null;
7974

8075
// static
8176
Value const& Value::nullSingleton() {
8277
static Value const nullStatic;
8378
return nullStatic;
8479
}
8580

81+
#if JSON_USE_NULLREF
8682
// for backwards compatibility, we'll leave these global references around, but
8783
// DO NOT use them in JSONCPP library code any more!
84+
// static
8885
Value const& Value::null = Value::nullSingleton();
86+
87+
// static
8988
Value const& Value::nullRef = Value::nullSingleton();
89+
#endif
9090

9191
const Int Value::minInt = Int(~(UInt(-1) / 2));
9292
const Int Value::maxInt = Int(UInt(-1) / 2);
@@ -1648,20 +1648,20 @@ const Value& Path::resolve(const Value& root) const {
16481648
for (const auto& arg : args_) {
16491649
if (arg.kind_ == PathArgument::kindIndex) {
16501650
if (!node->isArray() || !node->isValidIndex(arg.index_)) {
1651-
// Error: unable to resolve path (array value expected at position...
1652-
return Value::null;
1651+
// Error: unable to resolve path (array value expected at position... )
1652+
return Value::nullSingleton();
16531653
}
16541654
node = &((*node)[arg.index_]);
16551655
} else if (arg.kind_ == PathArgument::kindKey) {
16561656
if (!node->isObject()) {
16571657
// Error: unable to resolve path (object value expected at position...)
1658-
return Value::null;
1658+
return Value::nullSingleton();
16591659
}
16601660
node = &((*node)[arg.key_]);
16611661
if (node == &Value::nullSingleton()) {
16621662
// Error: unable to resolve path (object has no member named '' at
16631663
// position...)
1664-
return Value::null;
1664+
return Value::nullSingleton();
16651665
}
16661666
}
16671667
}

0 commit comments

Comments
 (0)