Skip to content

Commit 12c5c88

Browse files
committed
feat: improve type assertion messages with actual ValueType (#1627)
When indexing into a Json::Value, several assertions check that the value is an object or array. This commit enhances the error messages by reporting the actual type found, making it easier for users to debug type mismatch issues. Fixes #1627
1 parent 71d46ca commit 12c5c88

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

src/lib_json/json_value.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,24 @@ static inline void releaseStringValue(char* value, unsigned) { free(value); }
193193
// ValueInternals...
194194
// //////////////////////////////////////////////////////////////////
195195
// //////////////////////////////////////////////////////////////////
196+
197+
namespace Json {
198+
199+
static const char* valueTypeToString(ValueType type) {
200+
switch (type) {
201+
case nullValue: return "nullValue";
202+
case intValue: return "intValue";
203+
case uintValue: return "uintValue";
204+
case realValue: return "realValue";
205+
case stringValue: return "stringValue";
206+
case booleanValue: return "booleanValue";
207+
case arrayValue: return "arrayValue";
208+
case objectValue: return "objectValue";
209+
}
210+
return "unknown";
211+
}
212+
213+
} // namespace Json
196214
// //////////////////////////////////////////////////////////////////
197215
#if !defined(JSON_IS_AMALGAMATION)
198216

@@ -929,7 +947,7 @@ void Value::clear() {
929947

930948
void Value::resize(ArrayIndex newSize) {
931949
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
932-
"in Json::Value::resize(): requires arrayValue");
950+
"in Json::Value::resize(): requires arrayValue, but found " << valueTypeToString(type()));
933951
if (type() == nullValue)
934952
*this = Value(arrayValue);
935953
ArrayIndex oldSize = size();
@@ -1062,7 +1080,7 @@ void Value::dupMeta(const Value& other) {
10621080
Value& Value::resolveReference(const char* key) {
10631081
JSON_ASSERT_MESSAGE(
10641082
type() == nullValue || type() == objectValue,
1065-
"in Json::Value::resolveReference(): requires objectValue");
1083+
"in Json::Value::resolveReference(): requires objectValue, but found " << valueTypeToString(type()));
10661084
if (type() == nullValue)
10671085
*this = Value(objectValue);
10681086
CZString actualKey(key, static_cast<unsigned>(strlen(key)),
@@ -1081,7 +1099,7 @@ Value& Value::resolveReference(const char* key) {
10811099
Value& Value::resolveReference(char const* key, char const* end) {
10821100
JSON_ASSERT_MESSAGE(
10831101
type() == nullValue || type() == objectValue,
1084-
"in Json::Value::resolveReference(key, end): requires objectValue");
1102+
"in Json::Value::resolveReference(key, end): requires objectValue, but found " << valueTypeToString(type()));
10851103
if (type() == nullValue)
10861104
*this = Value(objectValue);
10871105
CZString actualKey(key, static_cast<unsigned>(end - key),
@@ -1192,7 +1210,7 @@ Value& Value::append(const Value& value) { return append(Value(value)); }
11921210

11931211
Value& Value::append(Value&& value) {
11941212
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
1195-
"in Json::Value::append: requires arrayValue");
1213+
"in Json::Value::append: requires arrayValue, but found " << valueTypeToString(type()));
11961214
if (type() == nullValue) {
11971215
*this = Value(arrayValue);
11981216
}
@@ -1252,7 +1270,7 @@ bool Value::removeMember(String const& key, Value* removed) {
12521270

12531271
void Value::removeMember(const char* key) {
12541272
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
1255-
"in Json::Value::removeMember(): requires objectValue");
1273+
"in Json::Value::removeMember(): requires objectValue, but found " << valueTypeToString(type()));
12561274
if (type() == nullValue)
12571275
return;
12581276

@@ -1707,3 +1725,4 @@ Value& Path::make(Value& root) const {
17071725
const char* version() { return JSONCPP_VERSION_STRING; }
17081726

17091727
} // namespace Json
1728+

0 commit comments

Comments
 (0)