Debian Codegraph @

doc/articles/output-formats.html

raw
1 ---
2 layout: default
3 title: Output Formats
4 ---
5
6 <div class="hgroup">
7 <div class="hgroup-inline">
8 <div class="panel">
9 <h1 id="top">Output Formats</h1>
10 </div>
11 <div style="clear: both"></div>
12 </div>
13 </div>
14
15 <div class="hgroup">
16 <div class="hgroup-inline">
17 <div class="panel">
18 <p>
19 A Jsonnet evaluation typically outputs a JSON document. For ways to output multiple JSON
20 documents, see <a href="/learning/getting_started.html">Getting Started</a>. Whether this
21 integrates cleanly depends on your application. If you're writing a custom application,
22 then you can simply consume this JSON. Another option is to write your config as a protocol
23 buffer and deserialize Jsonnet's JSON output using the proto3 canonical JSON representation.
24 Interoperation with YAML is also easy, see <a
25 href="/articles/kubernetes.html">Kubernetes</a> for an example. To support consumers of
26 syntactically incompatible formats such as INI or XML, custom serialization is needed.
27 </p>
28 </div>
29 <div style="clear: both"></div>
30 </div>
31 </div>
32
33 <div class="hgroup">
34 <div class="hgroup-inline">
35 <div class="panel">
36 <h2 id="top">Custom Output Formats</h2>
37 </div>
38 <div style="clear: both"></div>
39 </div>
40 </div>
41
42 <div class="hgroup">
43 <div class="hgroup-inline">
44 <div class="panel">
45 <p>
46 The INI format provides a simple example.
47 </p>
48 <ul>
49 <li>
50 The content of the INI file has been represented within the JSON object model, i.e. using
51 objects to represent the nesting of the sections.
52 </li>
53 <li>
54 The special <code>main</code> section holds the top-level keys (ones not in any section).
55 </li>
56 <li>
57 The function <code>std.manifestIni</code> (written in Jsonnet) serializes the structure to
58 a string.
59 </li>
60 <li>
61 Jsonnet is run in string output mode <tt>-S</tt> to print the string verbatim rather than
62 encoded as JSON.
63 </li>
64 </ul>
65 <p>
66 The code below could be executed with the command:
67 </p>
68 <pre>$ jsonnet -S ini.jsonnet</pre>
69 <p>
70 Try replacing <code>manifestIni</code>. with <code>manifestJson</code> or
71 <code>manifestPython</code>. There are many such functions in the standard library, and you
72 can write custom ones too. Finally, to see the effect of <tt>-S</tt> directly, try
73 replacing the whole buffer with just a string literal.
74 </p>
75 </div>
76 <div style="clear: both"></div>
77 </div>
78
79 </div>
80
81 <div class="inverse hgroup">
82 <div class=hgroup-inline>
83 <div class="tab-window-input" id="ini-input">
84 <div class="tab-header">
85 </div>
86 <textarea id=ini-jsonnet>
87 std.manifestIni({
88 main: {
89 a: 1,
90 },
91 sections: {
92 foo: {
93 a: true,
94 b: 2,
95 },
96 bar: {
97 x: 'text',
98 },
99 },
100 })
101 </textarea>
102 </div>
103 <div class="bigarrow">➡</div>
104 <div class="tab-window-output" id="ini-output">
105 <div class="tab-header">
106 <div class=selected onclick="tab_output_click(this, 'output-ini')">
107 output.ini
108 </div>
109 </div>
110 <textarea readonly class="selected code-json" id="output-ini">
111 a = 1
112 [bar]
113 x = text
114 [foo]
115 a = 1
116 b = 2
117 </textarea>
118 </div>
119 <script>
120 demo(
121 'ini-input',
122 {
123 'ini-jsonnet': 'ini.jsonnet',
124 },
125 'ini.jsonnet',
126 'ini-output',
127 false,
128 true
129 );
130 </script>
131 <div style="clear: both"></div>
132 </div>
133 </div>
134
135 <div class="hgroup">
136 <div class="hgroup-inline">
137 <div class="panel">
138 <p>
139 Thus, despite internally using the JSON data model, Jsonnet can provide config generation
140 for applications that consume INI.
141 </p>
142 <p>
143 Below is a more complex example where we construct an SVG file by emitting XML. It shows
144 how to write a custom manifestation function, and also a non-trivial example of representing
145 something in Jsonnet.
146 </p>
147 </div>
148 <div style="clear: both"></div>
149 </div>
150 </div>
151 <div class="inverse hgroup">
152 <div class=hgroup-inline>
153 <div class="tab-window-input" id="svg-input">
154 <div class="tab-header">
155 </div>
156 <textarea id=svg-jsonnet>
157 local xml = import 'xml.libsonnet';
158
159 local Svg = xml.Element('svg') {
160 height:: 100,
161 width:: 100,
162 };
163
164 local Polygon = xml.Element('polygon') {
165 points: std.join(
166 ' ',
167 [
168 '%s,%s' % coords
169 for coords in self.pointCoords
170 ]
171 ),
172 };
173
174 local Path = xml.Element('path') {
175 cmds:: [],
176 d: std.join('', self.cmds),
177 };
178
179 local ref(element) = 'url(#%s)' % element.id;
180
181 local logo = Svg {
182 version: '1.1',
183 xmlns: 'http://www.w3.org/2000/svg',
184 x: 0,
185 y: 0,
186 width: 90,
187 height: 90,
188 viewBox: '0 0 180 180',
189 bg:: xml.Element('linearGradient') {
190 id: 'bg',
191 gradientUnits: 'userSpaceOnUse',
192 x1: 53,
193 y1: 19,
194 x2: 126,
195 y2: 162,
196 colour1:: '#0091AD',
197 colour2:: '#00728F',
198 local grad = self,
199 has: [
200 xml.Element('stop') {
201 offset: 0,
202 'stop-color': grad.colour1,
203 },
204 xml.Element('stop') {
205 offset: 1,
206 'stop-color': grad.colour2,
207 },
208 ],
209 },
210 line:: Polygon {
211 fill: 'white',
212 pointCoords:: [
213 [0, 38],
214 [90, -14],
215 [180, 38],
216 [180, 142],
217 [90, 194],
218 [0, 142]
219 ],
220 },
221 fill:: Path {
222 fill: ref($.bg),
223 cmds: [
224 'M168,49 v86 L129,82 L168,49 z',
225 'M90,98 l67-59 L90,0 V98 z',
226 'M73,10 L12,45 v90 l61,35 V10 z',
227 'M117,94 L90,116 v63 l63-36 L117,94 z',
228 ],
229 },
230 has: [
231 $.bg,
232 $.line,
233 $.fill,
234 ],
235 };
236
237 xml.manifestXmlObj(logo)
238 </textarea>
239 <textarea id=xml-libsonnet>
240 // Some utility functions for generating XML.
241 {
242 // Serialize a XML document represented as a
243 // Jsonnet value. Each node of the XML tree
244 // has a field 'tag', and optionally 'attrs'
245 // (an string mapping), and 'has', an array
246 // of children.
247 manifestXmlObj(value)::
248 local aux(v, cindent) =
249 if !std.isObject(v) then
250 error 'Expected a object, got %s'
251 % std.type(value)
252 else
253 local attrs = [
254 ' %s="%s"' % [k, v.attrs[k]]
255 for k in std.objectFields(v.attrs)
256 ];
257 if std.length(v.has) &gt; 0 then
258 std.deepJoin([
259 cindent, '&lt;', v.tag, attrs, '&gt;\n',
260 [
261 aux(x, cindent + ' ')
262 for x in v.has
263 ],
264 cindent, '&lt;/', v.tag, '&gt;\n'
265 ])
266 else
267 std.deepJoin([
268 cindent, '&lt;', v.tag, attrs, '&gt;',
269 '&lt;/', v.tag, '&gt;\n']);
270 aux(value, ''),
271
272 // A convenience object whose attributes are
273 // taken from the top-level non-hidden fields
274 // of the object. This works except for
275 // attributes called 'tag' or 'has'.
276 Element(tag):: {
277 local element = self,
278 tag:: tag,
279 attrs:: {
280 [k]: element[k]
281 for k in std.objectFields(element)
282 },
283 has:: [],
284 },
285 }
286 </textarea>
287 </div>
288 <div class="bigarrow">➡</div>
289 <div class="tab-window-output" id="svg-output">
290 <div class="tab-header">
291 <div class=selected onclick="tab_output_click(this, 'output-svg')">
292 output.svg
293 </div>
294 </div>
295 <textarea readonly class="selected code-json" id="output-svg">
296 <svg version="1.1" viewBox="0 0 180 180" x="0" xmlns="http://www.w3.org/2000/svg" y="0">
297 <linearGradient gradientUnits="userSpaceOnUse" id="bg" x1="53" x2="126" y1="19" y2="162">
298 <stop offset="0" stop-color="#0091AD"></stop>
299 <stop offset="1" stop-color="#00728F"></stop>
300 </linearGradient>
301 <polygon fill="white" points="0,38 90,-14 180,38 180,142 90,194 0,142"></polygon>
302 <path d="M168,49 v86 L129,82 L168,49 zM90,98 l67-59 L90,0 V98 zM73,10 L12,45 v90 l61,35 V10 zM117,94 L90,116 v63 l63-36 L117,94 z" fill="url(#bg)"></path>
303 </svg>
304 </textarea>
305 </div>
306 <script>
307 demo(
308 'svg-input',
309 {
310 'svg-jsonnet': 'svg.jsonnet',
311 'xml-libsonnet': 'xml.libsonnet',
312 },
313 'svg.jsonnet',
314 'svg-output',
315 false,
316 true
317 );
318 </script>
319 <div style="clear: both"></div>
320 </div>
321 </div>
322
323 <div class="hgroup">
324 <div class="hgroup-inline">
325 <div class="panel">
326 <p>
327 This approach generalizes widely because JSON is very versatile. Indeed, its success as an
328 interchange format can be attributed to that versatility. Using the technique described
329 above, Jsonnet can output any format that can be represented in JSON.
330 </p>
331 </div>
332 <div style="clear: both"></div>
333 </div>
334 </div>