| <html devsite> |
| <head> |
| <meta name="project_path" value="/_project.yaml"> |
| <meta name="book_path" value="/versions/7.2.0/_book.yaml"> |
| </head> |
| <body> |
| |
| <h1 class="page-title" id="modules.json">json</h1> |
| |
| {% dynamic setvar source_file "src/main/java/net/starlark/java/lib/json/Json.java" %} |
| {% dynamic setvar version "7.2.0" %} |
| {% dynamic setvar original_path "/rules/lib/core/json" %} |
| {% include "_buttons.html" %} |
| <!-- {% 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, default=unbound)</pre></p> |
| |
| The decode function has one required positional parameter: a JSON string. |
| It returns the Starlark value that the string denotes. |
| <ul><li><code>"null"</code>, <code>"true"</code> and <code>"false"</code> are parsed as <code>None</code>, <code>True</code>, and <code>False</code>. |
| <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> |
| If <code>x</code> is not a valid JSON encoding and the optional <code>default</code> parameter is specified (including specified as <code>None</code>), this function returns the <code>default</code> value. |
| If <code>x</code> is not a valid JSON encoding and the optional <code>default</code> parameter is <em>not</em> specified, this function fails. |
| |
| <!-- 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/> |
| JSON string to decode. |
| </td> |
| </tr> |
| <tr> |
| <td id="decode.default"> |
| <code>default</code> |
| </td> |
| <td> |
| default is <code>unbound</code><br/> |
| If specified, the value to return when <code>x</code> cannot be decoded. |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| |
| <h2 id="encode">encode</h2> |
| <p><pre class="rule-signature"><a class="anchor" href="../core/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="../core/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 is <code>''</code><br/> |
| |
| </td> |
| </tr> |
| <tr> |
| <td id="encode_indent.indent"> |
| <code>indent</code> |
| </td> |
| <td> |
| default is <code>'\t'</code><br/> |
| |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| |
| <h2 id="indent">indent</h2> |
| <p><pre class="rule-signature"><a class="anchor" href="../core/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 is <code>''</code><br/> |
| |
| </td> |
| </tr> |
| <tr> |
| <td id="indent.indent"> |
| <code>indent</code> |
| </td> |
| <td> |
| default is <code>'\t'</code><br/> |
| |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| |
| |
| </body> |
| </html> |
| |
| <!-- {% endraw %} --> |