| <html devsite> | |
| <head> | |
| <meta name="project_path" value="/_project.yaml"> | |
| <meta name="book_path" value="/versions/6.3.0/_book.yaml"> | |
| </head> | |
| <body> | |
| <h1 class="page-title" id="modules.json">json</h1> | |
| <!-- {% raw %} --> | |
| Module json is a Starlark module of JSON-related functions. | |
| <h2>Members</h2> | |
| <ul> | |
| <li> | |
| <a href="#decode">decode</a> | |
| </li> | |
| <li> | |
| <a href="#encode">encode</a> | |
| </li> | |
| <li> | |
| <a href="#encode_indent">encode_indent</a> | |
| </li> | |
| <li> | |
| <a href="#indent">indent</a> | |
| </li> | |
| </ul> | |
| <h2 id="decode">decode</h2> | |
| <p><pre class="rule-signature">unknown json.decode(x)</pre></p> | |
| The decode function accepts one positional parameter, a JSON string. | |
| It returns the Starlark value that the string denotes. | |
| <ul><li>'null', 'true', and 'false' are parsed as None, True, and False. | |
| <li>Numbers are parsed as int, or as a float if they contain a decimal point or an exponent. Although JSON has no syntax for non-finite values, very large values may be decoded as infinity. | |
| <li>a JSON object is parsed as a new unfrozen Starlark dict. If the same key string occurs more than once in the object, the last value for the key is kept. | |
| <li>a JSON array is parsed as new unfrozen Starlark list. | |
| </ul> | |
| Decoding fails if x is not a valid JSON encoding. | |
| <!-- hide-from-toc is a class used by DevSite for the public Bazel site | |
| (https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) --> | |
| <h3 class="hide-from-toc">Parameters</h3> | |
| <table class="table table-bordered table-condensed table-params"> | |
| <colgroup> | |
| <col class="col-param"> | |
| <col class="param-description"> | |
| </colgroup> | |
| <thead> | |
| <tr> | |
| <th>Parameter</th> | |
| <th>Description</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr> | |
| <td id="decode.x"> | |
| <code>x</code> | |
| </td> | |
| <td> | |
| required<br/> | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <h2 id="encode">encode</h2> | |
| <p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> json.encode(x)</pre></p> | |
| <p>The encode function accepts one required positional argument, which it converts to JSON by cases: | |
| <ul> | |
| <li>None, True, and False are converted to 'null', 'true', and 'false', respectively. | |
| <li>An int, no matter how large, is encoded as a decimal integer. Some decoders may not be able to decode very large integers. | |
| <li>A float is encoded using a decimal point or an exponent or both, even if its numeric value is an integer. It is an error to encode a non-finite floating-point value. | |
| <li>A string value is encoded as a JSON string literal that denotes the value. Each unpaired surrogate is replaced by U+FFFD. | |
| <li>A dict is encoded as a JSON object, in key order. It is an error if any key is not a string. | |
| <li>A list or tuple is encoded as a JSON array. | |
| <li>A struct-like value is encoded as a JSON object, in field name order. | |
| </ul> | |
| An application-defined type may define its own JSON encoding. | |
| Encoding any other value yields an error. | |
| <!-- hide-from-toc is a class used by DevSite for the public Bazel site | |
| (https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) --> | |
| <h3 class="hide-from-toc">Parameters</h3> | |
| <table class="table table-bordered table-condensed table-params"> | |
| <colgroup> | |
| <col class="col-param"> | |
| <col class="param-description"> | |
| </colgroup> | |
| <thead> | |
| <tr> | |
| <th>Parameter</th> | |
| <th>Description</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr> | |
| <td id="encode.x"> | |
| <code>x</code> | |
| </td> | |
| <td> | |
| required<br/> | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <h2 id="encode_indent">encode_indent</h2> | |
| <p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> json.encode_indent(x, *, prefix='', indent='\t')</pre></p> | |
| The encode_indent function is equivalent to <code>json.indent(json.encode(x), ...)</code>. See <code>indent</code> for description of formatting parameters. | |
| <!-- hide-from-toc is a class used by DevSite for the public Bazel site | |
| (https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) --> | |
| <h3 class="hide-from-toc">Parameters</h3> | |
| <table class="table table-bordered table-condensed table-params"> | |
| <colgroup> | |
| <col class="col-param"> | |
| <col class="param-description"> | |
| </colgroup> | |
| <thead> | |
| <tr> | |
| <th>Parameter</th> | |
| <th>Description</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr> | |
| <td id="encode_indent.x"> | |
| <code>x</code> | |
| </td> | |
| <td> | |
| required<br/> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td id="encode_indent.prefix"> | |
| <code>prefix</code> | |
| </td> | |
| <td> | |
| default = ''<br/> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td id="encode_indent.indent"> | |
| <code>indent</code> | |
| </td> | |
| <td> | |
| default = '\t'<br/> | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <h2 id="indent">indent</h2> | |
| <p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> json.indent(s, *, prefix='', indent='\t')</pre></p> | |
| The indent function returns the indented form of a valid JSON-encoded string. | |
| Each array element or object field appears on a new line, beginning with the prefix string followed by one or more copies of the indent string, according to its nesting depth. | |
| The function accepts one required positional parameter, the JSON string, | |
| and two optional keyword-only string parameters, prefix and indent, | |
| that specify a prefix of each new line, and the unit of indentation. | |
| If the input is not valid, the function may fail or return invalid output. | |
| <!-- hide-from-toc is a class used by DevSite for the public Bazel site | |
| (https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) --> | |
| <h3 class="hide-from-toc">Parameters</h3> | |
| <table class="table table-bordered table-condensed table-params"> | |
| <colgroup> | |
| <col class="col-param"> | |
| <col class="param-description"> | |
| </colgroup> | |
| <thead> | |
| <tr> | |
| <th>Parameter</th> | |
| <th>Description</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr> | |
| <td id="indent.s"> | |
| <code>s</code> | |
| </td> | |
| <td> | |
| required<br/> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td id="indent.prefix"> | |
| <code>prefix</code> | |
| </td> | |
| <td> | |
| default = ''<br/> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td id="indent.indent"> | |
| <code>indent</code> | |
| </td> | |
| <td> | |
| default = '\t'<br/> | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| </body> | |
| </html> | |
| <!-- {% endraw %} --> |