Skip to content

Commit f7bc5a3

Browse files
committed
Fix C++11 ABI breakage when compiled with C++17 (#1668)
When JSONCPP_HAS_STRING_VIEW was defined, the library dropped the `const char*` and `const String&` overloads for `operator[]`, `get`, `removeMember`, and `isMember`, breaking ABI compatibility for projects consuming the library with C++11. This change unconditionally declares and defines the legacy overloads so they are always exported, restoring compatibility.
1 parent 31b1a58 commit f7bc5a3

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

include/json/value.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class JSON_API Value {
501501
/// that name.
502502
/// \param key may contain embedded nulls.
503503
const Value& operator[](std::string_view key) const;
504-
#else
504+
#endif
505505
/// Access an object value by name, create a null member if it does not exist.
506506
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.
507507
/// Exceeding that will cause an exception.
@@ -516,7 +516,6 @@ class JSON_API Value {
516516
/// that name.
517517
/// \param key may contain embedded nulls.
518518
const Value& operator[](const String& key) const;
519-
#endif
520519
/** \brief Access an object value by name, create a null member if it does not
521520
* exist.
522521
*
@@ -534,15 +533,14 @@ class JSON_API Value {
534533
/// Return the member named key if it exist, defaultValue otherwise.
535534
/// \note deep copy
536535
Value get(std::string_view key, const Value& defaultValue) const;
537-
#else
536+
#endif
538537
/// Return the member named key if it exist, defaultValue otherwise.
539538
/// \note deep copy
540539
Value get(const char* key, const Value& defaultValue) const;
541540
/// Return the member named key if it exist, defaultValue otherwise.
542541
/// \note deep copy
543542
/// \param key may contain embedded nulls.
544543
Value get(const String& key, const Value& defaultValue) const;
545-
#endif
546544
/// Return the member named key if it exist, defaultValue otherwise.
547545
/// \note deep copy
548546
/// \note key may contain embedded nulls.
@@ -589,12 +587,11 @@ class JSON_API Value {
589587
/// \post type() is unchanged
590588
#if JSONCPP_HAS_STRING_VIEW
591589
void removeMember(std::string_view key);
592-
#else
590+
#endif
593591
void removeMember(const char* key);
594592
/// Same as removeMember(const char*)
595593
/// \param key may contain embedded nulls.
596594
void removeMember(const String& key);
597-
#endif
598595
/** \brief Remove the named map member.
599596
*
600597
* Update 'removed' iff removed.
@@ -603,12 +600,11 @@ class JSON_API Value {
603600
*/
604601
#if JSONCPP_HAS_STRING_VIEW
605602
bool removeMember(std::string_view key, Value* removed);
606-
#else
603+
#endif
607604
bool removeMember(String const& key, Value* removed);
608605
/// Same as removeMember(const char* begin, const char* end, Value* removed),
609606
/// but 'key' is null-terminated.
610607
bool removeMember(const char* key, Value* removed);
611-
#endif
612608
/// Same as removeMember(String const& key, Value* removed)
613609
bool removeMember(const char* begin, const char* end, Value* removed);
614610
/** \brief Remove the indexed array element.
@@ -623,14 +619,13 @@ class JSON_API Value {
623619
/// Return true if the object has a member named key.
624620
/// \param key may contain embedded nulls.
625621
bool isMember(std::string_view key) const;
626-
#else
622+
#endif
627623
/// Return true if the object has a member named key.
628624
/// \note 'key' must be null-terminated.
629625
bool isMember(const char* key) const;
630626
/// Return true if the object has a member named key.
631627
/// \param key may contain embedded nulls.
632628
bool isMember(const String& key) const;
633-
#endif
634629
/// Same as isMember(String const& key)const
635630
bool isMember(const char* begin, const char* end) const;
636631

src/lib_json/json_value.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ const Value& Value::operator[](std::string_view key) const {
12001200
Value& Value::operator[](std::string_view key) {
12011201
return resolveReference(key.data(), key.data() + key.length());
12021202
}
1203-
#else
1203+
#endif
12041204
const Value& Value::operator[](const char* key) const {
12051205
Value const* found = find(key, key + strlen(key));
12061206
if (!found)
@@ -1221,7 +1221,6 @@ Value& Value::operator[](const char* key) {
12211221
Value& Value::operator[](const String& key) {
12221222
return resolveReference(key.data(), key.data() + key.length());
12231223
}
1224-
#endif
12251224

12261225
Value& Value::operator[](const StaticString& key) {
12271226
return resolveReference(key.c_str());
@@ -1265,14 +1264,13 @@ Value Value::get(char const* begin, char const* end,
12651264
Value Value::get(std::string_view key, const Value& defaultValue) const {
12661265
return get(key.data(), key.data() + key.length(), defaultValue);
12671266
}
1268-
#else
1267+
#endif
12691268
Value Value::get(char const* key, Value const& defaultValue) const {
12701269
return get(key, key + strlen(key), defaultValue);
12711270
}
12721271
Value Value::get(String const& key, Value const& defaultValue) const {
12731272
return get(key.data(), key.data() + key.length(), defaultValue);
12741273
}
1275-
#endif
12761274

12771275
bool Value::removeMember(const char* begin, const char* end, Value* removed) {
12781276
if (type() != objectValue) {
@@ -1292,14 +1290,13 @@ bool Value::removeMember(const char* begin, const char* end, Value* removed) {
12921290
bool Value::removeMember(std::string_view key, Value* removed) {
12931291
return removeMember(key.data(), key.data() + key.length(), removed);
12941292
}
1295-
#else
1293+
#endif
12961294
bool Value::removeMember(const char* key, Value* removed) {
12971295
return removeMember(key, key + strlen(key), removed);
12981296
}
12991297
bool Value::removeMember(String const& key, Value* removed) {
13001298
return removeMember(key.data(), key.data() + key.length(), removed);
13011299
}
1302-
#endif
13031300

13041301
#ifdef JSONCPP_HAS_STRING_VIEW
13051302
void Value::removeMember(std::string_view key) {
@@ -1312,7 +1309,7 @@ void Value::removeMember(std::string_view key) {
13121309
CZString::noDuplication);
13131310
value_.map_->erase(actualKey);
13141311
}
1315-
#else
1312+
#endif
13161313
void Value::removeMember(const char* key) {
13171314
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
13181315
"in Json::Value::removeMember(): requires objectValue");
@@ -1323,7 +1320,6 @@ void Value::removeMember(const char* key) {
13231320
value_.map_->erase(actualKey);
13241321
}
13251322
void Value::removeMember(const String& key) { removeMember(key.c_str()); }
1326-
#endif
13271323

13281324
bool Value::removeIndex(ArrayIndex index, Value* removed) {
13291325
if (type() != arrayValue) {
@@ -1357,14 +1353,13 @@ bool Value::isMember(char const* begin, char const* end) const {
13571353
bool Value::isMember(std::string_view key) const {
13581354
return isMember(key.data(), key.data() + key.length());
13591355
}
1360-
#else
1356+
#endif
13611357
bool Value::isMember(char const* key) const {
13621358
return isMember(key, key + strlen(key));
13631359
}
13641360
bool Value::isMember(String const& key) const {
13651361
return isMember(key.data(), key.data() + key.length());
13661362
}
1367-
#endif
13681363

13691364
Value::Members Value::getMemberNames() const {
13701365
JSON_ASSERT_MESSAGE(

0 commit comments

Comments
 (0)