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
diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp
index 168251e..ac88109 100644
--- a/src/lib_json/json_value.cpp
+++ b/src/lib_json/json_value.cpp
@@ -193,6 +193,32 @@
// ValueInternals...
// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
+
+namespace Json {
+
+static const char* valueTypeToString(ValueType type) {
+ switch (type) {
+ case nullValue:
+ return "nullValue";
+ case intValue:
+ return "intValue";
+ case uintValue:
+ return "uintValue";
+ case realValue:
+ return "realValue";
+ case stringValue:
+ return "stringValue";
+ case booleanValue:
+ return "booleanValue";
+ case arrayValue:
+ return "arrayValue";
+ case objectValue:
+ return "objectValue";
+ }
+ return "unknown";
+}
+
+} // namespace Json
// //////////////////////////////////////////////////////////////////
#if !defined(JSON_IS_AMALGAMATION)
@@ -928,8 +954,10 @@
}
void Value::resize(ArrayIndex newSize) {
- JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
- "in Json::Value::resize(): requires arrayValue");
+ JSON_ASSERT_MESSAGE(
+ type() == nullValue || type() == arrayValue,
+ "in Json::Value::resize(): requires arrayValue, but found "
+ << valueTypeToString(type()));
if (type() == nullValue)
*this = Value(arrayValue);
ArrayIndex oldSize = size();
@@ -1062,7 +1090,8 @@
Value& Value::resolveReference(const char* key) {
JSON_ASSERT_MESSAGE(
type() == nullValue || type() == objectValue,
- "in Json::Value::resolveReference(): requires objectValue");
+ "in Json::Value::resolveReference(): requires objectValue, but found "
+ << valueTypeToString(type()));
if (type() == nullValue)
*this = Value(objectValue);
CZString actualKey(key, static_cast<unsigned>(strlen(key)),
@@ -1079,9 +1108,10 @@
// @param key is not null-terminated.
Value& Value::resolveReference(char const* key, char const* end) {
- JSON_ASSERT_MESSAGE(
- type() == nullValue || type() == objectValue,
- "in Json::Value::resolveReference(key, end): requires objectValue");
+ JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
+ "in Json::Value::resolveReference(key, end): requires "
+ "objectValue, but found "
+ << valueTypeToString(type()));
if (type() == nullValue)
*this = Value(objectValue);
CZString actualKey(key, static_cast<unsigned>(end - key),
@@ -1192,7 +1222,8 @@
Value& Value::append(Value&& value) {
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
- "in Json::Value::append: requires arrayValue");
+ "in Json::Value::append: requires arrayValue, but found "
+ << valueTypeToString(type()));
if (type() == nullValue) {
*this = Value(arrayValue);
}
@@ -1251,8 +1282,10 @@
}
void Value::removeMember(const char* key) {
- JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
- "in Json::Value::removeMember(): requires objectValue");
+ JSON_ASSERT_MESSAGE(
+ type() == nullValue || type() == objectValue,
+ "in Json::Value::removeMember(): requires objectValue, but found "
+ << valueTypeToString(type()));
if (type() == nullValue)
return;