|
|
<!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="API documentation for the Rust `Serializer` trait in crate `serde`."><meta name="keywords" content="rust, rustlang, rust-lang, Serializer"><title>serde::ser::Serializer - Rust</title><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="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../serde/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><p class='location'>Trait Serializer</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#associated-types">Associated Types</a><div class="sidebar-links"><a href="#associatedtype.Ok">Ok</a><a href="#associatedtype.Error">Error</a><a href="#associatedtype.SerializeSeq">SerializeSeq</a><a href="#associatedtype.SerializeTuple">SerializeTuple</a><a href="#associatedtype.SerializeTupleStruct">SerializeTupleStruct</a><a href="#associatedtype.SerializeTupleVariant">SerializeTupleVariant</a><a href="#associatedtype.SerializeMap">SerializeMap</a><a href="#associatedtype.SerializeStruct">SerializeStruct</a><a href="#associatedtype.SerializeStructVariant">SerializeStructVariant</a></div><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.serialize_bool">serialize_bool</a><a href="#tymethod.serialize_bytes">serialize_bytes</a><a href="#tymethod.serialize_char">serialize_char</a><a href="#tymethod.serialize_f32">serialize_f32</a><a href="#tymethod.serialize_f64">serialize_f64</a><a href="#tymethod.serialize_i16">serialize_i16</a><a href="#tymethod.serialize_i32">serialize_i32</a><a href="#tymethod.serialize_i64">serialize_i64</a><a href="#tymethod.serialize_i8">serialize_i8</a><a href="#tymethod.serialize_map">serialize_map</a><a href="#tymethod.serialize_newtype_struct">serialize_newtype_struct</a><a href="#tymethod.serialize_newtype_variant">serialize_newtype_variant</a><a href="#tymethod.serialize_none">serialize_none</a><a href="#tymethod.serialize_seq">serialize_seq</a><a href="#tymethod.serialize_some">serialize_some</a><a href="#tymethod.serialize_str">serialize_str</a><a href="#tymethod.serialize_struct">serialize_struct</a><a href="#tymethod.serialize_struct_variant">serialize_struct_variant</a><a href="#tymethod.serialize_tuple">serialize_tuple</a><a href="#tymethod.serialize_tuple_struct">serialize_tuple_struct</a><a href="#tymethod.serialize_tuple_variant">serialize_tuple_variant</a><a href="#tymethod.serialize_u16">serialize_u16</a><a href="#tymethod.serialize_u32">serialize_u32</a><a href="#tymethod.serialize_u64">serialize_u64</a><a href="#tymethod.serialize_u8">serialize_u8</a><a href="#tymethod.serialize_unit">serialize_unit</a><a href="#tymethod.serialize_unit_struct">serialize_unit_struct</a><a href="#tymethod.serialize_unit_variant">serialize_unit_variant</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.collect_map">collect_map</a><a href="#method.collect_seq">collect_seq</a><a href="#method.collect_str">collect_str</a><a href="#method.is_human_readable">is_human_readable</a><a href="#method.serialize_i128">serialize_i128</a><a href="#method.serialize_u128">serialize_u128</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>serde</a>::<wbr><a href='index.html'>ser</a></p><script>window.sidebarCurrent = {name: 'Serializer', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/serde/ser/mod.rs.html#323-1445' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='../index.html'>serde</a>::<wbr><a href='index.html'>ser</a>::<wbr><a class="trait" href=''>Serializer</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'>pub trait Serializer: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
|
|
|
type <a href='#associatedtype.Ok' class="type">Ok</a>;
|
|
|
type <a href='#associatedtype.Error' class="type">Error</a>: <a class="trait" href="../../serde/ser/trait.Error.html" title="trait serde::ser::Error">Error</a>;
|
|
|
type <a href='#associatedtype.SerializeSeq' class="type">SerializeSeq</a>: <a class="trait" href="../../serde/ser/trait.SerializeSeq.html" title="trait serde::ser::SerializeSeq">SerializeSeq</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
type <a href='#associatedtype.SerializeTuple' class="type">SerializeTuple</a>: <a class="trait" href="../../serde/ser/trait.SerializeTuple.html" title="trait serde::ser::SerializeTuple">SerializeTuple</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
type <a href='#associatedtype.SerializeTupleStruct' class="type">SerializeTupleStruct</a>: <a class="trait" href="../../serde/ser/trait.SerializeTupleStruct.html" title="trait serde::ser::SerializeTupleStruct">SerializeTupleStruct</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
type <a href='#associatedtype.SerializeTupleVariant' class="type">SerializeTupleVariant</a>: <a class="trait" href="../../serde/ser/trait.SerializeTupleVariant.html" title="trait serde::ser::SerializeTupleVariant">SerializeTupleVariant</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
type <a href='#associatedtype.SerializeMap' class="type">SerializeMap</a>: <a class="trait" href="../../serde/ser/trait.SerializeMap.html" title="trait serde::ser::SerializeMap">SerializeMap</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
type <a href='#associatedtype.SerializeStruct' class="type">SerializeStruct</a>: <a class="trait" href="../../serde/ser/trait.SerializeStruct.html" title="trait serde::ser::SerializeStruct">SerializeStruct</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
type <a href='#associatedtype.SerializeStructVariant' class="type">SerializeStructVariant</a>: <a class="trait" href="../../serde/ser/trait.SerializeStructVariant.html" title="trait serde::ser::SerializeStructVariant">SerializeStructVariant</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
fn <a href='#tymethod.serialize_bool' class='fnname'>serialize_bool</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_i8' class='fnname'>serialize_i8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i8.html">i8</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_i16' class='fnname'>serialize_i16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i16.html">i16</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_i32' class='fnname'>serialize_i32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i32.html">i32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_i64' class='fnname'>serialize_i64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i64.html">i64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_u8' class='fnname'>serialize_u8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_u16' class='fnname'>serialize_u16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_u32' class='fnname'>serialize_u32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_u64' class='fnname'>serialize_u64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_f32' class='fnname'>serialize_f32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_f64' class='fnname'>serialize_f64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_char' class='fnname'>serialize_char</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.char.html">char</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_str' class='fnname'>serialize_str</a>(self, v: &<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_bytes' class='fnname'>serialize_bytes</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_none' class='fnname'>serialize_none</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_some' class='fnname'>serialize_some</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T<br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>><br> <span class="where">where<br> T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_unit' class='fnname'>serialize_unit</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_unit_struct' class='fnname'>serialize_unit_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_unit_variant' class='fnname'>serialize_unit_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_newtype_struct' class='fnname'>serialize_newtype_struct</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T<br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>><br> <span class="where">where<br> T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_newtype_variant' class='fnname'>serialize_newtype_variant</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T<br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>><br> <span class="where">where<br> T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_seq' class='fnname'>serialize_seq</a>(<br> self, <br> len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeSeq" title="type serde::Serializer::SerializeSeq">SerializeSeq</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_tuple' class='fnname'>serialize_tuple</a>(<br> self, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTuple" title="type serde::Serializer::SerializeTuple">SerializeTuple</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_tuple_struct' class='fnname'>serialize_tuple_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTupleStruct" title="type serde::Serializer::SerializeTupleStruct">SerializeTupleStruct</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_tuple_variant' class='fnname'>serialize_tuple_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTupleVariant" title="type serde::Serializer::SerializeTupleVariant">SerializeTupleVariant</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_map' class='fnname'>serialize_map</a>(<br> self, <br> len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeMap" title="type serde::Serializer::SerializeMap">SerializeMap</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_struct' class='fnname'>serialize_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeStruct" title="type serde::Serializer::SerializeStruct">SerializeStruct</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
<div class='item-spacer'></div> fn <a href='#tymethod.serialize_struct_variant' class='fnname'>serialize_struct_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeStructVariant" title="type serde::Serializer::SerializeStructVariant">SerializeStructVariant</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>>;
|
|
|
|
|
|
fn <a href='#method.serialize_i128' class='fnname'>serialize_i128</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i128.html">i128</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>> { ... }
|
|
|
<div class='item-spacer'></div> fn <a href='#method.serialize_u128' class='fnname'>serialize_u128</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u128.html">u128</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>> { ... }
|
|
|
<div class='item-spacer'></div> fn <a href='#method.collect_seq' class='fnname'>collect_seq</a><I>(self, iter: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>><br> <span class="where">where<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a>,<br> <I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a>>::<a class="type" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html#associatedtype.Item" title="type core::iter::traits::collect::IntoIterator::Item">Item</a>: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a></span>,
|
|
|
{ ... }
|
|
|
<div class='item-spacer'></div> fn <a href='#method.collect_map' class='fnname'>collect_map</a><K, V, I>(self, iter: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>><br> <span class="where">where<br> K: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br> V: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a><Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>K, V<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>></span>,
|
|
|
{ ... }
|
|
|
<div class='item-spacer'></div> fn <a href='#method.collect_str' class='fnname'>collect_str</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(self, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>><br> <span class="where">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a></span>,
|
|
|
{ ... }
|
|
|
<div class='item-spacer'></div> fn <a href='#method.is_human_readable' class='fnname'>is_human_readable</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> { ... }
|
|
|
}</pre></div><div class='docblock'><p>A <strong>data format</strong> that can serialize any data structure supported by Serde.</p>
|
|
|
<p>The role of this trait is to define the serialization half of the <a href="https://serde.rs/data-model.html">Serde
|
|
|
data model</a>, which is a way to categorize every Rust data structure into one
|
|
|
of 29 possible types. Each method of the <code>Serializer</code> trait corresponds to
|
|
|
one of the types of the data model.</p>
|
|
|
<p>Implementations of <code>Serialize</code> map themselves into this data model by
|
|
|
invoking exactly one of the <code>Serializer</code> methods.</p>
|
|
|
<p>The types that make up the Serde data model are:</p>
|
|
|
<ul>
|
|
|
<li><strong>14 primitive types</strong>
|
|
|
<ul>
|
|
|
<li>bool</li>
|
|
|
<li>i8, i16, i32, i64, i128</li>
|
|
|
<li>u8, u16, u32, u64, u128</li>
|
|
|
<li>f32, f64</li>
|
|
|
<li>char</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>string</strong>
|
|
|
<ul>
|
|
|
<li>UTF-8 bytes with a length and no null terminator.</li>
|
|
|
<li>When serializing, all strings are handled equally. When deserializing,
|
|
|
there are three flavors of strings: transient, owned, and borrowed.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>byte array</strong> - [u8]
|
|
|
<ul>
|
|
|
<li>Similar to strings, during deserialization byte arrays can be
|
|
|
transient, owned, or borrowed.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>option</strong>
|
|
|
<ul>
|
|
|
<li>Either none or some value.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>unit</strong>
|
|
|
<ul>
|
|
|
<li>The type of <code>()</code> in Rust. It represents an anonymous value containing
|
|
|
no data.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>unit_struct</strong>
|
|
|
<ul>
|
|
|
<li>For example <code>struct Unit</code> or <code>PhantomData<T></code>. It represents a named
|
|
|
value containing no data.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>unit_variant</strong>
|
|
|
<ul>
|
|
|
<li>For example the <code>E::A</code> and <code>E::B</code> in <code>enum E { A, B }</code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>newtype_struct</strong>
|
|
|
<ul>
|
|
|
<li>For example <code>struct Millimeters(u8)</code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>newtype_variant</strong>
|
|
|
<ul>
|
|
|
<li>For example the <code>E::N</code> in <code>enum E { N(u8) }</code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>seq</strong>
|
|
|
<ul>
|
|
|
<li>A variably sized heterogeneous sequence of values, for example
|
|
|
<code>Vec<T></code> or <code>HashSet<T></code>. When serializing, the length may or may not
|
|
|
be known before iterating through all the data. When deserializing,
|
|
|
the length is determined by looking at the serialized data.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>tuple</strong>
|
|
|
<ul>
|
|
|
<li>A statically sized heterogeneous sequence of values for which the
|
|
|
length will be known at deserialization time without looking at the
|
|
|
serialized data, for example <code>(u8,)</code> or <code>(String, u64, Vec<T>)</code> or
|
|
|
<code>[u64; 10]</code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>tuple_struct</strong>
|
|
|
<ul>
|
|
|
<li>A named tuple, for example <code>struct Rgb(u8, u8, u8)</code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>tuple_variant</strong>
|
|
|
<ul>
|
|
|
<li>For example the <code>E::T</code> in <code>enum E { T(u8, u8) }</code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>map</strong>
|
|
|
<ul>
|
|
|
<li>A heterogeneous key-value pairing, for example <code>BTreeMap<K, V></code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>struct</strong>
|
|
|
<ul>
|
|
|
<li>A heterogeneous key-value pairing in which the keys are strings and
|
|
|
will be known at deserialization time without looking at the
|
|
|
serialized data, for example <code>struct S { r: u8, g: u8, b: u8 }</code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>struct_variant</strong>
|
|
|
<ul>
|
|
|
<li>For example the <code>E::S</code> in <code>enum E { S { r: u8, g: u8, b: u8 } }</code>.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
</ul>
|
|
|
<p>Many Serde serializers produce text or binary data as output, for example
|
|
|
JSON or Bincode. This is not a requirement of the <code>Serializer</code> trait, and
|
|
|
there are serializers that do not produce text or binary output. One example
|
|
|
is the <code>serde_json::value::Serializer</code> (distinct from the main <code>serde_json</code>
|
|
|
serializer) that produces a <code>serde_json::Value</code> data structure in memory as
|
|
|
output.</p>
|
|
|
<h1 id="example-implementation" class="section-header"><a href="#example-implementation">Example implementation</a></h1>
|
|
|
<p>The <a href="https://serde.rs/data-format.html">example data format</a> presented on the website contains example code for
|
|
|
a basic JSON <code>Serializer</code>.</p>
|
|
|
</div>
|
|
|
<h2 id='associated-types' class='small-section-header'>Associated Types<a href='#associated-types' class='anchor'></a></h2><div class='methods'><h3 id='associatedtype.Ok' class='method'><code id='Ok.t'>type <a href='#associatedtype.Ok' class="type">Ok</a></code></h3><div class='docblock'><p>The output type produced by this <code>Serializer</code> during successful
|
|
|
serialization. Most serializers that produce text or binary output
|
|
|
should set <code>Ok = ()</code> and serialize into an <a href="https://doc.rust-lang.org/std/io/trait.Write.html"><code>io::Write</code></a> or buffer
|
|
|
contained within the <code>Serializer</code> instance. Serializers that build
|
|
|
in-memory data structures may be simplified by using <code>Ok</code> to propagate
|
|
|
the data structure around.</p>
|
|
|
</div><h3 id='associatedtype.Error' class='method'><code id='Error.t'>type <a href='#associatedtype.Error' class="type">Error</a>: <a class="trait" href="../../serde/ser/trait.Error.html" title="trait serde::ser::Error">Error</a></code></h3><div class='docblock'><p>The error type when some error occurs during serialization.</p>
|
|
|
</div><h3 id='associatedtype.SerializeSeq' class='method'><code id='SerializeSeq.t'>type <a href='#associatedtype.SerializeSeq' class="type">SerializeSeq</a>: <a class="trait" href="../../serde/ser/trait.SerializeSeq.html" title="trait serde::ser::SerializeSeq">SerializeSeq</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_seq"><code>serialize_seq</code></a> for serializing the content of the
|
|
|
sequence.</p>
|
|
|
</div><h3 id='associatedtype.SerializeTuple' class='method'><code id='SerializeTuple.t'>type <a href='#associatedtype.SerializeTuple' class="type">SerializeTuple</a>: <a class="trait" href="../../serde/ser/trait.SerializeTuple.html" title="trait serde::ser::SerializeTuple">SerializeTuple</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple"><code>serialize_tuple</code></a> for serializing the content of
|
|
|
the tuple.</p>
|
|
|
</div><h3 id='associatedtype.SerializeTupleStruct' class='method'><code id='SerializeTupleStruct.t'>type <a href='#associatedtype.SerializeTupleStruct' class="type">SerializeTupleStruct</a>: <a class="trait" href="../../serde/ser/trait.SerializeTupleStruct.html" title="trait serde::ser::SerializeTupleStruct">SerializeTupleStruct</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple_struct"><code>serialize_tuple_struct</code></a> for serializing the
|
|
|
content of the tuple struct.</p>
|
|
|
</div><h3 id='associatedtype.SerializeTupleVariant' class='method'><code id='SerializeTupleVariant.t'>type <a href='#associatedtype.SerializeTupleVariant' class="type">SerializeTupleVariant</a>: <a class="trait" href="../../serde/ser/trait.SerializeTupleVariant.html" title="trait serde::ser::SerializeTupleVariant">SerializeTupleVariant</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_tuple_variant"><code>serialize_tuple_variant</code></a> for serializing the
|
|
|
content of the tuple variant.</p>
|
|
|
</div><h3 id='associatedtype.SerializeMap' class='method'><code id='SerializeMap.t'>type <a href='#associatedtype.SerializeMap' class="type">SerializeMap</a>: <a class="trait" href="../../serde/ser/trait.SerializeMap.html" title="trait serde::ser::SerializeMap">SerializeMap</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_map"><code>serialize_map</code></a> for serializing the content of the
|
|
|
map.</p>
|
|
|
</div><h3 id='associatedtype.SerializeStruct' class='method'><code id='SerializeStruct.t'>type <a href='#associatedtype.SerializeStruct' class="type">SerializeStruct</a>: <a class="trait" href="../../serde/ser/trait.SerializeStruct.html" title="trait serde::ser::SerializeStruct">SerializeStruct</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_struct"><code>serialize_struct</code></a> for serializing the content of
|
|
|
the struct.</p>
|
|
|
</div><h3 id='associatedtype.SerializeStructVariant' class='method'><code id='SerializeStructVariant.t'>type <a href='#associatedtype.SerializeStructVariant' class="type">SerializeStructVariant</a>: <a class="trait" href="../../serde/ser/trait.SerializeStructVariant.html" title="trait serde::ser::SerializeStructVariant">SerializeStructVariant</a><Ok = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Error = Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Type returned from <a href="#tymethod.serialize_struct_variant"><code>serialize_struct_variant</code></a> for serializing the
|
|
|
content of the struct variant.</p>
|
|
|
</div></div><span class='loading-content'>Loading content...</span>
|
|
|
<h2 id='required-methods' class='small-section-header'>Required methods<a href='#required-methods' class='anchor'></a></h2><div class='methods'><h3 id='tymethod.serialize_bool' class='method'><code id='serialize_bool.v'>fn <a href='#tymethod.serialize_bool' class='fnname'>serialize_bool</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <code>bool</code> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for bool {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_bool(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_i8' class='method'><code id='serialize_i8.v'>fn <a href='#tymethod.serialize_i8' class='fnname'>serialize_i8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i8.html">i8</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize an <code>i8</code> value.</p>
|
|
|
<p>If the format does not differentiate between <code>i8</code> and <code>i64</code>, a
|
|
|
reasonable implementation would be to cast the value to <code>i64</code> and
|
|
|
forward to <code>serialize_i64</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for i8 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_i8(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_i16' class='method'><code id='serialize_i16.v'>fn <a href='#tymethod.serialize_i16' class='fnname'>serialize_i16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i16.html">i16</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize an <code>i16</code> value.</p>
|
|
|
<p>If the format does not differentiate between <code>i16</code> and <code>i64</code>, a
|
|
|
reasonable implementation would be to cast the value to <code>i64</code> and
|
|
|
forward to <code>serialize_i64</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for i16 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_i16(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_i32' class='method'><code id='serialize_i32.v'>fn <a href='#tymethod.serialize_i32' class='fnname'>serialize_i32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i32.html">i32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize an <code>i32</code> value.</p>
|
|
|
<p>If the format does not differentiate between <code>i32</code> and <code>i64</code>, a
|
|
|
reasonable implementation would be to cast the value to <code>i64</code> and
|
|
|
forward to <code>serialize_i64</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for i32 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_i32(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_i64' class='method'><code id='serialize_i64.v'>fn <a href='#tymethod.serialize_i64' class='fnname'>serialize_i64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i64.html">i64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize an <code>i64</code> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for i64 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_i64(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_u8' class='method'><code id='serialize_u8.v'>fn <a href='#tymethod.serialize_u8' class='fnname'>serialize_u8</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <code>u8</code> value.</p>
|
|
|
<p>If the format does not differentiate between <code>u8</code> and <code>u64</code>, a
|
|
|
reasonable implementation would be to cast the value to <code>u64</code> and
|
|
|
forward to <code>serialize_u64</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for u8 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_u8(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_u16' class='method'><code id='serialize_u16.v'>fn <a href='#tymethod.serialize_u16' class='fnname'>serialize_u16</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <code>u16</code> value.</p>
|
|
|
<p>If the format does not differentiate between <code>u16</code> and <code>u64</code>, a
|
|
|
reasonable implementation would be to cast the value to <code>u64</code> and
|
|
|
forward to <code>serialize_u64</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for u16 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_u16(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_u32' class='method'><code id='serialize_u32.v'>fn <a href='#tymethod.serialize_u32' class='fnname'>serialize_u32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <code>u32</code> value.</p>
|
|
|
<p>If the format does not differentiate between <code>u32</code> and <code>u64</code>, a
|
|
|
reasonable implementation would be to cast the value to <code>u64</code> and
|
|
|
forward to <code>serialize_u64</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for u32 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_u32(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_u64' class='method'><code id='serialize_u64.v'>fn <a href='#tymethod.serialize_u64' class='fnname'>serialize_u64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <code>u64</code> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for u64 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_u64(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_f32' class='method'><code id='serialize_f32.v'>fn <a href='#tymethod.serialize_f32' class='fnname'>serialize_f32</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize an <code>f32</code> value.</p>
|
|
|
<p>If the format does not differentiate between <code>f32</code> and <code>f64</code>, a
|
|
|
reasonable implementation would be to cast the value to <code>f64</code> and
|
|
|
forward to <code>serialize_f64</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for f32 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_f32(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_f64' class='method'><code id='serialize_f64.v'>fn <a href='#tymethod.serialize_f64' class='fnname'>serialize_f64</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize an <code>f64</code> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for f64 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_f64(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_char' class='method'><code id='serialize_char.v'>fn <a href='#tymethod.serialize_char' class='fnname'>serialize_char</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.char.html">char</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a character.</p>
|
|
|
<p>If the format does not support characters, it is reasonable to serialize
|
|
|
it as a single element <code>str</code> or a <code>u32</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for char {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_char(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_str' class='method'><code id='serialize_str.v'>fn <a href='#tymethod.serialize_str' class='fnname'>serialize_str</a>(self, v: &<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <code>&str</code>.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for str {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_str(self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_bytes' class='method'><code id='serialize_bytes.v'>fn <a href='#tymethod.serialize_bytes' class='fnname'>serialize_bytes</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a chunk of raw byte data.</p>
|
|
|
<p>Enables serializers to serialize byte slices more compactly or more
|
|
|
efficiently than other types of slices. If no efficient implementation
|
|
|
is available, a reasonable implementation would be to forward to
|
|
|
<code>serialize_seq</code>. If forwarded, the implementation looks usually just
|
|
|
like this:</p>
|
|
|
<pre><code class="language-edition2018"># use serde::ser::{Serializer, SerializeSeq};
|
|
|
# use serde::private::ser::Error;
|
|
|
#
|
|
|
# struct MySerializer;
|
|
|
#
|
|
|
# impl Serializer for MySerializer {
|
|
|
# type Ok = ();
|
|
|
# type Error = Error;
|
|
|
#
|
|
|
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
|
|
|
let mut seq = self.serialize_seq(Some(v.len()))?;
|
|
|
for b in v {
|
|
|
seq.serialize_element(b)?;
|
|
|
}
|
|
|
seq.end()
|
|
|
}
|
|
|
#
|
|
|
# serde::__serialize_unimplemented! {
|
|
|
# bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some
|
|
|
# unit unit_struct unit_variant newtype_struct newtype_variant
|
|
|
# seq tuple tuple_struct tuple_variant map struct struct_variant
|
|
|
# }
|
|
|
# }
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_none' class='method'><code id='serialize_none.v'>fn <a href='#tymethod.serialize_none' class='fnname'>serialize_none</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <a href="https://doc.rust-lang.org/std/option/enum.Option.html#variant.None"><code>None</code></a> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::{Serialize, Serializer};
|
|
|
#
|
|
|
# enum Option<T> {
|
|
|
# Some(T),
|
|
|
# None,
|
|
|
# }
|
|
|
#
|
|
|
# use self::Option::{Some, None};
|
|
|
#
|
|
|
impl<T> Serialize for Option<T>
|
|
|
where
|
|
|
T: Serialize,
|
|
|
{
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
match *self {
|
|
|
Some(ref value) => serializer.serialize_some(value),
|
|
|
None => serializer.serialize_none(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
#
|
|
|
# fn main() {}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_some' class='method'><code id='serialize_some.v'>fn <a href='#tymethod.serialize_some' class='fnname'>serialize_some</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(self, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>, </span></code></h3><div class='docblock'><p>Serialize a <a href="https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some"><code>Some(T)</code></a> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::{Serialize, Serializer};
|
|
|
#
|
|
|
# enum Option<T> {
|
|
|
# Some(T),
|
|
|
# None,
|
|
|
# }
|
|
|
#
|
|
|
# use self::Option::{Some, None};
|
|
|
#
|
|
|
impl<T> Serialize for Option<T>
|
|
|
where
|
|
|
T: Serialize,
|
|
|
{
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
match *self {
|
|
|
Some(ref value) => serializer.serialize_some(value),
|
|
|
None => serializer.serialize_none(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
#
|
|
|
# fn main() {}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_unit' class='method'><code id='serialize_unit.v'>fn <a href='#tymethod.serialize_unit' class='fnname'>serialize_unit</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <code>()</code> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for () {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_unit()
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_unit_struct' class='method'><code id='serialize_unit_struct.v'>fn <a href='#tymethod.serialize_unit_struct' class='fnname'>serialize_unit_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a unit struct like <code>struct Unit</code> or <code>PhantomData<T></code>.</p>
|
|
|
<p>A reasonable implementation would be to forward to <code>serialize_unit</code>.</p>
|
|
|
<pre><code class="language-edition2018">use serde::{Serialize, Serializer};
|
|
|
|
|
|
struct Nothing;
|
|
|
|
|
|
impl Serialize for Nothing {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_unit_struct("Nothing")
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_unit_variant' class='method'><code id='serialize_unit_variant.v'>fn <a href='#tymethod.serialize_unit_variant' class='fnname'>serialize_unit_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a unit variant like <code>E::A</code> in <code>enum E { A, B }</code>.</p>
|
|
|
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
|
|
|
this variant within the enum, and the <code>variant</code> is the name of the
|
|
|
variant.</p>
|
|
|
<pre><code class="language-edition2018">use serde::{Serialize, Serializer};
|
|
|
|
|
|
enum E {
|
|
|
A,
|
|
|
B,
|
|
|
}
|
|
|
|
|
|
impl Serialize for E {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
match *self {
|
|
|
E::A => serializer.serialize_unit_variant("E", 0, "A"),
|
|
|
E::B => serializer.serialize_unit_variant("E", 1, "B"),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_newtype_struct' class='method'><code id='serialize_newtype_struct.v'>fn <a href='#tymethod.serialize_newtype_struct' class='fnname'>serialize_newtype_struct</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T<br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>, </span></code></h3><div class='docblock'><p>Serialize a newtype struct like <code>struct Millimeters(u8)</code>.</p>
|
|
|
<p>Serializers are encouraged to treat newtype structs as insignificant
|
|
|
wrappers around the data they contain. A reasonable implementation would
|
|
|
be to forward to <code>value.serialize(self)</code>.</p>
|
|
|
<pre><code class="language-edition2018">use serde::{Serialize, Serializer};
|
|
|
|
|
|
struct Millimeters(u8);
|
|
|
|
|
|
impl Serialize for Millimeters {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_newtype_struct("Millimeters", &self.0)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_newtype_variant' class='method'><code id='serialize_newtype_variant.v'>fn <a href='#tymethod.serialize_newtype_variant' class='fnname'>serialize_newtype_variant</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T<br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>, </span></code></h3><div class='docblock'><p>Serialize a newtype variant like <code>E::N</code> in <code>enum E { N(u8) }</code>.</p>
|
|
|
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
|
|
|
this variant within the enum, and the <code>variant</code> is the name of the
|
|
|
variant. The <code>value</code> is the data contained within this newtype variant.</p>
|
|
|
<pre><code class="language-edition2018">use serde::{Serialize, Serializer};
|
|
|
|
|
|
enum E {
|
|
|
M(String),
|
|
|
N(u8),
|
|
|
}
|
|
|
|
|
|
impl Serialize for E {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
match *self {
|
|
|
E::M(ref s) => serializer.serialize_newtype_variant("E", 0, "M", s),
|
|
|
E::N(n) => serializer.serialize_newtype_variant("E", 1, "N", &n),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_seq' class='method'><code id='serialize_seq.v'>fn <a href='#tymethod.serialize_seq' class='fnname'>serialize_seq</a>(<br> self, <br> len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeSeq" title="type serde::Serializer::SerializeSeq">SerializeSeq</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Begin to serialize a variably sized sequence. This call must be
|
|
|
followed by zero or more calls to <code>serialize_element</code>, then a call to
|
|
|
<code>end</code>.</p>
|
|
|
<p>The argument is the number of elements in the sequence, which may or may
|
|
|
not be computable before the sequence is iterated. Some serializers only
|
|
|
support sequences whose length is known up front.</p>
|
|
|
<pre><code class="language-edition2018"># use std::marker::PhantomData;
|
|
|
#
|
|
|
# struct Vec<T>(PhantomData<T>);
|
|
|
#
|
|
|
# impl<T> Vec<T> {
|
|
|
# fn len(&self) -> usize {
|
|
|
# unimplemented!()
|
|
|
# }
|
|
|
# }
|
|
|
#
|
|
|
# impl<'a, T> IntoIterator for &'a Vec<T> {
|
|
|
# type Item = &'a T;
|
|
|
# type IntoIter = Box<Iterator<Item = &'a T>>;
|
|
|
#
|
|
|
# fn into_iter(self) -> Self::IntoIter {
|
|
|
# unimplemented!()
|
|
|
# }
|
|
|
# }
|
|
|
#
|
|
|
use serde::ser::{Serialize, Serializer, SerializeSeq};
|
|
|
|
|
|
impl<T> Serialize for Vec<T>
|
|
|
where
|
|
|
T: Serialize,
|
|
|
{
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
let mut seq = serializer.serialize_seq(Some(self.len()))?;
|
|
|
for element in self {
|
|
|
seq.serialize_element(element)?;
|
|
|
}
|
|
|
seq.end()
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_tuple' class='method'><code id='serialize_tuple.v'>fn <a href='#tymethod.serialize_tuple' class='fnname'>serialize_tuple</a>(<br> self, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTuple" title="type serde::Serializer::SerializeTuple">SerializeTuple</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Begin to serialize a statically sized sequence whose length will be
|
|
|
known at deserialization time without looking at the serialized data.
|
|
|
This call must be followed by zero or more calls to <code>serialize_element</code>,
|
|
|
then a call to <code>end</code>.</p>
|
|
|
<pre><code class="language-edition2018">use serde::ser::{Serialize, Serializer, SerializeTuple};
|
|
|
|
|
|
# mod fool {
|
|
|
# trait Serialize {}
|
|
|
impl<A, B, C> Serialize for (A, B, C)
|
|
|
# {}
|
|
|
# }
|
|
|
#
|
|
|
# struct Tuple3<A, B, C>(A, B, C);
|
|
|
#
|
|
|
# impl<A, B, C> Serialize for Tuple3<A, B, C>
|
|
|
where
|
|
|
A: Serialize,
|
|
|
B: Serialize,
|
|
|
C: Serialize,
|
|
|
{
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
let mut tup = serializer.serialize_tuple(3)?;
|
|
|
tup.serialize_element(&self.0)?;
|
|
|
tup.serialize_element(&self.1)?;
|
|
|
tup.serialize_element(&self.2)?;
|
|
|
tup.end()
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
<pre><code class="language-edition2018">use serde::ser::{Serialize, SerializeTuple, Serializer};
|
|
|
|
|
|
const VRAM_SIZE: usize = 386;
|
|
|
struct Vram([u16; VRAM_SIZE]);
|
|
|
|
|
|
impl Serialize for Vram {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
let mut seq = serializer.serialize_tuple(VRAM_SIZE)?;
|
|
|
for element in &self.0[..] {
|
|
|
seq.serialize_element(element)?;
|
|
|
}
|
|
|
seq.end()
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_tuple_struct' class='method'><code id='serialize_tuple_struct.v'>fn <a href='#tymethod.serialize_tuple_struct' class='fnname'>serialize_tuple_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTupleStruct" title="type serde::Serializer::SerializeTupleStruct">SerializeTupleStruct</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Begin to serialize a tuple struct like <code>struct Rgb(u8, u8, u8)</code>. This
|
|
|
call must be followed by zero or more calls to <code>serialize_field</code>, then a
|
|
|
call to <code>end</code>.</p>
|
|
|
<p>The <code>name</code> is the name of the tuple struct and the <code>len</code> is the number
|
|
|
of data fields that will be serialized.</p>
|
|
|
<pre><code class="language-edition2018">use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
|
|
|
|
|
|
struct Rgb(u8, u8, u8);
|
|
|
|
|
|
impl Serialize for Rgb {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
let mut ts = serializer.serialize_tuple_struct("Rgb", 3)?;
|
|
|
ts.serialize_field(&self.0)?;
|
|
|
ts.serialize_field(&self.1)?;
|
|
|
ts.serialize_field(&self.2)?;
|
|
|
ts.end()
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_tuple_variant' class='method'><code id='serialize_tuple_variant.v'>fn <a href='#tymethod.serialize_tuple_variant' class='fnname'>serialize_tuple_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeTupleVariant" title="type serde::Serializer::SerializeTupleVariant">SerializeTupleVariant</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Begin to serialize a tuple variant like <code>E::T</code> in <code>enum E { T(u8, u8) }</code>. This call must be followed by zero or more calls to
|
|
|
<code>serialize_field</code>, then a call to <code>end</code>.</p>
|
|
|
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
|
|
|
this variant within the enum, the <code>variant</code> is the name of the variant,
|
|
|
and the <code>len</code> is the number of data fields that will be serialized.</p>
|
|
|
<pre><code class="language-edition2018">use serde::ser::{Serialize, SerializeTupleVariant, Serializer};
|
|
|
|
|
|
enum E {
|
|
|
T(u8, u8),
|
|
|
U(String, u32, u32),
|
|
|
}
|
|
|
|
|
|
impl Serialize for E {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
match *self {
|
|
|
E::T(ref a, ref b) => {
|
|
|
let mut tv = serializer.serialize_tuple_variant("E", 0, "T", 2)?;
|
|
|
tv.serialize_field(a)?;
|
|
|
tv.serialize_field(b)?;
|
|
|
tv.end()
|
|
|
}
|
|
|
E::U(ref a, ref b, ref c) => {
|
|
|
let mut tv = serializer.serialize_tuple_variant("E", 1, "U", 3)?;
|
|
|
tv.serialize_field(a)?;
|
|
|
tv.serialize_field(b)?;
|
|
|
tv.serialize_field(c)?;
|
|
|
tv.end()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_map' class='method'><code id='serialize_map.v'>fn <a href='#tymethod.serialize_map' class='fnname'>serialize_map</a>(<br> self, <br> len: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeMap" title="type serde::Serializer::SerializeMap">SerializeMap</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Begin to serialize a map. This call must be followed by zero or more
|
|
|
calls to <code>serialize_key</code> and <code>serialize_value</code>, then a call to <code>end</code>.</p>
|
|
|
<p>The argument is the number of elements in the map, which may or may not
|
|
|
be computable before the map is iterated. Some serializers only support
|
|
|
maps whose length is known up front.</p>
|
|
|
<pre><code class="language-edition2018"># use std::marker::PhantomData;
|
|
|
#
|
|
|
# struct HashMap<K, V>(PhantomData<K>, PhantomData<V>);
|
|
|
#
|
|
|
# impl<K, V> HashMap<K, V> {
|
|
|
# fn len(&self) -> usize {
|
|
|
# unimplemented!()
|
|
|
# }
|
|
|
# }
|
|
|
#
|
|
|
# impl<'a, K, V> IntoIterator for &'a HashMap<K, V> {
|
|
|
# type Item = (&'a K, &'a V);
|
|
|
# type IntoIter = Box<Iterator<Item = (&'a K, &'a V)>>;
|
|
|
#
|
|
|
# fn into_iter(self) -> Self::IntoIter {
|
|
|
# unimplemented!()
|
|
|
# }
|
|
|
# }
|
|
|
#
|
|
|
use serde::ser::{Serialize, Serializer, SerializeMap};
|
|
|
|
|
|
impl<K, V> Serialize for HashMap<K, V>
|
|
|
where
|
|
|
K: Serialize,
|
|
|
V: Serialize,
|
|
|
{
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
let mut map = serializer.serialize_map(Some(self.len()))?;
|
|
|
for (k, v) in self {
|
|
|
map.serialize_entry(k, v)?;
|
|
|
}
|
|
|
map.end()
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_struct' class='method'><code id='serialize_struct.v'>fn <a href='#tymethod.serialize_struct' class='fnname'>serialize_struct</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeStruct" title="type serde::Serializer::SerializeStruct">SerializeStruct</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Begin to serialize a struct like <code>struct Rgb { r: u8, g: u8, b: u8 }</code>.
|
|
|
This call must be followed by zero or more calls to <code>serialize_field</code>,
|
|
|
then a call to <code>end</code>.</p>
|
|
|
<p>The <code>name</code> is the name of the struct and the <code>len</code> is the number of
|
|
|
data fields that will be serialized.</p>
|
|
|
<pre><code class="language-edition2018">use serde::ser::{Serialize, SerializeStruct, Serializer};
|
|
|
|
|
|
struct Rgb {
|
|
|
r: u8,
|
|
|
g: u8,
|
|
|
b: u8,
|
|
|
}
|
|
|
|
|
|
impl Serialize for Rgb {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
let mut rgb = serializer.serialize_struct("Rgb", 3)?;
|
|
|
rgb.serialize_field("r", &self.r)?;
|
|
|
rgb.serialize_field("g", &self.g)?;
|
|
|
rgb.serialize_field("b", &self.b)?;
|
|
|
rgb.end()
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='tymethod.serialize_struct_variant' class='method'><code id='serialize_struct_variant.v'>fn <a href='#tymethod.serialize_struct_variant' class='fnname'>serialize_struct_variant</a>(<br> self, <br> name: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> variant_index: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, <br> variant: &'static <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, <br> len: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.SerializeStructVariant" title="type serde::Serializer::SerializeStructVariant">SerializeStructVariant</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Begin to serialize a struct variant like <code>E::S</code> in <code>enum E { S { r: u8, g: u8, b: u8 } }</code>. This call must be followed by zero or more calls to
|
|
|
<code>serialize_field</code>, then a call to <code>end</code>.</p>
|
|
|
<p>The <code>name</code> is the name of the enum, the <code>variant_index</code> is the index of
|
|
|
this variant within the enum, the <code>variant</code> is the name of the variant,
|
|
|
and the <code>len</code> is the number of data fields that will be serialized.</p>
|
|
|
<pre><code class="language-edition2018">use serde::ser::{Serialize, SerializeStructVariant, Serializer};
|
|
|
|
|
|
enum E {
|
|
|
S { r: u8, g: u8, b: u8 },
|
|
|
}
|
|
|
|
|
|
impl Serialize for E {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
match *self {
|
|
|
E::S {
|
|
|
ref r,
|
|
|
ref g,
|
|
|
ref b,
|
|
|
} => {
|
|
|
let mut sv = serializer.serialize_struct_variant("E", 0, "S", 3)?;
|
|
|
sv.serialize_field("r", r)?;
|
|
|
sv.serialize_field("g", g)?;
|
|
|
sv.serialize_field("b", b)?;
|
|
|
sv.end()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div></div><span class='loading-content'>Loading content...</span>
|
|
|
<h2 id='provided-methods' class='small-section-header'>Provided methods<a href='#provided-methods' class='anchor'></a></h2><div class='methods'><h3 id='method.serialize_i128' class='method'><code id='serialize_i128.v'>fn <a href='#method.serialize_i128' class='fnname'>serialize_i128</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i128.html">i128</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize an <code>i128</code> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for i128 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_i128(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
<p>This method is available only on Rust compiler versions >=1.26. The
|
|
|
default behavior unconditionally returns an error.</p>
|
|
|
</div><h3 id='method.serialize_u128' class='method'><code id='serialize_u128.v'>fn <a href='#method.serialize_u128' class='fnname'>serialize_u128</a>(self, v: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u128.html">u128</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>></code></h3><div class='docblock'><p>Serialize a <code>u128</code> value.</p>
|
|
|
<pre><code class="language-edition2018"># use serde::Serializer;
|
|
|
#
|
|
|
# serde::__private_serialize!();
|
|
|
#
|
|
|
impl Serialize for u128 {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.serialize_u128(*self)
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
<p>This method is available only on Rust compiler versions >=1.26. The
|
|
|
default behavior unconditionally returns an error.</p>
|
|
|
</div><h3 id='method.collect_seq' class='method'><code id='collect_seq.v'>fn <a href='#method.collect_seq' class='fnname'>collect_seq</a><I>(self, iter: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>> <span class="where fmt-newline">where<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a>,<br> <I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a>>::<a class="type" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html#associatedtype.Item" title="type core::iter::traits::collect::IntoIterator::Item">Item</a>: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>, </span></code></h3><div class='docblock'><p>Collect an iterator as a sequence.</p>
|
|
|
<p>The default implementation serializes each item yielded by the iterator
|
|
|
using <a href="#tymethod.serialize_seq"><code>serialize_seq</code></a>. Implementors should not need to override this
|
|
|
method.</p>
|
|
|
<pre><code class="language-edition2018">use serde::{Serialize, Serializer};
|
|
|
|
|
|
struct SecretlyOneHigher {
|
|
|
data: Vec<i32>,
|
|
|
}
|
|
|
|
|
|
impl Serialize for SecretlyOneHigher {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.collect_seq(self.data.iter().map(|x| x + 1))
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='method.collect_map' class='method'><code id='collect_map.v'>fn <a href='#method.collect_map' class='fnname'>collect_map</a><K, V, I>(self, iter: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>> <span class="where fmt-newline">where<br> K: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br> V: <a class="trait" href="../../serde/trait.Serialize.html" title="trait serde::Serialize">Serialize</a>,<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a><Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>K, V<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>>, </span></code></h3><div class='docblock'><p>Collect an iterator as a map.</p>
|
|
|
<p>The default implementation serializes each pair yielded by the iterator
|
|
|
using <a href="#tymethod.serialize_map"><code>serialize_map</code></a>. Implementors should not need to override this
|
|
|
method.</p>
|
|
|
<pre><code class="language-edition2018">use serde::{Serialize, Serializer};
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
|
struct MapToUnit {
|
|
|
keys: BTreeSet<i32>,
|
|
|
}
|
|
|
|
|
|
// Serializes as a map in which the values are all unit.
|
|
|
impl Serialize for MapToUnit {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.collect_map(self.keys.iter().map(|k| (k, ())))
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='method.collect_str' class='method'><code id='collect_str.v'>fn <a href='#method.collect_str' class='fnname'>collect_str</a><T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(self, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Ok" title="type serde::Serializer::Ok">Ok</a>, Self::<a class="type" href="../../serde/trait.Serializer.html#associatedtype.Error" title="type serde::Serializer::Error">Error</a>> <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a>, </span></code></h3><div class='docblock'><p>Serialize a string produced by an implementation of <code>Display</code>.</p>
|
|
|
<p>The default implementation builds a heap-allocated <a href="https://doc.rust-lang.org/std/string/struct.String.html"><code>String</code></a> and
|
|
|
delegates to <a href="#tymethod.serialize_str"><code>serialize_str</code></a>. Serializers are encouraged to provide a
|
|
|
more efficient implementation if possible.</p>
|
|
|
<pre><code class="language-edition2018"># struct DateTime;
|
|
|
#
|
|
|
# impl DateTime {
|
|
|
# fn naive_local(&self) -> () { () }
|
|
|
# fn offset(&self) -> () { () }
|
|
|
# }
|
|
|
#
|
|
|
use serde::{Serialize, Serializer};
|
|
|
|
|
|
impl Serialize for DateTime {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
serializer.collect_str(&format_args!("{:?}{:?}",
|
|
|
self.naive_local(),
|
|
|
self.offset()))
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
</div><h3 id='method.is_human_readable' class='method'><code id='is_human_readable.v'>fn <a href='#method.is_human_readable' class='fnname'>is_human_readable</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></h3><div class='docblock'><p>Determine whether <code>Serialize</code> implementations should serialize in
|
|
|
human-readable form.</p>
|
|
|
<p>Some types have a human-readable form that may be somewhat expensive to
|
|
|
construct, as well as a binary form that is compact and efficient.
|
|
|
Generally text-based formats like JSON and YAML will prefer to use the
|
|
|
human-readable one and binary formats like Bincode will prefer the
|
|
|
compact one.</p>
|
|
|
<pre><code class="language-edition2018"># use std::fmt::{self, Display};
|
|
|
#
|
|
|
# struct Timestamp;
|
|
|
#
|
|
|
# impl Timestamp {
|
|
|
# fn seconds_since_epoch(&self) -> u64 { unimplemented!() }
|
|
|
# }
|
|
|
#
|
|
|
# impl Display for Timestamp {
|
|
|
# fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
# unimplemented!()
|
|
|
# }
|
|
|
# }
|
|
|
#
|
|
|
use serde::{Serialize, Serializer};
|
|
|
|
|
|
impl Serialize for Timestamp {
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
where
|
|
|
S: Serializer,
|
|
|
{
|
|
|
if serializer.is_human_readable() {
|
|
|
// Serialize to a human-readable string "2015-05-15T17:01:00Z".
|
|
|
self.to_string().serialize(serializer)
|
|
|
} else {
|
|
|
// Serialize to a compact binary representation.
|
|
|
self.seconds_since_epoch().serialize(serializer)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
</code></pre>
|
|
|
<p>The default implementation of this method returns <code>true</code>. Data formats
|
|
|
may override this to <code>false</code> to request a compact form for types that
|
|
|
support one. Note that modifying this method to change a format from
|
|
|
human-readable to compact or vice versa should be regarded as a breaking
|
|
|
change, as a value serialized in human-readable mode is not required to
|
|
|
deserialize from the same data in compact mode.</p>
|
|
|
</div></div><span class='loading-content'>Loading content...</span>
|
|
|
<h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'></div><span class='loading-content'>Loading content...</span><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async
|
|
|
src="../../implementors/serde/ser/trait.Serializer.js">
|
|
|
</script></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "serde";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html> |