@@ -31,10 +31,22 @@ namespace Json {
31
31
#else
32
32
#define ALIGNAS (byte_alignment )
33
33
#endif
34
- static const unsigned char ALIGNAS (8 ) kNull [sizeof (Value)] = { 0 };
35
- const unsigned char & kNullRef = kNull [0 ];
36
- const Value& Value::null = reinterpret_cast <const Value&>(kNullRef );
37
- const Value& Value::nullRef = null;
34
+ // static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = { 0 };
35
+ // const unsigned char& kNullRef = kNull[0];
36
+ // const Value& Value::null = reinterpret_cast<const Value&>(kNullRef);
37
+ // const Value& Value::nullRef = null;
38
+
39
+ // static
40
+ Value const & Value::nullSingleton ()
41
+ {
42
+ static Value const * nullStatic = new Value;
43
+ return *nullStatic;
44
+ }
45
+
46
+ // for backwards compatibility, we'll leave these global references around, but DO NOT
47
+ // use them in JSONCPP library code any more!
48
+ Value const & Value::null = Value::nullSingleton ();
49
+ Value const & Value::nullRef = Value::nullSingleton ();
38
50
39
51
const Int Value::minInt = Int (~(UInt (-1 ) / 2 ));
40
52
const Int Value::maxInt = Int (UInt (-1 ) / 2 );
@@ -972,7 +984,7 @@ Value& Value::operator[](ArrayIndex index) {
972
984
if (it != value_.map_ ->end () && (*it).first == key)
973
985
return (*it).second ;
974
986
975
- ObjectValues::value_type defaultValue (key, nullRef );
987
+ ObjectValues::value_type defaultValue (key, nullSingleton () );
976
988
it = value_.map_ ->insert (it, defaultValue);
977
989
return (*it).second ;
978
990
}
@@ -989,11 +1001,11 @@ const Value& Value::operator[](ArrayIndex index) const {
989
1001
type_ == nullValue || type_ == arrayValue,
990
1002
" in Json::Value::operator[](ArrayIndex)const: requires arrayValue" );
991
1003
if (type_ == nullValue)
992
- return nullRef ;
1004
+ return nullSingleton () ;
993
1005
CZString key (index );
994
1006
ObjectValues::const_iterator it = value_.map_ ->find (key);
995
1007
if (it == value_.map_ ->end ())
996
- return nullRef ;
1008
+ return nullSingleton () ;
997
1009
return (*it).second ;
998
1010
}
999
1011
@@ -1027,7 +1039,7 @@ Value& Value::resolveReference(const char* key) {
1027
1039
if (it != value_.map_ ->end () && (*it).first == actualKey)
1028
1040
return (*it).second ;
1029
1041
1030
- ObjectValues::value_type defaultValue (actualKey, nullRef );
1042
+ ObjectValues::value_type defaultValue (actualKey, nullSingleton () );
1031
1043
it = value_.map_ ->insert (it, defaultValue);
1032
1044
Value& value = (*it).second ;
1033
1045
return value;
@@ -1047,15 +1059,15 @@ Value& Value::resolveReference(char const* key, char const* cend)
1047
1059
if (it != value_.map_ ->end () && (*it).first == actualKey)
1048
1060
return (*it).second ;
1049
1061
1050
- ObjectValues::value_type defaultValue (actualKey, nullRef );
1062
+ ObjectValues::value_type defaultValue (actualKey, nullSingleton () );
1051
1063
it = value_.map_ ->insert (it, defaultValue);
1052
1064
Value& value = (*it).second ;
1053
1065
return value;
1054
1066
}
1055
1067
1056
1068
Value Value::get (ArrayIndex index, const Value& defaultValue) const {
1057
1069
const Value* value = &((*this )[index ]);
1058
- return value == &nullRef ? defaultValue : *value;
1070
+ return value == &nullSingleton () ? defaultValue : *value;
1059
1071
}
1060
1072
1061
1073
bool Value::isValidIndex (ArrayIndex index) const { return index < size (); }
@@ -1074,13 +1086,13 @@ Value const* Value::find(char const* key, char const* cend) const
1074
1086
const Value& Value::operator [](const char * key) const
1075
1087
{
1076
1088
Value const * found = find (key, key + strlen (key));
1077
- if (!found) return nullRef ;
1089
+ if (!found) return nullSingleton () ;
1078
1090
return *found;
1079
1091
}
1080
1092
Value const & Value::operator [](JSONCPP_STRING const & key) const
1081
1093
{
1082
1094
Value const * found = find (key.data (), key.data () + key.length ());
1083
- if (!found) return nullRef ;
1095
+ if (!found) return nullSingleton () ;
1084
1096
return *found;
1085
1097
}
1086
1098
@@ -1103,7 +1115,7 @@ Value& Value::operator[](const CppTL::ConstString& key) {
1103
1115
Value const & Value::operator [](CppTL::ConstString const & key) const
1104
1116
{
1105
1117
Value const * found = find (key.c_str (), key.end_c_str ());
1106
- if (!found) return nullRef ;
1118
+ if (!found) return nullSingleton () ;
1107
1119
return *found;
1108
1120
}
1109
1121
#endif
@@ -1151,7 +1163,7 @@ Value Value::removeMember(const char* key)
1151
1163
JSON_ASSERT_MESSAGE (type_ == nullValue || type_ == objectValue,
1152
1164
" in Json::Value::removeMember(): requires objectValue" );
1153
1165
if (type_ == nullValue)
1154
- return nullRef ;
1166
+ return nullSingleton () ;
1155
1167
1156
1168
Value removed; // null
1157
1169
removeMember (key, key + strlen (key), &removed);
@@ -1538,7 +1550,7 @@ const Value& Path::resolve(const Value& root) const {
1538
1550
// Error: unable to resolve path (object value expected at position...)
1539
1551
}
1540
1552
node = &((*node)[arg.key_ ]);
1541
- if (node == &Value::nullRef ) {
1553
+ if (node == &Value::nullSingleton () ) {
1542
1554
// Error: unable to resolve path (object has no member named '' at
1543
1555
// position...)
1544
1556
}
@@ -1559,7 +1571,7 @@ Value Path::resolve(const Value& root, const Value& defaultValue) const {
1559
1571
if (!node->isObject ())
1560
1572
return defaultValue;
1561
1573
node = &((*node)[arg.key_ ]);
1562
- if (node == &Value::nullRef )
1574
+ if (node == &Value::nullSingleton () )
1563
1575
return defaultValue;
1564
1576
}
1565
1577
}
0 commit comments