All Projects → lovasoa → Json_in_type

lovasoa / Json_in_type

Licence: bsd-2-clause
Fast json encoder in rust, that encodes the structure of JSON values in their types

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Json in type

Yyjson
The fastest JSON library in C
Stars: ✭ 1,894 (+2267.5%)
Mutual labels:  json, performance
Velocypack
A fast and compact format for serialization and storage
Stars: ✭ 347 (+333.75%)
Mutual labels:  json, performance
Zio Json
Fast, secure JSON library with tight ZIO integration.
Stars: ✭ 218 (+172.5%)
Mutual labels:  json, performance
Djson
Fast Go decoder for dynamic JSON
Stars: ✭ 588 (+635%)
Mutual labels:  json, performance
Rz Go
Ripzap - Fast and 0 allocs leveled JSON logger for Go ⚡️. Dependency free.
Stars: ✭ 256 (+220%)
Mutual labels:  json, performance
Encoding
Go package containing implementations of efficient encoding, decoding, and validation APIs.
Stars: ✭ 705 (+781.25%)
Mutual labels:  json, performance
Foxylink
An easy way to handle integration tasks in a reliable way and run them on 1C:Enterprise server
Stars: ✭ 77 (-3.75%)
Mutual labels:  json
Neo
Create blazing fast multithreaded Web Apps
Stars: ✭ 1,219 (+1423.75%)
Mutual labels:  json
Rumqttd
rust mqtt broker
Stars: ✭ 77 (-3.75%)
Mutual labels:  performance
Json2go
Create go type representation from json
Stars: ✭ 76 (-5%)
Mutual labels:  json
Json Stat
JSON-stat Toolkit version 0
Stars: ✭ 79 (-1.25%)
Mutual labels:  json
Clef Tool
A command-line tool for manipulating Compact Log Event Format files
Stars: ✭ 79 (-1.25%)
Mutual labels:  json
Dartson
Dartson is a Dart library that can be used to convert Dart objects into a JSON string.
Stars: ✭ 78 (-2.5%)
Mutual labels:  json
Tortilla
Wrapping web APIs made easy.
Stars: ✭ 1,215 (+1418.75%)
Mutual labels:  json
Reactime
Chrome extension for improving and optimizing performance in React applications (Gatsby and Next.js compatible).
Stars: ✭ 1,219 (+1423.75%)
Mutual labels:  performance
Delta
Programming language focused on performance and productivity
Stars: ✭ 77 (-3.75%)
Mutual labels:  performance
Memorymonitor
内存监控器
Stars: ✭ 79 (-1.25%)
Mutual labels:  performance
Jsonp
Java API for JSON Processing (JSON-P)
Stars: ✭ 77 (-3.75%)
Mutual labels:  json
Mir Ion
WIP, use libmir/asdf package for now
Stars: ✭ 78 (-2.5%)
Mutual labels:  json
Perftools Runner
Google Performance Tools runner using Puppeteer
Stars: ✭ 79 (-1.25%)
Mutual labels:  performance

json_in_type

Fast json encoder in rust, that does more at compile time, and less at run time. One notable feature is the ability to encode the structure of JSON objects in their type.

This allows for a very compact representation of objects in memory, and up to an order of magnitude better performance than the traditional approach (used by serde's json! marco, for instance) where JSON objects are stored in maps.

The goal of this library is to be as close as possible to the performance and memory footprint you would get by writing the json by hand in your source code and using string formatting to insert your dynamic values.

fn write_obj_bad(value: f32) -> String { 
    format!("{{\"value\":{}}}", value)
}

// Safer, but equivalent and not less efficient :
fn write_obj_good(value: f32) -> String {
    ( json_object! { value } ).to_json_string()
}

Example use

use json_in_type::*;

fn main() {
    let void = ();
    let list = json_list![42u8, true];
    let dynamic_key = "hello";
    
    let json_val = json_object!{
        void, list,
        [dynamic_key]: "world"
    };
    /* The type of json_val is:
    
        InlinedJSONObjectEntry<
            (),
        InlinedJSONObjectEntry<
            JSONListElem<u8,
            JSONListElem<JSONtrue,
            JSONListEnd>>>,
        JSONObjectEntry<
            &str, &str,
        JSONObjectEnd>>>
    */

    assert_eq!(
        r#"{"void":null,"list":[42,true],"hello":"world"}"#,
        json_val.to_json_string()
    );
}

Memory use

The generated types have a very small memory footprint at runtime. You don't pay for the json structure, only for what you put in it !

In the next example, we store the following json structure on only two bytes:

{
  "result_count" : 1,
  "errors" : null,
  "results" : [
    {"answer":42, "ok":true}
   ]
}
fn test_memory_size() {
    let (result_count, answer) = (1u8, 42u8);
    let my_val = json_object! {
        result_count,
        errors: null,
        results: json_list![
            json_object!{answer, ok: true}
        ]
    };
    // my_val weighs only two bytes, because we stored only 2 u8 in it
    assert_eq!(2, ::std::mem::size_of_val(&my_val));
}

Performance

This library is generally faster than SERDE. Here are detailed comparison results on different json serialization tasks realized on an AMD Ryzen 5 1600X. See detailed benchmark results.

Encoding 8 nested json objects using a rust macro

We use serde's json! and json_in_type's json_object! macro to encode a nested json object.

Encoded object

We encode a JSON structure composed of 8 nested objects, each of which contains a single key, that is known at compile time. The last nested object contains an integer n that is not known at compile time.

{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"nested":{"value":n}}}}}}}}}

Benchmark result

nested json objects comparison

Encoding a very simple json object using a rust macro

Encoded object

{
        "void": null,
        "list": [1, 2, 3, 3],
        "hello": "world"
}

Benchmark result

simple object

Encoding a very simple json object using #[derive(...)]

Encoded object

{
        "void": null,
        "list": [1, 2, 3, 3],
        "hello": "world"
}

created from the following rust struct

#[derive(Serialize, JSONValue)]
struct MyObject {
    void: (),
    list: Vec<f64>,
    hello: String,
}

Benchmark result

simple object

External links

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].