[][src]Trait json_in_type::JSONValue

pub trait JSONValue {
    fn write_json<W: Write>(&self, w: &mut W) -> Result<()>;

    fn to_json_string(&self) -> String { ... }
fn to_json_buffer(&self) -> Vec<u8> { ... } }

A trait implemented by types that can be serialized to JSON

This trait can be derived for custom structs using json_in_type_derive

Required methods

fn write_json<W: Write>(&self, w: &mut W) -> Result<()>

Write the object as json to the given writer

Examples

Write a JSON object to a file

use json_in_type::JSONValue;

vec![1, 2, 3].write_json(&mut my_file);
Loading content...

Provided methods

fn to_json_string(&self) -> String

Returns the object formatted as a json string

Panics

If you implement JSONValue on your own types and emit invalid UTF-8 in write_json. If you use the implementations of JSONValue provided in this library, this function will never panic.

fn to_json_buffer(&self) -> Vec<u8>

Returns a buffer containing the bytes of a json representation of the object

Loading content...

Implementations on Foreign Types

impl JSONValue for i8[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for i16[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for i32[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for i64[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for i128[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for isize[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for u8[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for u16[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for u32[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for u64[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for u128[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for usize[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for f32[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for f64[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for ()[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for bool[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<T: JSONValue> JSONValue for Option<T>[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<T: JSONValue> JSONValue for Vec<T>[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<T: JSONValue, I: Iterator<Item = T>> JSONValue for RefCell<I>[src]

Allows to serialize an iterator to JSON in a streaming fashion. The iterator needs to be wrapped in a RefCell because it will be consumed as JSON is written.

Examples

Serialize an iterator JSON

use std::cell::RefCell;
use std::iter::repeat;
use json_in_type::JSONValue;

let my_iter = repeat(42).take(3);
let my_iter_cell = RefCell::new(my_iter);

// The iterator will be consumed as json is produced
assert_eq!("[42,42,42]", my_iter_cell.to_json_string());

// Here, the iterator has already been consumed, so there is nothing left to serialize
assert_eq!("[]", my_iter_cell.to_json_string());

Write a large JSON to a file

In this example, we take a potentially large input file, and serialize it to a JSON file containing an array with all the lines from the input file.

The output should look like this:

[
  {"line": 1, "contents": "a line of text"},
  {"line": 2, "contents": "another line of text"}
]
use std::cell::RefCell;
use std::io::BufRead;
use std::io::BufReader;
use json_in_type::*;


let json_lines = BufReader::new(input_file)
                .lines()
                .map(|l| l.unwrap())
                .enumerate()
                .map(|(i, contents)| json_object!{line:i+1, contents:contents});

RefCell::new(json_lines).write_json(&mut output_file);

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<K: JSONString + Eq + Hash, V: JSONValue, S: BuildHasher> JSONValue for HashMap<K, V, S>[src]

Serialize a HashMap to a JSON object. The property order is not guaranteed.

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for char[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<'a> JSONValue for &'a str[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for String[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<'a, S: JSONValue + ?Sized> JSONValue for &'a S[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<'a, S: JSONValue + ?Sized> JSONValue for Box<S>[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

Loading content...

Implementors

impl JSONValue for JSONfalse[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for JSONtrue[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for JSONListEnd[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl JSONValue for JSONObjectEnd[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<K, V, I> JSONValue for ToJSONObject<K, V, I> where
    K: JSONString,
    V: JSONValue,
    &'a I: IntoIterator<Item = &'a (K, V)>, 
[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<K: JSONString, V: JSONValue, U: JSONObject> JSONValue for JSONObjectEntry<K, V, U>[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<T: JSONValue, U> JSONValue for ToJSONList<T, U> where
    &'a U: IntoIterator<Item = &'a T>, 
[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

impl<T: JSONValue, U: JSONList> JSONValue for JSONListElem<T, U>[src]

fn to_json_string(&self) -> String[src]

fn to_json_buffer(&self) -> Vec<u8>[src]

Loading content...