jsonxs

Crates.iojsonxs
lib.rsjsonxs
version0.0.1
sourcesrc
created_at2020-11-17 20:41:40.249253
updated_at2020-11-17 20:41:40.249253
descriptionExtra small JSON exporter
homepagehttps://github.com/pkozelka/jsonxs-rs
repositoryhttps://github.com/pkozelka/jsonxs-rs
max_upload_size
id313398
size12,509
Petr Kozelka (pkozelka)

documentation

https://docs.rs/jsonxs

README

json-xs

JSON Serialization with eXtra Small memory footprint.

Provides helper to produce JSON for existing, JSON-agnostic data model.

Usage

This library is not yet published to crates.io as it is still very immature.

Add following dependency to your Cargo.toml:

[dependencies]
jsonxs = { git = "https://github.com/pkozelka/jsonxs-rs", branch = "master" }

In your code, use it like this:

use std::collections::HashMap;
use std::io::Result;

use jsonxs::{JsonXsSerializer, JsonXsValue};
pub fn json_save(map: &HashMap<String, String>) -> Result<()> {
    // let mut json = JsonXsSerializer::use_file("hello.json")?;
    let mut json = JsonXsSerializer::use_stdout();
    json.open_obj(JsonXsValue::NA)?; // opens root object, "{"

    json.open_obj("map")?;
    for (k, v) in map {
        json.element(k, v)?;
    }
    json.close()?;

    json.close()?; // closes root object, "}"
    json.done() // checks that nesting went well
}

fn main() {
    let mut map = HashMap::new();
    map.insert("hello".to_string(), "world".to_string());
    map.insert("how".to_string(), "are you".to_string());

    json_save(&map).unwrap();
}
Commit count: 8

cargo fmt