Struct cbor_event::se::Serializer

source ·
pub struct Serializer<W: Write + Sized>(_);
Expand description

simple CBOR serializer into any std::io::Write.

Implementations§

create a new serializer.

use cbor_event::se::{Serializer};

let serializer = Serializer::new_vec();

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.

finalize the serializer, returning the serializer bytes

use cbor_event::se::{Serializer};

let serializer = Serializer::new_vec();

let bytes = serializer.finalize();

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");

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");

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");

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");

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");

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");

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");

Write a tag that indicates that the following list is a finite set. See https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml.

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");

Convenient member function to chain serialisation

Trait Implementations§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.