Commit e1925b369 for llama.cpp
commit e1925b3695b8d2f2acd162fb10ad0748da0d921b
Author: Xuan-Son Nguyen <son@huggingface.co>
Date: Mon Nov 16 12:28:19 2026 +0100
jinja: support json sort_keys=true (#32366)
diff --git a/common/jinja/value.cpp b/common/jinja/value.cpp
index 335461136..b3295edbe 100644
--- a/common/jinja/value.cpp
+++ b/common/jinja/value.cpp
@@ -281,14 +281,12 @@ static value tojson(const func_args & args) {
if (is_val<value_int>(val_indent)) {
indent = static_cast<int>(val_indent->as_int());
}
- if (val_sort->as_bool()) { // undefined == false
- throw not_implemented_exception("tojson sort_keys=true not implemented");
- }
+ const bool sort_keys = val_sort->as_bool(); // undefined == false
const bool ensure_ascii = val_ascii->as_bool(); // undefined == false
auto separators = (is_val<value_array>(val_separators) ? val_separators : mk_val<value_array>())->as_array();
std::string item_sep = separators.size() > 0 ? separators[0]->as_string().str() : (indent < 0 ? ", " : ",");
std::string key_sep = separators.size() > 1 ? separators[1]->as_string().str() : ": ";
- std::string json_str = value_to_json(args.get_pos(0), indent, item_sep, key_sep);
+ std::string json_str = value_to_json(args.get_pos(0), indent, item_sep, key_sep, sort_keys);
if (ensure_ascii) {
json_str = json_ensure_ascii_preserving_format(json_str);
}
@@ -1761,7 +1759,7 @@ static std::string float_json(double val) {
// 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) {
+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, bool sort_keys) {
auto indent_str = [indent, curr_lvl]() -> std::string {
return (indent > 0) ? std::string(curr_lvl * indent, ' ') : "";
};
@@ -1808,7 +1806,7 @@ static void value_to_json_internal(std::ostringstream & oss, const value & val,
oss << newline();
for (size_t i = 0; i < arr.size(); ++i) {
oss << indent_str() << (indent > 0 ? std::string(indent, ' ') : "");
- value_to_json_internal(oss, arr[i], curr_lvl + 1, indent, item_sep, key_sep);
+ value_to_json_internal(oss, arr[i], curr_lvl + 1, indent, item_sep, key_sep, sort_keys);
if (i < arr.size() - 1) {
oss << item_sep;
}
@@ -1819,17 +1817,25 @@ static void value_to_json_internal(std::ostringstream & oss, const value & val,
oss << "]";
} else if (is_val<value_object>(val)) {
const auto & obj = val->as_ordered_object(); // IMPORTANT: need to keep exact order
+ std::vector<std::pair<std::string, value>> items;
+ items.reserve(obj.size());
+ for (const auto & pair : obj) {
+ items.emplace_back(is_val<value_float>(pair.first) ? float_json(pair.first->as_float()) : pair.first->as_string().str(), pair.second);
+ }
+ if (sort_keys) {
+ // same order as python json.dumps(sort_keys=True): utf-8 byte order is code point order
+ std::stable_sort(items.begin(), items.end(), [](const auto & a, const auto & b) { return a.first < b.first; });
+ }
oss << "{";
- if (!obj.empty()) {
+ if (!items.empty()) {
oss << newline();
size_t i = 0;
- for (const auto & pair : obj) {
+ for (const auto & [key, item] : items) {
oss << indent_str() << (indent > 0 ? std::string(indent, ' ') : "");
- const std::string key = is_val<value_float>(pair.first) ? float_json(pair.first->as_float()) : pair.first->as_string().str();
- value_to_json_internal(oss, mk_val<value_string>(key), curr_lvl + 1, indent, item_sep, key_sep);
+ value_to_json_internal(oss, mk_val<value_string>(key), curr_lvl + 1, indent, item_sep, key_sep, sort_keys);
oss << key_sep;
- value_to_json_internal(oss, pair.second, curr_lvl + 1, indent, item_sep, key_sep);
- if (i < obj.size() - 1) {
+ value_to_json_internal(oss, item, curr_lvl + 1, indent, item_sep, key_sep, sort_keys);
+ if (i < items.size() - 1) {
oss << item_sep;
}
oss << newline();
@@ -1843,9 +1849,9 @@ static void value_to_json_internal(std::ostringstream & oss, const value & val,
}
}
-std::string value_to_json(const value & val, int indent, const std::string_view item_sep, const std::string_view key_sep) {
+std::string value_to_json(const value & val, int indent, const std::string_view item_sep, const std::string_view key_sep, bool sort_keys) {
std::ostringstream oss;
- value_to_json_internal(oss, val, 0, indent, item_sep, key_sep);
+ value_to_json_internal(oss, val, 0, indent, item_sep, key_sep, sort_keys);
JJ_DEBUG("value_to_json: result=%s", oss.str().c_str());
return oss.str();
}
diff --git a/common/jinja/value.h b/common/jinja/value.h
index bdc35ed39..d39b41eac 100644
--- a/common/jinja/value.h
+++ b/common/jinja/value.h
@@ -199,7 +199,7 @@ value find_builtin_func(context & ctx, const std::string & name, value & input);
value try_builtin_func(context & ctx, const std::string & name, value & input, bool undef_on_missing = false);
value apply_filter_by_name(context & ctx, std::string filter_id, value input, const func_args & args);
-std::string value_to_json(const value & val, int indent = -1, const std::string_view item_sep = ", ", const std::string_view key_sep = ": ");
+std::string value_to_json(const value & val, int indent = -1, const std::string_view item_sep = ", ", const std::string_view key_sep = ": ", bool sort_keys = false);
// Note: only used for debugging purposes
std::string value_to_string_repr(const value & val);
diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp
index 06064c494..a2c14e9a7 100644
--- a/tests/test-jinja.cpp
+++ b/tests/test-jinja.cpp
@@ -1132,6 +1132,12 @@ static void test_filters(testing & t) {
"{\"a\": 1, \"b\": 2}"
);
+ test_template(t, "tojson sort_keys=true nested with separators",
+ "{{ data|tojson(sort_keys=true, separators=(',',':')) }}",
+ {{"data", {{"name", "f"}, {"args", {{"z", 1}, {"a", json::array({json{{"y", 2}, {"b", 3}}})}}}}}},
+ "{\"args\":{\"a\":[{\"b\":3,\"y\":2}],\"z\":1},\"name\":\"f\"}"
+ );
+
test_template(t, "tojson",
"{{ data|tojson }}",
{{"data", {{"a", 1}, {"b", json::array({1, 2})}}}},