Struct nyoom_json::Serializer

source ·
#[repr(transparent)]
pub struct Serializer<S: JsonBuffer> { /* private fields */ }
Expand description

A general JSON serializer, over a mutable buffer of some sort.

Examples

use nyoom_json::Serializer;

let mut out = String::new();
let mut ser = Serializer::create(&mut out);

let mut obj = ser.object();
obj.field("kind", "cat");
obj.field("has_been_fed", false);
obj.field("meow_decibels", 45);
obj.end();

let mut arr = ser.array();
arr.add("friends");
arr.add("romans");
arr.add("countrymen");
arr.end();

ser.end();

Implementations§

source§

impl<'a> Serializer<StringBuffer<'a>>

source

pub fn create(buf: &'a mut String) -> Serializer<StringBuffer<'a>>

Creates a serializer over a mutable string reference.

source§

impl<S: JsonBuffer> Serializer<S>

source

pub fn new(buf: S) -> Serializer<S>

source

pub fn write(&mut self, val: impl WriteToJson<S>)

Writes out a single primitive JSON value.

Examples
use nyoom_json::Serializer;

let mut out = String::new();
let mut ser = Serializer::create(&mut out);
ser.write(3);
source

pub fn array(&mut self) -> ArrayWriter<&mut S>

Starts serialization of an array.

Examples
use nyoom_json::Serializer;

let mut out = String::new();
let mut ser = Serializer::create(&mut out);

let mut arr = ser.array();
arr.add("friends");
arr.add("romans");
arr.add("countrymen");
arr.end();
source

pub fn object(&mut self) -> ObjectWriter<&mut S>

Starts serialization of an object.

Examples
use nyoom_json::Serializer;

let mut out = String::new();
let mut ser = Serializer::create(&mut out);

let mut obj = ser.object();
obj.field("kind", "cat");
obj.field("has_been_fed", false);
obj.field("meow_decibels", 45);
obj.end();
source

pub fn end(self) -> S

Ends the serializer.

Auto Trait Implementations§

§

impl<S> RefUnwindSafe for Serializer<S>where S: RefUnwindSafe,

§

impl<S> Send for Serializer<S>where S: Send,

§

impl<S> Sync for Serializer<S>where S: Sync,

§

impl<S> Unpin for Serializer<S>where S: Unpin,

§

impl<S> UnwindSafe for Serializer<S>where S: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.