1 unstable release
Uses old Rust 2015
0.1.0 | Aug 15, 2018 |
---|
#51 in #sink
30 downloads per month
Used in daemon-engine
10KB
130 lines
Tokio / Serde bindings for JSON
Utilities needed to easily implement a Tokio JSON transport using serde for JSON serialization and deserialization of frame values.
Usage
To use tokio-serde-json
, first add this to your Cargo.toml
:
[dependencies]
tokio-serde-json = { git = "https://github.com/carllerche/tokio-serde-json" }
Next, add this to your crate:
extern crate tokio_serde_json;
use tokio_serde_json::{ReadJson, WriteJson};
License
tokio-serde-json
is primarily distributed under the terms of both the MIT
license and the Apache License (Version 2.0), with portions covered by various
BSD-like licenses.
See LICENSE-APACHE, and LICENSE-MIT for details.
lib.rs
:
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
use futures::{Future, Sink};
use tokio_core::reactor::Core;
use tokio_core::net::TcpStream;
// Use length delimited frames
use tokio_io::codec::length_delimited;
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 = length_delimited::FramedWrite::new(socket);
// 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.
Dependencies
~0.7–1.2MB
~24K SLoC