Skip to content

Commit 72cb770

Browse files
author
Fszontagh
committed
Fix json_decode recursive conversion and add OBJECT toString()
1 parent b8dd34e commit 72cb770

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/Modules/BuiltIn/JsonConverters.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ Symbols::ValuePtr convertJsonNumberToValue(const nlohmann::json& json) {
8383
* @brief Recursively converts a JSON object to VoidScript ObjectMap
8484
*
8585
* @param json The JSON object to convert
86+
* @param context Additional context information
8687
* @return Symbols::ObjectMap The converted ObjectMap
8788
*/
88-
Symbols::ObjectMap convertJsonObjectToMap(const nlohmann::json& json) {
89+
Symbols::ObjectMap convertJsonObjectToMap(const nlohmann::json& json, const std::string& context = "") {
8990
Symbols::ObjectMap result;
9091

9192
for (auto it = json.begin(); it != json.end(); ++it) {
@@ -124,14 +125,14 @@ Symbols::ObjectMap convertJsonObjectToMap(const nlohmann::json& json) {
124125
} else if (element.is_string()) {
125126
arrayMap[indexKey] = Symbols::ValuePtr(element.get<std::string>());
126127
} else if (element.is_object()) {
127-
arrayMap[indexKey] = Symbols::ValuePtr(convertJsonObjectToMap(element));
128+
arrayMap[indexKey] = jsonToValueWithContext(element, context);
128129
} else if (element.is_array()) {
129-
arrayMap[indexKey] = Symbols::ValuePtr(convertJsonObjectToMap(element));
130+
arrayMap[indexKey] = jsonToValueWithContext(element, context);
130131
}
131132
}
132133
result[key] = Symbols::ValuePtr(arrayMap);
133134
} else if (value.is_object()) {
134-
result[key] = Symbols::ValuePtr(convertJsonObjectToMap(value));
135+
result[key] = jsonToValueWithContext(value, context);
135136
}
136137
}
137138

@@ -282,14 +283,14 @@ Symbols::ValuePtr jsonToValueWithContext(const nlohmann::json& json, const std::
282283
} else if (element.is_string()) {
283284
arrayMap[indexKey] = Symbols::ValuePtr(element.get<std::string>());
284285
} else if (element.is_object()) {
285-
arrayMap[indexKey] = Symbols::ValuePtr(convertJsonObjectToMap(element));
286+
arrayMap[indexKey] = jsonToValueWithContext(element, context);
286287
} else if (element.is_array()) {
287-
arrayMap[indexKey] = Symbols::ValuePtr(convertJsonObjectToMap(element));
288+
arrayMap[indexKey] = jsonToValueWithContext(element, context);
288289
}
289290
}
290291
return Symbols::ValuePtr(arrayMap);
291292
} else if (json.is_object()) {
292-
return Symbols::ValuePtr(convertJsonObjectToMap(json));
293+
return Symbols::ValuePtr(convertJsonObjectToMap(json, context));
293294
} else {
294295
throw std::runtime_error(createJsonErrorMessage("JSON to ValuePtr conversion",
295296
"unknown", context));

src/Symbols/Value.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ std::string Value::toString() const {
122122
if (type_ == Symbols::Variables::Type::UNDEFINED_TYPE) {
123123
return "undefined";
124124
}
125-
if (is_null() && type_ != Variables::Type::STRING && type_ != Variables::Type::OBJECT &&
126-
type_ != Variables::Type::CLASS) { // Allow toString on null strings/objects
125+
if (is_null() && type_ != Variables::Type::STRING) { // Allow toString on null strings/objects
127126
if (type_ == Variables::Type::NULL_TYPE || (!data_ && type_ != Variables::Type::STRING)) {
128127
return "null";
129128
}
@@ -166,8 +165,22 @@ std::string Value::toString() const {
166165
return "[Invalid Class Object]";
167166
}
168167
}
168+
case Variables::Type::OBJECT:
169+
{
170+
const auto & objMap = get<ObjectMap>();
171+
if (objMap.empty()) {
172+
return "{}";
173+
}
174+
std::string result = "{";
175+
for (auto it = objMap.begin(); it != objMap.end(); ++it) {
176+
if (it != objMap.begin()) result += ", ";
177+
result += "\"" + it->first + "\": " + it->second.toString();
178+
}
179+
result += "}";
180+
return result;
181+
}
169182
// STRING is handled above
170-
// OBJECT, NULL_TYPE will fall to default or handled by isNULL checks
183+
// NULL_TYPE will fall to default or handled by isNULL checks
171184
default:
172185
return "null"; // Should ideally be covered by isNULL or type checks
173186
}

0 commit comments

Comments
 (0)