adapt jinja

This commit is contained in:
Xuan Son Nguyen
2026-08-21 18:51:09 +02:00
parent 7a3360f6d8
commit f262180084
7 changed files with 57 additions and 23 deletions
+12 -10
View File
@@ -3,7 +3,7 @@
#include "value.h"
// for converting from JSON to jinja values
#include <nlohmann/json.hpp>
#include "json.h"
#include <sstream>
#include <string>
@@ -1355,7 +1355,7 @@ const func_builtins & value_undefined_t::get_builtins() const {
//////////////////////////////////
static value from_json(const nlohmann::ordered_json & j, bool mark_input) {
static value from_json(const common_json_ref & j, bool mark_input) {
if (j.is_null()) {
return mk_val<value_none>();
} else if (j.is_boolean()) {
@@ -1378,8 +1378,8 @@ static value from_json(const nlohmann::ordered_json & j, bool mark_input) {
return arr;
} else if (j.is_object()) {
auto obj = mk_val<value_object>();
for (auto it = j.begin(); it != j.end(); ++it) {
obj->insert(it.key(), from_json(it.value(), mark_input));
for (const auto & [key, val] : j.items()) {
obj->insert(key, from_json(val, mark_input));
}
return obj;
} else {
@@ -1451,18 +1451,20 @@ bool value_compare(const value & a, const value & b, value_compare_op op) {
return result;
}
template<>
void global_from_json(context & ctx, const nlohmann::ordered_json & json_obj, bool mark_input) {
// printf("global_from_json: %s\n" , json_obj.dump(2).c_str());
template<typename T_JSON>
void global_from_json(context & ctx, const T_JSON & json_obj, bool mark_input) {
if (json_obj.is_null() || !json_obj.is_object()) {
throw std::runtime_error("global_from_json: input JSON value must be an object");
}
for (auto it = json_obj.begin(); it != json_obj.end(); ++it) {
JJ_DEBUG("global_from_json: setting key '%s'", it.key().c_str());
ctx.set_val(it.key(), from_json(it.value(), mark_input));
for (const auto & [key, val] : json_obj.items()) {
JJ_DEBUG("global_from_json: setting key '%s'", key.c_str());
ctx.set_val(key, from_json(val, mark_input));
}
}
template void global_from_json<common_json> (context &, const common_json &, bool);
template void global_from_json<common_json_ref>(context &, const common_json_ref &, bool);
// recursively convert value to JSON string
// TODO: avoid circular references
static void value_to_json_internal(std::ostringstream & oss, const value & val, int curr_lvl, int indent, const std::string_view item_sep, const std::string_view key_sep) {