Skip to content

Commit d4d0721

Browse files
authored
feat: improve type assertion messages with actual ValueType (#1686)
* 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 * style: run clang-format
1 parent 71d46ca commit d4d0721

1 file changed

Lines changed: 42 additions & 9 deletions

File tree

src/lib_json/json_value.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,32 @@ 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:
202+
return "nullValue";
203+
case intValue:
204+
return "intValue";
205+
case uintValue:
206+
return "uintValue";
207+
case realValue:
208+
return "realValue";
209+
case stringValue:
210+
return "stringValue";
211+
case booleanValue:
212+
return "booleanValue";
213+
case arrayValue:
214+
return "arrayValue";
215+
case objectValue:
216+
return "objectValue";
217+
}
218+
return "unknown";
219+
}
220+
221+
} // namespace Json
196222
// //////////////////////////////////////////////////////////////////
197223
#if !defined(JSON_IS_AMALGAMATION)
198224

@@ -928,8 +954,10 @@ void Value::clear() {
928954
}
929955

930956
void Value::resize(ArrayIndex newSize) {
931-
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
932-
"in Json::Value::resize(): requires arrayValue");
957+
JSON_ASSERT_MESSAGE(
958+
type() == nullValue || type() == arrayValue,
959+
"in Json::Value::resize(): requires arrayValue, but found "
960+
<< valueTypeToString(type()));
933961
if (type() == nullValue)
934962
*this = Value(arrayValue);
935963
ArrayIndex oldSize = size();
@@ -1062,7 +1090,8 @@ void Value::dupMeta(const Value& other) {
10621090
Value& Value::resolveReference(const char* key) {
10631091
JSON_ASSERT_MESSAGE(
10641092
type() == nullValue || type() == objectValue,
1065-
"in Json::Value::resolveReference(): requires objectValue");
1093+
"in Json::Value::resolveReference(): requires objectValue, but found "
1094+
<< valueTypeToString(type()));
10661095
if (type() == nullValue)
10671096
*this = Value(objectValue);
10681097
CZString actualKey(key, static_cast<unsigned>(strlen(key)),
@@ -1079,9 +1108,10 @@ Value& Value::resolveReference(const char* key) {
10791108

10801109
// @param key is not null-terminated.
10811110
Value& Value::resolveReference(char const* key, char const* end) {
1082-
JSON_ASSERT_MESSAGE(
1083-
type() == nullValue || type() == objectValue,
1084-
"in Json::Value::resolveReference(key, end): requires objectValue");
1111+
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
1112+
"in Json::Value::resolveReference(key, end): requires "
1113+
"objectValue, but found "
1114+
<< valueTypeToString(type()));
10851115
if (type() == nullValue)
10861116
*this = Value(objectValue);
10871117
CZString actualKey(key, static_cast<unsigned>(end - key),
@@ -1192,7 +1222,8 @@ Value& Value::append(const Value& value) { return append(Value(value)); }
11921222

11931223
Value& Value::append(Value&& value) {
11941224
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
1195-
"in Json::Value::append: requires arrayValue");
1225+
"in Json::Value::append: requires arrayValue, but found "
1226+
<< valueTypeToString(type()));
11961227
if (type() == nullValue) {
11971228
*this = Value(arrayValue);
11981229
}
@@ -1251,8 +1282,10 @@ bool Value::removeMember(String const& key, Value* removed) {
12511282
}
12521283

12531284
void Value::removeMember(const char* key) {
1254-
JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
1255-
"in Json::Value::removeMember(): requires objectValue");
1285+
JSON_ASSERT_MESSAGE(
1286+
type() == nullValue || type() == objectValue,
1287+
"in Json::Value::removeMember(): requires objectValue, but found "
1288+
<< valueTypeToString(type()));
12561289
if (type() == nullValue)
12571290
return;
12581291

0 commit comments

Comments
 (0)