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.
diff --git a/include/json/config.h b/include/json/config.h
index eca9e8c..8724ad9 100644
--- a/include/json/config.h
+++ b/include/json/config.h
@@ -30,6 +30,11 @@
#define JSON_USE_EXCEPTION 1
#endif
+// Temporary, tracked for removal with issue #982.
+#ifndef JSON_USE_NULLREF
+#define JSON_USE_NULLREF 1
+#endif
+
/// If defined, indicates that the source file is amalgamated
/// to prevent private header inclusion.
/// Remarks: it is automatically defined in the generated amalgamated header.
diff --git a/include/json/value.h b/include/json/value.h
index a9d9ea1..957f3f6 100644
--- a/include/json/value.h
+++ b/include/json/value.h
@@ -193,11 +193,14 @@
// Required for boost integration, e. g. BOOST_TEST
typedef std::string value_type;
- static const Value& null; ///< We regret this reference to a global instance;
- ///< prefer the simpler Value().
- static const Value& nullRef; ///< just a kludge for binary-compatibility; same
- ///< as null
- static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
+#if JSON_USE_NULLREF
+ // Binary compatibility kludges, do not use.
+ static const Value& null;
+ static const Value& nullRef;
+#endif
+
+ // null and nullRef are deprecated, use this instead.
+ static Value const& nullSingleton();
/// Minimum signed integer value that can be stored in a Json::Value.
static const LargestInt minLargestInt;
diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp
index a702c56..d813c16 100644
--- a/src/lib_json/json_value.cpp
+++ b/src/lib_json/json_value.cpp
@@ -54,7 +54,6 @@
#define JSON_ASSERT_UNREACHABLE assert(false)
namespace Json {
-
template <typename T>
static std::unique_ptr<T> cloneUnique(const std::unique_ptr<T>& p) {
std::unique_ptr<T> r;
@@ -72,10 +71,6 @@
#else
#define ALIGNAS(byte_alignment)
#endif
-// static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = { 0 };
-// const unsigned char& kNullRef = kNull[0];
-// const Value& Value::null = reinterpret_cast<const Value&>(kNullRef);
-// const Value& Value::nullRef = null;
// static
Value const& Value::nullSingleton() {
@@ -83,10 +78,15 @@
return nullStatic;
}
+#if JSON_USE_NULLREF
// for backwards compatibility, we'll leave these global references around, but
// DO NOT use them in JSONCPP library code any more!
+// static
Value const& Value::null = Value::nullSingleton();
+
+// static
Value const& Value::nullRef = Value::nullSingleton();
+#endif
const Int Value::minInt = Int(~(UInt(-1) / 2));
const Int Value::maxInt = Int(UInt(-1) / 2);
@@ -1648,20 +1648,20 @@
for (const auto& arg : args_) {
if (arg.kind_ == PathArgument::kindIndex) {
if (!node->isArray() || !node->isValidIndex(arg.index_)) {
- // Error: unable to resolve path (array value expected at position...
- return Value::null;
+ // Error: unable to resolve path (array value expected at position... )
+ return Value::nullSingleton();
}
node = &((*node)[arg.index_]);
} else if (arg.kind_ == PathArgument::kindKey) {
if (!node->isObject()) {
// Error: unable to resolve path (object value expected at position...)
- return Value::null;
+ return Value::nullSingleton();
}
node = &((*node)[arg.key_]);
if (node == &Value::nullSingleton()) {
// Error: unable to resolve path (object has no member named '' at
// position...)
- return Value::null;
+ return Value::nullSingleton();
}
}
}