{"id":13599902,"url":"https://github.com/lovasoa/json_in_type","last_synced_at":"2025-05-07T15:22:55.040Z","repository":{"id":57634638,"uuid":"151961483","full_name":"lovasoa/json_in_type","owner":"lovasoa","description":"Fast json encoder in rust, that encodes the structure of JSON values in their types ","archived":false,"fork":false,"pushed_at":"2022-08-30T02:11:25.000Z","size":14765,"stargazers_count":83,"open_issues_count":6,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-15T01:18:44.457Z","etag":null,"topics":["json","performance","rust"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/json_in_type","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lovasoa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-07T16:31:40.000Z","updated_at":"2024-11-28T16:34:06.000Z","dependencies_parsed_at":"2022-09-26T20:20:33.835Z","dependency_job_id":null,"html_url":"https://github.com/lovasoa/json_in_type","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovasoa%2Fjson_in_type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovasoa%2Fjson_in_type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovasoa%2Fjson_in_type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovasoa%2Fjson_in_type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lovasoa","download_url":"https://codeload.github.com/lovasoa/json_in_type/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252903040,"owners_count":21822360,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","performance","rust"],"created_at":"2024-08-01T17:01:16.840Z","updated_at":"2025-05-07T15:22:55.016Z","avatar_url":"https://github.com/lovasoa.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# json_in_type\n\nFast json encoder in rust, that does more at compile time, and less at run time.\nOne notable feature is the ability to encode the structure of JSON objects in their type.\n\nThis allows for a very compact representation of objects in memory, and up to an order of magnitude better performance\nthan the traditional approach (used by serde's `json!` marco, for instance) where JSON objects are stored in maps.\n\nThe goal of this library is to be as close as possible to the performance\nand memory footprint you would get by writing the json by hand in your source code\nand using string formatting to insert your dynamic values.\n\n```rust\nfn write_obj_bad(value: f32) -\u003e String { \n    format!(\"{{\\\"value\\\":{}}}\", value)\n}\n\n// Safer, but equivalent and not less efficient :\nfn write_obj_good(value: f32) -\u003e String {\n    ( json_object! { value } ).to_json_string()\n}\n```\n\n## Example use\n\n```rust\nuse json_in_type::*;\n\nfn main() {\n    let void = ();\n    let list = json_list![42u8, true];\n    let dynamic_key = \"hello\";\n    \n    let json_val = json_object!{\n        void, list,\n        [dynamic_key]: \"world\"\n    };\n    /* The type of json_val is:\n    \n        InlinedJSONObjectEntry\u003c\n            (),\n        InlinedJSONObjectEntry\u003c\n            JSONListElem\u003cu8,\n            JSONListElem\u003cJSONtrue,\n            JSONListEnd\u003e\u003e\u003e,\n        JSONObjectEntry\u003c\n            \u0026str, \u0026str,\n        JSONObjectEnd\u003e\u003e\u003e\n    */\n\n    assert_eq!(\n        r#\"{\"void\":null,\"list\":[42,true],\"hello\":\"world\"}\"#,\n        json_val.to_json_string()\n    );\n}\n```\n\n## Memory use\nThe generated types have a very small memory footprint at runtime.\nYou don't pay for the json structure, only for what you put in it !\n\nIn the next example, we store the following json structure on only two bytes:\n```json\n{\n  \"result_count\" : 1,\n  \"errors\" : null,\n  \"results\" : [\n    {\"answer\":42, \"ok\":true}\n   ]\n}\n```\n\n```rust\nfn test_memory_size() {\n    let (result_count, answer) = (1u8, 42u8);\n    let my_val = json_object! {\n        result_count,\n        errors: null,\n        results: json_list![\n            json_object!{answer, ok: true}\n        ]\n    };\n    // my_val weighs only two bytes, because we stored only 2 u8 in it\n    assert_eq!(2, ::std::mem::size_of_val(\u0026my_val));\n}\n```\n## Performance\n\nThis library is generally faster than SERDE.\nHere are detailed comparison results on different json serialization tasks realized on an  AMD Ryzen 5 1600X.\n[See detailed benchmark results.](https://lovasoa.github.io/json_in_type/docs/criterion/report/)\n\n### Encoding 8 nested json objects using a rust macro\n\nWe use serde's\n[`json!`](https://docs.serde.rs/serde_json/macro.json.html)\nand json_in_type's\n[`json_object!`](https://docs.rs/json_in_type/latest/json_in_type/macro.json_object.html)\nmacro to encode a nested json object.\n\n#### Encoded object\nWe encode a JSON structure composed of 8 nested objects, each of \nwhich contains a single key, that is known at compile time.\nThe last nested object contains an integer *n* that is not known at compile time.\n```js\n{\"nested\":{\"nested\":{\"nested\":{\"nested\":{\"nested\":{\"nested\":{\"nested\":{\"nested\":{\"value\":n}}}}}}}}}\n```\n\n#### Benchmark result\n![nested json objects comparison](https://lovasoa.github.io/json_in_type/docs/criterion/encode%20nested%20objects/report/violin.svg)\n\n### Encoding a very simple json object using a rust macro\n\n#### Encoded object\n```json\n{\n        \"void\": null,\n        \"list\": [1, 2, 3, 3],\n        \"hello\": \"world\"\n}\n```\n\n#### Benchmark result\n![simple object](https://lovasoa.github.io/json_in_type/docs/criterion/encode%20simple%20object%20with%20macro/report/violin.svg)\n\n### Encoding a very simple json object using `#[derive(...)]`\n\n#### Encoded object\n```json\n{\n        \"void\": null,\n        \"list\": [1, 2, 3, 3],\n        \"hello\": \"world\"\n}\n```\n\ncreated from the following rust struct\n\n```rust\n#[derive(Serialize, JSONValue)]\nstruct MyObject {\n    void: (),\n    list: Vec\u003cf64\u003e,\n    hello: String,\n}\n```\n\n#### Benchmark result\n![simple object](https://lovasoa.github.io/json_in_type/docs/criterion/encode%20simple%20object%20with%20derive/report/violin.svg)\n\n## External links\n\n * docs.rs hosts this crate's [api documentation](https://docs.rs/json_in_type).\n    * documentation for [the `json_object!` macro](https://docs.rs/json_in_type/latest/json_in_type/macro.json_object.html)\n    * documentation for [the `JSONValue` trait](https://docs.rs/json_in_type/latest/json_in_type/trait.JSONValue.html)\n * You can automatically derive the `JSONValue` trait for your type using the [json_in_type_derive crate](https://docs.rs/json_in_type_derive)\n * You can see [json_in_type on crates.io](https://crates.io/crates/json_in_type).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flovasoa%2Fjson_in_type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flovasoa%2Fjson_in_type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flovasoa%2Fjson_in_type/lists"}