Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
injabridge.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "injabridge.h"
5
7
8/*!
9 \class InjaBridge
10 \brief Adapter for converting Qt JSON types to Inja template engine format.
11
12 InjaBridge provides static methods to convert between Qt's native JSON types
13 (QJsonObject, QJsonArray, QJsonValue) and nlohmann::json, which is the data
14 format expected by the Inja template engine.
15
16 This adapter allows QDoc to maintain its Qt-native API while leveraging
17 Inja for template-based documentation generation. All JSON data in QDoc's
18 intermediate representation (IR) uses Qt types, and InjaBridge handles the
19 conversion when rendering templates.
20
21 \note All numbers in QJsonValue are stored as doubles. When converted to
22 nlohmann::json, integers are rendered with decimal points (e.g., 30 becomes
23 "30.0" in template output). This is expected behavior.
24
25 \note Inja and nlohmann::json may report template or data errors. QDoc is
26 built with exceptions disabled (\c{-fno-exceptions}), so such errors are
27 treated as fatal and will terminate the process. This is consistent with
28 Qt's no-exception policy for command-line tools.
29
30 \sa QJsonObject, QJsonArray, QJsonValue
31*/
32
33/*!
34 \brief Converts a QJsonValue, \a value, to nlohmann::json.
35
36 Handles all QJsonValue types: Null, Bool, Double, String, Array, Object,
37 and Undefined. Undefined values are treated as null.
38
39 Returns the equivalent nlohmann::json representation.
40*/
41nlohmann::json InjaBridge::toInjaJson(const QJsonValue &value)
42{
43 switch (value.type()) {
44 case QJsonValue::Null:
45 return nullptr;
46 case QJsonValue::Bool:
47 return value.toBool();
48 case QJsonValue::Double:
49 return value.toDouble();
50 case QJsonValue::String:
51 return value.toString().toUtf8().toStdString();
52 case QJsonValue::Array:
53 return toInjaJson(value.toArray());
54 case QJsonValue::Object:
55 return toInjaJson(value.toObject());
56 case QJsonValue::Undefined:
57 return nullptr;
58 }
59 return nullptr;
60}
61
62/*!
63 \brief Converts a QJsonObject, \a obj, to nlohmann::json.
64
65 Recursively converts all values in the object, preserving the key-value
66 structure. Nested objects and arrays are handled correctly.
67
68 Returns the equivalent nlohmann::json object.
69*/
70nlohmann::json InjaBridge::toInjaJson(const QJsonObject &obj)
71{
72 nlohmann::json result = nlohmann::json::object();
73
74 for (const auto &[key, value] : obj.asKeyValueRange())
75 result[key.toString().toUtf8().toStdString()] = toInjaJson(value);
76
77 return result;
78}
79
80/*!
81 \brief Converts a QJsonArray, \a array, to nlohmann::json.
82
83 Recursively converts all elements in the array, preserving order.
84 Mixed-type arrays are supported.
85
86 Returns the equivalent nlohmann::json array.
87*/
88nlohmann::json InjaBridge::toInjaJson(const QJsonArray &array)
89{
90 nlohmann::json result = nlohmann::json::array();
91
92 for (const QJsonValue &value : array)
93 result.push_back(toInjaJson(value));
94
95 return result;
96}
97
98/*!
99 \brief Renders a template string, \a templateStr, with provided \a data.
100
101 Uses Inja to render the template with the given JSON data. The data
102 is automatically converted from QJsonObject to nlohmann::json.
103
104 The Inja template string, \a templateStr, supports Jinja2 syntax. \a data is
105 the JSON data to use for rendering.
106
107 Returns the rendered template as a QString.
108*/
109QString InjaBridge::render(const QString &templateStr, const QJsonObject &data)
110{
111 inja::Environment env;
112 nlohmann::json jsonData = toInjaJson(data);
113
114 std::string templateUtf8 = templateStr.toUtf8().toStdString();
115 std::string resultUtf8 = env.render(templateUtf8, jsonData);
116
117 return QString::fromUtf8(resultUtf8.c_str());
118}
119
120/*!
121 \brief Renders a template file with provided data.
122
123 Loads and renders a template from the filesystem, using \a templatePath
124 which holds the absolute path to the template file. The file should use
125 Inja/Jinja2 syntax. \a data is the JSON data to use for rendering.
126
127 Returns the rendered template as a QString.
128*/
129QString InjaBridge::renderFile(const QString &templatePath, const QJsonObject &data)
130{
131 inja::Environment env;
132 nlohmann::json jsonData = toInjaJson(data);
133
134 std::string pathUtf8 = templatePath.toUtf8().toStdString();
135 std::string resultUtf8 = env.render_file(pathUtf8, jsonData);
136
137 return QString::fromUtf8(resultUtf8.c_str());
138}
139
140QT_END_NAMESPACE
Combined button and popup list for selecting options.