157 lines
17 KiB
HTML
157 lines
17 KiB
HTML
<!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">☰</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">−</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: 'and you can quote me on that',
|
||
singleQuotes: 'I can use "double quotes" here',
|
||
lineBreaks: "Look, Mom! \
|
||
No \\n's!",
|
||
hexadecimal: 0xdecaf,
|
||
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
|
||
positiveSign: +1,
|
||
trailingComma: 'in objects', andIn: ['arrays',],
|
||
"backwardsCompatible": "with JSON",
|
||
}</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 Serde’s <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">"
|
||
{
|
||
// A traditional message.
|
||
message: 'hello world',
|
||
|
||
// A number for some reason.
|
||
n: 42,
|
||
}
|
||
"</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">"hello world"</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">"
|
||
{
|
||
// A traditional message.
|
||
message: 'hello world',
|
||
|
||
// A number for some reason.
|
||
n: 42,
|
||
}
|
||
"</span>;
|
||
|
||
<span class="macro">assert_eq!</span>(
|
||
<span class="ident">json5::from_str</span>::<span class="op"><</span><span class="ident">Value</span><span class="op">></span>(<span class="kw-2">&</span><span class="ident">config</span>),
|
||
<span class="prelude-val">Ok</span>(<span class="macro">json!</span>({
|
||
<span class="string">"message"</span>: <span class="string">"hello world"</span>,
|
||
<span class="string">"n"</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 it’s 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"><</span><span class="ident">Val</span><span class="op">></span>),
|
||
<span class="ident">Object</span>(<span class="ident">HashMap</span><span class="op"><</span><span class="ident">String</span>, <span class="ident">Val</span><span class="op">></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">"a"</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">"hello"</span>.<span class="ident">to_owned</span>()),
|
||
])
|
||
);
|
||
<span class="macro">assert_eq!</span>(
|
||
<span class="ident">json5::to_string</span>(<span class="kw-2">&</span><span class="ident">Val::Object</span>(<span class="ident">map</span>)),
|
||
<span class="prelude-val">Ok</span>(<span class="string">"{\"a\":[null,true,42,42.42,NaN,\"hello\"]}"</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">&</span><span class="macro">json!</span>({<span class="string">"a"</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">"hello"</span>]})
|
||
),
|
||
<span class="prelude-val">Ok</span>(<span class="string">"{\"a\":[null,true,42,42.42,null,\"hello\"]}"</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">"a"</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">"hello"</span>.<span class="ident">to_owned</span>()),
|
||
])
|
||
);
|
||
<span class="macro">assert_eq!</span>(
|
||
<span class="ident">json5::to_string</span>(<span class="kw-2">&</span><span class="ident">Value::Object</span>(<span class="ident">map</span>)),
|
||
<span class="prelude-val">Ok</span>(<span class="string">"{\"a\":[null,true,42,42.42,\"hello\"]}"</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>&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 doesn’t 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> |