Skip to content

Commit fe177fe

Browse files
committed
fix tojson ensure_ascii
1 parent ec271d7 commit fe177fe

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

include/minja/minja.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,7 @@ inline std::shared_ptr<Context> Context::builtins() {
27362736
globals.set("raise_exception", simple_function("raise_exception", { "message" }, [](const std::shared_ptr<Context> &, Value & args) -> Value {
27372737
throw std::runtime_error(args.at("message").get<std::string>());
27382738
}));
2739-
globals.set("tojson", simple_function("tojson", { "value", "indent" }, [](const std::shared_ptr<Context> &, Value & args) {
2739+
globals.set("tojson", simple_function("tojson", { "value", "indent", "ensure_ascii" }, [](const std::shared_ptr<Context> &, Value & args) {
27402740
return Value(args.at("value").dump(args.get<int64_t>("indent", -1), /* to_json= */ true));
27412741
}));
27422742
globals.set("items", simple_function("items", { "object" }, [](const std::shared_ptr<Context> &, Value & args) {

scripts/fetch_templates_and_goldens.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def strftime_now(format):
5050
now = datetime.datetime.strptime(TEST_DATE, "%Y-%m-%d")
5151
return now.strftime(format)
5252

53+
def tojson(value, indent=None, ensure_ascii=False, sort_keys=False):
54+
return json.dumps(value, indent=indent, ensure_ascii=ensure_ascii, sort_keys=sort_keys)
5355

5456
def join_cmake_path(parent, child):
5557
'''
@@ -119,8 +121,11 @@ def __init__(self, template, env=None, filters=None, global_functions=None):
119121
env = jinja2.Environment(
120122
trim_blocks=True,
121123
lstrip_blocks=True,
122-
extensions=[jinja2.ext.loopcontrols]
124+
extensions=[jinja2.ext.loopcontrols],
123125
)
126+
# https://jinja.palletsprojects.com/en/stable/api/#policies
127+
env.policies["json.dumps_function"] = tojson
128+
env.filters['tojson'] = tojson
124129
if filters:
125130
for name, func in filters.items():
126131
env.filters[name] = func

0 commit comments

Comments
 (0)