Struct cbor_event::se::Serializer
source · 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_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_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_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_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::Break
when 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_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::Break
when 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_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_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");