ssw/doc/json5/index.html
2023-01-09 19:23:20 +01:00

157 lines
17 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="JSON5 is a superset of JSON with an expanded syntax including some productions from ECMAScript 5.1."><meta name="keywords" content="rust, rustlang, rust-lang, json5"><title>json5 - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../json5/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../json5/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate json5</a></h2><div class="sidebar-elems"><div class="block"><ul><li class="version">Version 0.4.1</li><li><a id="all-types" href="all.html">All Items</a></li></ul></div><section><div class="block"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#functions">Functions</a></li><li><a href="#types">Type Definitions</a></li></ul></div></section></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../json5/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">json5</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../src/json5/lib.rs.html#1-190">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>JSON5 is a superset of <a href="https://tools.ietf.org/html/rfc7159">JSON</a> with an expanded syntax including some productions from
<a href="https://www.ecma-international.org/ecma-262/5.1/">ECMAScript 5.1</a>.</p>
<p>In particular, JSON5 allows comments, trailing commas, object keys without quotes, single
quoted strings and more. See the <a href="https://json5.org/">JSON5 project page</a> for full details.</p>
<div class="example-wrap"><pre class="language-json5,ignore"><code>{
// comments
unquoted: &#39;and you can quote me on that&#39;,
singleQuotes: &#39;I can use &quot;double quotes&quot; here&#39;,
lineBreaks: &quot;Look, Mom! \
No \\n&#39;s!&quot;,
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: &#39;in objects&#39;, andIn: [&#39;arrays&#39;,],
&quot;backwardsCompatible&quot;: &quot;with JSON&quot;,
}</code></pre></div>
<p>This crate provides functions for deserializing JSON5 text into a Rust datatype and for
serializing a Rust datatype as JSON5 text, both via the <a href="https://serde.rs/">Serde framework</a>.</p>
<h2 id="deserialization"><a href="#deserialization">Deserialization</a></h2>
<p>Implementing Serdes <a href="https://docs.serde.rs/serde/de/trait.Deserialize.html"><code>Deserialize</code></a> trait on your type will allow you to parse JSON5
text into a value of that type with <a href="fn.from_str.html"><code>from_str</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">serde_derive::Deserialize</span>;
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Deserialize</span>, <span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span>
<span class="kw">struct</span> <span class="ident">Config</span> {
<span class="ident">message</span>: <span class="ident">String</span>,
<span class="ident">n</span>: <span class="ident">i32</span>,
}
<span class="kw">let</span> <span class="ident">config</span> <span class="op">=</span> <span class="string">&quot;
{
// A traditional message.
message: &#39;hello world&#39;,
// A number for some reason.
n: 42,
}
&quot;</span>;
<span class="macro">assert_eq!</span>(
<span class="ident">json5::from_str</span>(<span class="ident">config</span>),
<span class="prelude-val">Ok</span>(<span class="ident">Config</span> {
<span class="ident">message</span>: <span class="string">&quot;hello world&quot;</span>.<span class="ident">to_string</span>(),
<span class="ident">n</span>: <span class="number">42</span>,
}),
);</code></pre></div>
<p>Also, you could deserialize into serde_json::Value</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">json5</span>;
<span class="kw">use</span> <span class="ident">serde_json</span>::{<span class="ident">Value</span>, <span class="ident">json</span>};
<span class="kw">let</span> <span class="ident">config</span> <span class="op">=</span> <span class="string">&quot;
{
// A traditional message.
message: &#39;hello world&#39;,
// A number for some reason.
n: 42,
}
&quot;</span>;
<span class="macro">assert_eq!</span>(
<span class="ident">json5::from_str</span>::<span class="op">&lt;</span><span class="ident">Value</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="ident">config</span>),
<span class="prelude-val">Ok</span>(<span class="macro">json!</span>({
<span class="string">&quot;message&quot;</span>: <span class="string">&quot;hello world&quot;</span>,
<span class="string">&quot;n&quot;</span>: <span class="number">42</span>
}))
);</code></pre></div>
<p>There are many ways to customize the deserialization (e.g. deserializing <code>camelCase</code> field
names into a struct with <code>snake_case</code> fields). See the Serde docs, especially the
<a href="https://serde.rs/attributes.html">Attributes</a>, <a href="https://serde.rs/custom-serialization.html">Custom serialization</a> and <a href="https://serde.rs/examples.html">Examples</a> sections.</p>
<h2 id="serialization"><a href="#serialization">Serialization</a></h2>
<p>Similarly, implementing <a href="https://docs.serde.rs/serde/ser/trait.Serialize.html"><code>Serialize</code></a> on a Rust type allows you to produce a JSON5
serialization of values of that type with <a href="fn.to_string.html"><code>to_string</code></a>. At present the serializer will just
produce JSON (since its a valid subset of JSON5), but future work will allow specifying the
output style (single over double quotes, trailing commas, indentation etc.).</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">serde_derive::Serialize</span>;
<span class="kw">use</span> <span class="ident">std::collections::HashMap</span>;
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Serialize</span>, <span class="ident">Debug</span>)]</span>
<span class="attribute">#[<span class="ident">serde</span>(<span class="ident">untagged</span>)]</span>
<span class="kw">enum</span> <span class="ident">Val</span> {
<span class="ident">Null</span>,
<span class="ident">Bool</span>(<span class="ident">bool</span>),
<span class="ident">Number</span>(<span class="ident">f64</span>),
<span class="ident">String</span>(<span class="ident">String</span>),
<span class="ident">Array</span>(<span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">Val</span><span class="op">&gt;</span>),
<span class="ident">Object</span>(<span class="ident">HashMap</span><span class="op">&lt;</span><span class="ident">String</span>, <span class="ident">Val</span><span class="op">&gt;</span>),
}
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">map</span> <span class="op">=</span> <span class="ident">HashMap::new</span>();
<span class="ident">map</span>.<span class="ident">insert</span>(
<span class="string">&quot;a&quot;</span>.<span class="ident">to_owned</span>(),
<span class="ident">Val::Array</span>(<span class="macro">vec!</span>[
<span class="ident">Val::Null</span>,
<span class="ident">Val::Bool</span>(<span class="bool-val">true</span>),
<span class="ident">Val::Number</span>(<span class="number">42.</span>),
<span class="ident">Val::Number</span>(<span class="number">42.42</span>),
<span class="ident">Val::Number</span>(<span class="ident">f64::NAN</span>),
<span class="ident">Val::String</span>(<span class="string">&quot;hello&quot;</span>.<span class="ident">to_owned</span>()),
])
);
<span class="macro">assert_eq!</span>(
<span class="ident">json5::to_string</span>(<span class="kw-2">&amp;</span><span class="ident">Val::Object</span>(<span class="ident">map</span>)),
<span class="prelude-val">Ok</span>(<span class="string">&quot;{\&quot;a\&quot;:[null,true,42,42.42,NaN,\&quot;hello\&quot;]}&quot;</span>.<span class="ident">to_owned</span>()),
)</code></pre></div>
<p>You could also build from serde_json</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">serde_json</span>::{<span class="ident">json</span>, <span class="ident">Value</span>, <span class="ident">Map</span>, <span class="ident">Number</span>};
<span class="macro">assert_eq!</span>(
<span class="ident">json5::to_string</span>(
<span class="kw-2">&amp;</span><span class="macro">json!</span>({<span class="string">&quot;a&quot;</span>: [<span class="ident">null</span>, <span class="bool-val">true</span>, <span class="number">42</span>, <span class="number">42.42</span>, <span class="ident">f64::NAN</span>, <span class="string">&quot;hello&quot;</span>]})
),
<span class="prelude-val">Ok</span>(<span class="string">&quot;{\&quot;a\&quot;:[null,true,42,42.42,null,\&quot;hello\&quot;]}&quot;</span>.<span class="ident">to_owned</span>())
);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">map</span> <span class="op">=</span> <span class="ident">Map::new</span>();
<span class="ident">map</span>.<span class="ident">insert</span>(
<span class="string">&quot;a&quot;</span>.<span class="ident">to_owned</span>(),
<span class="ident">Value::Array</span>(<span class="macro">vec!</span>[
<span class="ident">Value::Null</span>,
<span class="ident">Value::Bool</span>(<span class="bool-val">true</span>),
<span class="ident">Value::Number</span>(<span class="ident">Number::from_f64</span>(<span class="number">42.</span>).<span class="ident">unwrap</span>()),
<span class="ident">Value::Number</span>(<span class="ident">Number::from_f64</span>(<span class="number">42.42</span>).<span class="ident">unwrap</span>()),
<span class="ident">Value::String</span>(<span class="string">&quot;hello&quot;</span>.<span class="ident">to_owned</span>()),
])
);
<span class="macro">assert_eq!</span>(
<span class="ident">json5::to_string</span>(<span class="kw-2">&amp;</span><span class="ident">Value::Object</span>(<span class="ident">map</span>)),
<span class="prelude-val">Ok</span>(<span class="string">&quot;{\&quot;a\&quot;:[null,true,42,42.42,\&quot;hello\&quot;]}&quot;</span>.<span class="ident">to_owned</span>()),
)</code></pre></div>
<p>There are many ways to customize the serialization (e.g. serializing <code>snake_case</code> struct fields
as <code>camelCase</code>). See the Serde docs, especially the <a href="https://serde.rs/attributes.html">Attributes</a>, <a href="https://serde.rs/custom-serialization.html">Custom serialization</a>
and <a href="https://serde.rs/examples.html">Examples</a> sections.</p>
<h2 id="limitations"><a href="#limitations">Limitations</a></h2>
<p>At the time of writing the following is unsupported:</p>
<ul>
<li>
<p>deserializing into borrowed types (e.g. fields of type <code>&amp;str</code>)</p>
</li>
<li>
<p>serializing or deserializing <a href="https://serde.rs/data-model.html#types">byte arrays</a></p>
</li>
<li>
<p>specifying the style of JSON5 output from the serializer (single over double quotes, trailing
commas, indentation etc.)</p>
</li>
</ul>
</div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Deserializer.html" title="json5::Deserializer struct">Deserializer</a></div><div class="item-right docblock-short"></div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Location.html" title="json5::Location struct">Location</a></div><div class="item-right docblock-short"><p>One-based line and column at which the error was detected.</p>
</div></div></div><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Error.html" title="json5::Error enum">Error</a></div><div class="item-right docblock-short"><p>A bare bones error type which currently just collapses all the underlying errors in to a single
string… This is fine for displaying to the user, but not very useful otherwise. Work to be
done here.</p>
</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.from_str.html" title="json5::from_str fn">from_str</a></div><div class="item-right docblock-short"><p>Deserialize an instance of type <code>T</code> from a string of JSON5 text. Can fail if the input is
invalid JSON5, or doesnt match the structure of the target type.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.to_string.html" title="json5::to_string fn">to_string</a></div><div class="item-right docblock-short"><p>Attempts to serialize the input as a JSON5 string (actually a JSON string).</p>
</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.Result.html" title="json5::Result type">Result</a></div><div class="item-right docblock-short"><p>Alias for a <code>Result</code> with error type <code>json5::Error</code></p>
</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="json5" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>