[][src]Crate tokio_serde_json

Stream and Sink adaptors for serializing and deserializing values using JSON.

This crate provides adaptors for going from a stream or sink of buffers (Bytes) to a stream or sink of values by performing JSON encoding or decoding. It is expected that each yielded buffer contains a single serialized JSON value. The specific strategy by which this is done is left up to the user. One option is to use using length_delimited from tokio-io.

Examples

This example is not tested
use futures::{Future, Sink};

use tokio::{codec::{FramedWrite, LengthDelimitedCodec}, net::TcpStream};

use tokio_serde_json::WriteJson;

// Bind a server socket
let socket = TcpStream::connect(
    &"127.0.0.1:17653".parse().unwrap(),
    &handle);

socket.and_then(|socket| {
    // Delimit frames using a length header
    let length_delimited = FramedWrite::new(socket, LengthDelimitedCodec::new());

    // Serialize frames with JSON
    let serialized = WriteJson::new(length_delimited);

    // Send the value
    serialized.send(json!({
      "name": "John Doe",
      "age": 43,
      "phones": [
        "+44 1234567",
        "+44 2345678"
      ]
    }))
})

For a full working server and client example, see the examples directory.

Structs

ReadJson

Adapts a stream of JSON encoded buffers to a stream of values by deserializing them.

WriteJson

Adapts a buffer sink to a value sink by serializing the values as JSON.