#json-parser #serialization #serde-json #tokio #frame #write-json #stream-sink

tokio-serde-json

Utilities needed to easily implement a Tokio JSON transport using Serde for JSON serialization and deserialization of frame values

4 releases (breaking)

0.3.0 Oct 7, 2019
0.2.0 Nov 9, 2018
0.1.0 Aug 16, 2018
0.0.0 Mar 14, 2017

#11 in #write-json

Download history 91/week @ 2025-09-18 110/week @ 2025-09-25 60/week @ 2025-10-02 139/week @ 2025-10-09 73/week @ 2025-10-16 89/week @ 2025-10-23 59/week @ 2025-10-30 34/week @ 2025-11-06 28/week @ 2025-11-13 42/week @ 2025-11-20 12/week @ 2025-11-27 27/week @ 2025-12-04 49/week @ 2025-12-11 45/week @ 2025-12-18 22/week @ 2025-12-25 51/week @ 2026-01-01

175 downloads per month
Used in 2 crates

MIT license

12KB
130 lines

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::prelude::*;

use serde_json::json;

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

use tokio_serde_json::WriteJson;

#[tokio::main]
async fn main() {
    // Bind a server socket
    let socket = TcpStream::connect("127.0.0.1:17653")
        .await
        .unwrap();

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

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

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

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


Tokio / Serde bindings for JSON

Utilities needed to easily implement a Tokio JSON transport using serde for JSON serialization and deserialization of frame values.

Documentation

Usage

To use tokio-serde-json, first add this to your Cargo.toml:

[dependencies]
tokio-serde-json = "0.2"

Next, add this to your crate:

extern crate tokio_serde_json;

use tokio_serde_json::{ReadJson, WriteJson};

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in tower-web by you, shall be licensed as MIT, without any additional terms or conditions.

Dependencies

~4MB
~84K SLoC