pub struct Serializer<W: Write + Sized>(/* private fields */);Expand description
simple CBOR serializer into any
std::io::Write.
Implementations§
Source§impl Serializer<Vec<u8>>
impl Serializer<Vec<u8>>
Source§impl<W: Write + Sized> Serializer<W>
impl<W: Write + Sized> Serializer<W>
Sourcepub fn write_raw_bytes(&mut self, bytes: &[u8]) -> Result<&mut Self>
pub fn write_raw_bytes(&mut self, bytes: &[u8]) -> Result<&mut Self>
extend the serializer with the given bytes
This is not encoding the given bytes in the CBOR format. More a way to add already CBOR encoded data or to add any bytes that may suite your protocol.
pub fn new(w: W) -> Self
Sourcepub fn finalize(self) -> W
pub fn finalize(self) -> W
finalize the serializer, returning the serializer bytes
use cbor_event::se::{Serializer};
let serializer = Serializer::new_vec();
let bytes = serializer.finalize();
Sourcepub fn write_unsigned_integer(&mut self, value: u64) -> Result<&mut Self>
pub fn write_unsigned_integer(&mut self, value: u64) -> Result<&mut Self>
serialise the given unsigned integer
§Example
use cbor_event::se::{Serializer};
let mut serializer = Serializer::new_vec();
serializer.write_unsigned_integer(0x12)
.expect("write a negative integer");
Sourcepub fn write_unsigned_integer_sz(
&mut self,
value: u64,
sz: Sz,
) -> Result<&mut Self>
pub fn write_unsigned_integer_sz( &mut self, value: u64, sz: Sz, ) -> Result<&mut Self>
serialise the given unsigned integer using a specific encoding
see write_unsigned_integer and Sz
Sourcepub fn write_negative_integer(&mut self, value: i64) -> Result<&mut Self>
pub fn write_negative_integer(&mut self, value: i64) -> Result<&mut Self>
write a negative integer
This function fails if one tries to write a non negative value.
use cbor_event::se::{Serializer};
let mut serializer = Serializer::new_vec();
serializer.write_negative_integer(-12)
.expect("write a negative integer");
Sourcepub fn write_negative_integer_sz(
&mut self,
value: i128,
sz: Sz,
) -> Result<&mut Self>
pub fn write_negative_integer_sz( &mut self, value: i128, sz: Sz, ) -> Result<&mut Self>
write a negative integer using a specific encoding
see write_negative_integer and Sz
value must be within -1 and -u64::MAX -1 to fit into CBOR nint
Sourcepub fn write_bytes<B: AsRef<[u8]>>(&mut self, bytes: B) -> Result<&mut Self>
pub fn write_bytes<B: AsRef<[u8]>>(&mut self, bytes: B) -> Result<&mut Self>
write the given object as bytes
use cbor_event::se::{Serializer};
let mut serializer = Serializer::new_vec();
serializer.write_bytes(vec![0,1,2,3])
.expect("write bytes");
Sourcepub fn write_bytes_sz<B: AsRef<[u8]>>(
&mut self,
bytes: B,
sz: StringLenSz,
) -> Result<&mut Self>
pub fn write_bytes_sz<B: AsRef<[u8]>>( &mut self, bytes: B, sz: StringLenSz, ) -> Result<&mut Self>
write the given object as bytes using a specific bytestring encoding
see write_bytes and StringLenSz
Sourcepub fn write_text<S: AsRef<str>>(&mut self, text: S) -> Result<&mut Self>
pub fn write_text<S: AsRef<str>>(&mut self, text: S) -> Result<&mut Self>
write the given object as text
use cbor_event::se::{Serializer};
let mut serializer = Serializer::new_vec();
serializer.write_text(r"hello world")
.expect("write text");
Sourcepub fn write_text_sz<S: AsRef<str>>(
&mut self,
text: S,
sz: StringLenSz,
) -> Result<&mut Self>
pub fn write_text_sz<S: AsRef<str>>( &mut self, text: S, sz: StringLenSz, ) -> Result<&mut Self>
write the given object as text using a specific string encoding
see write_text and StringLenSz
Sourcepub fn write_array(&mut self, len: Len) -> Result<&mut Self>
pub fn write_array(&mut self, len: Len) -> Result<&mut Self>
start to write an array
Either you know the length of your array and you can pass it to the funtion or use an indefinite length.
- if you set a fixed length of element, you are responsible to set the correct amount of elements.
- if you set an indefinite length, you are responsible to write the
Special::Breakwhen your stream ends.
§Example
use cbor_event::{se::{Serializer}, Len};
let mut serializer = Serializer::new_vec();
serializer
.write_array(Len::Len(2)).expect("write an array")
.write_text(r"hello").expect("write text")
.write_text(r"world").expect("write text");
use cbor_event::{se::{Serializer}, Len, Special};
let mut serializer = Serializer::new_vec();
serializer
.write_array(Len::Indefinite).expect("write an array")
.write_text(r"hello").expect("write text")
.write_text(r"world").expect("write text")
.write_special(Special::Break).expect("write break");
Sourcepub fn write_array_sz(&mut self, len: LenSz) -> Result<&mut Self>
pub fn write_array_sz(&mut self, len: LenSz) -> Result<&mut Self>
start to write an array using a specific length encoding
see write_array and LenSz
Sourcepub fn write_map(&mut self, len: Len) -> Result<&mut Self>
pub fn write_map(&mut self, len: Len) -> Result<&mut Self>
start to write a map
Either you know the length of your map and you can pass it to the funtion or use an indefinite length.
- if you set a fixed length of element, you are responsible to set the correct amount of elements.
- if you set an indefinite length, you are responsible to write the
Special::Breakwhen your stream ends.
A map is like an array but works by pair of element, so the length is half of the number of element you are going to write, i.e. the number of pairs, not the number of elements.
§Example
use cbor_event::{se::{Serializer}, Len};
let mut serializer = Serializer::new_vec();
serializer
.write_map(Len::Len(2)).expect("write a map")
.write_unsigned_integer(1).expect("write unsigned integer")
.write_text(r"hello").expect("write text")
.write_unsigned_integer(2).expect("write unsigned integer")
.write_text(r"world").expect("write text");
use cbor_event::{se::{Serializer}, Len, Special};
let mut serializer = Serializer::new_vec();
serializer
.write_map(Len::Indefinite).expect("write a map")
.write_unsigned_integer(1).expect("write unsigned integer")
.write_text(r"hello").expect("write text")
.write_unsigned_integer(2).expect("write unsigned integer")
.write_text(r"world").expect("write text")
.write_special(Special::Break).expect("write the break");
Sourcepub fn write_map_sz(&mut self, len: LenSz) -> Result<&mut Self>
pub fn write_map_sz(&mut self, len: LenSz) -> Result<&mut Self>
start to write a map using a specific length encoding
see write_map and LenSz
Sourcepub fn write_tag(&mut self, tag: u64) -> Result<&mut Self>
pub fn write_tag(&mut self, tag: u64) -> Result<&mut Self>
write a tag
in cbor a tag should be followed by a tagged object. You are responsible to making sure you are writing the tagged object just after this
§Example
use cbor_event::{se::{Serializer}, Len};
let mut serializer = Serializer::new_vec();
serializer
.write_tag(24).expect("write a tag")
.write_text(r"hello").expect("write text");
Sourcepub fn write_tag_sz(&mut self, tag: u64, sz: Sz) -> Result<&mut Self>
pub fn write_tag_sz(&mut self, tag: u64, sz: Sz) -> Result<&mut Self>
write a tag using a specific encoding
see write_tag and Sz
Sourcepub fn write_set_tag(&mut self) -> Result<&mut Self>
pub fn write_set_tag(&mut self) -> Result<&mut Self>
Write a tag that indicates that the following list is a finite set. See https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml.
Sourcepub fn write_special(&mut self, special: Special) -> Result<&mut Self>
pub fn write_special(&mut self, special: Special) -> Result<&mut Self>
write a special value in cbor
§Example
use cbor_event::{se::{Serializer}, Len, Special};
let mut serializer = Serializer::new_vec();
serializer
.write_array(Len::Indefinite).expect("write an array")
.write_special(Special::Bool(false)).expect("write false")
.write_special(Special::Bool(true)).expect("write true")
.write_special(Special::Null).expect("write null")
.write_special(Special::Undefined).expect("write undefined")
.write_special(Special::Break).expect("write the break");