| 1 | --- |
| 2 | layout: default |
| 3 | title: Bindings |
| 4 | --- |
| 5 | |
| 6 | <div class="hgroup"> |
| 7 | <div class="hgroup-inline"> |
| 8 | <div class="panel"> |
| 9 | <h1 id="bindings">Bindings</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 | If you don't want to invoke the <tt>jsonnet</tt> commandline utility to evaluate Jsonnet, |
| 20 | there are also official libraries for Python, Go, and C. The C library wraps the C++ |
| 21 | implementation, hiding all the C++ to make it easy to use Jsonnet from C. In turn, there is |
| 22 | a C++ wrapper around the C library to make it easy to use from client C++ code (surprisingly |
| 23 | enough, this actually makes sense)! There are also various unofficial bindings built on the |
| 24 | C API. |
| 25 | </p> |
| 26 | </div> |
| 27 | <div style="clear: both"></div> |
| 28 | </div> |
| 29 | </div> |
| 30 | |
| 31 | |
| 32 | <div class="hgroup"> |
| 33 | <div class="hgroup-inline"> |
| 34 | <div class="panel"> |
| 35 | <h2 id="server_side">A Warning On Server-Side Evaluation</h2> |
| 36 | </div> |
| 37 | <div style="clear: both"></div> |
| 38 | </div> |
| 39 | </div> |
| 40 | |
| 41 | |
| 42 | <div class="hgroup"> |
| 43 | <div class="hgroup-inline"> |
| 44 | <div class="panel"> |
| 45 | <p> |
| 46 | If you are creating an API server that will evaluate untrusted Jsonnet code in the same |
| 47 | process that handles other requests, be aware of your exposure to denial of service attacks. |
| 48 | Since Jsonnet allows expressing programs that take unreasonable amounts of CPU time and RAM, |
| 49 | it is possible for a malicious request to cause its host API server to exceed its OS |
| 50 | resources and crash. Such a crash would affect concurrently running handlers for other |
| 51 | users, and decrease your capacity until the server is restarted. A simple remedy for this |
| 52 | situation is to <tt>exec</tt> the Jsonnet commandline binary in a <tt>ulimit</tt> child |
| 53 | process where the CPU and RAM can be tightly bounded for the specific request. |
| 54 | Communication to and from the commandline binary could be via temporary files or pipes. |
| 55 | Thus, only the malicious request fails. |
| 56 | </p> |
| 57 | <p> |
| 58 | Linking Jsonnet as a library is more appropriate for client-side applications or situations |
| 59 | where the input configuration can be trusted. |
| 60 | </p> |
| 61 | </div> |
| 62 | <div style="clear: both"></div> |
| 63 | </div> |
| 64 | </div> |
| 65 | |
| 66 | |
| 67 | <div class="hgroup"> |
| 68 | <div class="hgroup-inline"> |
| 69 | <div class="panel"> |
| 70 | <h2 id="c_api">C API</h2> |
| 71 | </div> |
| 72 | <div style="clear: both"></div> |
| 73 | </div> |
| 74 | </div> |
| 75 | |
| 76 | |
| 77 | <div class="hgroup"> |
| 78 | <div class="hgroup-inline"> |
| 79 | <div class="panel"> |
| 80 | <p> |
| 81 | The C API simply wraps the C++ implementation to hide C++ language features like templates, |
| 82 | classes, overloading, etc. This makes it easier to bind to other languages, as C is |
| 83 | typically the lowest common denominator across all systems. |
| 84 | </p> |
| 85 | |
| 86 | <p> |
| 87 | The API is documented in <a |
| 88 | href="https://github.com/google/jsonnet/blob/master/include/libjsonnet.h">libjsonnet.h</a>. |
| 89 | It is built with 'make libjsonnet.so'. It is used by the python bindings, the Jsonnet |
| 90 | commandline tool, as well as a couple of simpler tests. Search for #include "libjsonnet.h". |
| 91 | </p> |
| 92 | |
| 93 | <p> |
| 94 | To use the API, create a JsonnetVM object, set various options, then tell it to evaluate a |
| 95 | filename or snippet. To avoid leaking memory, the result of execution (JSON or error |
| 96 | message) and the JsonnetVM object itself must be cleaned up using the provided |
| 97 | <tt>jsonnet_realloc</tt> and the <tt>jsonnet_destroy</tt> functions, respectively. |
| 98 | </p> |
| 99 | <div style="clear: both"></div> |
| 100 | </div> |
| 101 | </div> |
| 102 | |
| 103 | <div class="hgroup"> |
| 104 | <div class="hgroup-inline"> |
| 105 | <div class="panel"> |
| 106 | <h2 id="python_api">Python API</h2> |
| 107 | </div> |
| 108 | <div style="clear: both"></div> |
| 109 | </div> |
| 110 | </div> |
| 111 | |
| 112 | |
| 113 | <div class="hgroup"> |
| 114 | <div class="hgroup-inline"> |
| 115 | <div class="panel"> |
| 116 | <p> |
| 117 | The Python API wraps the C API in a straightforward way. It can be installed with <tt>pip |
| 118 | install jsonnet</tt> or built directly with <tt>setup.py</tt>. It supports Python 2.7 and |
| 119 | Python 3. |
| 120 | </p> |
| 121 | <p> |
| 122 | The Python module provides two functions, <tt>evaluate_file(filename)</tt> and |
| 123 | <tt>evaluate_snippet(filename, expr)</tt>. In the latter case, the parameter |
| 124 | <tt>filename</tt> is used in stack traces, because all errors are given with the "filename" |
| 125 | containing the code. |
| 126 | </p> |
| 127 | <p> |
| 128 | Keyword arguments to these functions are used to control the virtual machine. They are: |
| 129 | </p> |
| 130 | <ul> |
| 131 | <li><tt>max_stack</tt> (number)</li> |
| 132 | <li><tt>gc_min_objects</tt> (number)</li> |
| 133 | <li><tt>gc_growth_trigger</tt> (number)</li> |
| 134 | <li><tt>ext_vars</tt> (dict: string to string)</li> |
| 135 | <li><tt>ext_codes</tt> (dict string to string)</li> |
| 136 | <li><tt>tla_vars</tt> (dict string to string)</li> |
| 137 | <li><tt>tla_codes</tt> (dict string to string)</li> |
| 138 | <li><tt>max_trace</tt> (number)</li> |
| 139 | <li><tt>import_callback</tt> (see example in python/)</li> |
| 140 | <li><tt>native_callbacks</tt> (see example in python/)</li> |
| 141 | </ul> |
| 142 | <p> |
| 143 | The argument <tt>import_callback</tt> can be used to pass a callable, to trap the Jsonnet |
| 144 | <code>import</code>, <code>importstr</code>, and <code>importbin</code> constructs. This allows, e.g., reading files |
| 145 | out of archives or implementing library search paths. The argument <tt>native_callback</tt> |
| 146 | is used to allow execution of arbitrary Python code via <code>std.native(...)</code>. This |
| 147 | is useful so Jsonnet code can access pure functions in the Python ecosystem, such as |
| 148 | compression, encryption, encoding, etc. |
| 149 | </p> |
| 150 | <p> |
| 151 | If an error is raised during the evaluation of the Jsonnet code, it is formed into a stack |
| 152 | trace and thrown as a python RuntimeError. Otherwise, the JSON string is returned. To |
| 153 | convert this into objects for easy interpretation in Python, use the <a |
| 154 | href="https://docs.python.org/2/library/json.html">json</a> module. An example: |
| 155 | </p> |
| 156 | <pre>import json |
| 157 | import _jsonnet |
| 158 | |
| 159 | jsonnet_str = ''' |
| 160 | { |
| 161 | person1: { |
| 162 | name: "Alice", |
| 163 | welcome: "Hello " + self.name + "!", |
| 164 | }, |
| 165 | person2: self.person1 { |
| 166 | name: std.extVar("OTHER_NAME"), |
| 167 | }, |
| 168 | } |
| 169 | ''' |
| 170 | |
| 171 | json_str = _jsonnet.evaluate_snippet( |
| 172 | "snippet", jsonnet_str, |
| 173 | ext_vars={'OTHER_NAME': 'Bob'}) |
| 174 | |
| 175 | json_obj = json.loads(json_str) |
| 176 | for person_id, person in json_obj.items(): |
| 177 | print('%s is %s, greeted by "%s"' % ( |
| 178 | person_id, |
| 179 | person['name'], |
| 180 | person['welcome']))</pre> |
| 181 | <div style="clear: both"></div> |
| 182 | </div> |
| 183 | </div> |
| 184 | |
| 185 | |
| 186 | <div class="hgroup"> |
| 187 | <div class="hgroup-inline"> |
| 188 | <div class="panel"> |
| 189 | <h2 id="third_party_apis">Unofficial Third Party APIs</h2> |
| 190 | </div> |
| 191 | <div style="clear: both"></div> |
| 192 | </div> |
| 193 | </div> |
| 194 | |
| 195 | |
| 196 | <div class="hgroup"> |
| 197 | <div class="hgroup-inline"> |
| 198 | <div class="panel"> |
| 199 | <p> |
| 200 | There are unofficial bindings available for other languages. These are not supported by |
| 201 | Google and may be some versions behind the latest release. |
| 202 | </p> |
| 203 | |
| 204 | <ul> |
| 205 | <li> |
| 206 | <a href="https://github.com/strickyak/jsonnet_cgo">Go</a> but please first consider the |
| 207 | <a href="https://github.com/google/go-jsonnet">native port</a> |
| 208 | </li> |
| 209 | <li> |
| 210 | <a href="https://github.com/yuduanchen/luajit-jsonnet">LuaJIT (FFI)</a> |
| 211 | </li> |
| 212 | <li> |
| 213 | <a href="https://github.com/yosuke-furukawa/node-jsonnet">Node.js</a> |
| 214 | </li> |
| 215 | <li> |
| 216 | <a href="https://github.com/Neeke/Jsonnet-PHP">PHP</a> |
| 217 | </li> |
| 218 | <li> |
| 219 | <a href="https://github.com/yugui/ruby-jsonnet">Ruby</a> |
| 220 | </li> |
| 221 | <li> |
| 222 | <a href="https://github.com/anguslees/rust-jsonnet">Rust</a> but please first consider the |
| 223 | <a href="https://github.com/CertainLach/jrsonnet">native port</a> |
| 224 | </li> |
| 225 | <li> |
| 226 | <a href="https://github.com/moleike/haskell-jsonnet">Haskell native port</a> |
| 227 | </li> |
| 228 | </ul> |
| 229 | <div style="clear: both"></div> |
| 230 | </div> |
| 231 | </div> |