{"id":13730378,"url":"https://github.com/yak32/glw_json","last_synced_at":"2025-08-25T20:20:41.659Z","repository":{"id":81870837,"uuid":"58681627","full_name":"yak32/glw_json","owner":"yak32","description":"Single-header fast and compact C++ library for JSON serialization, no memory allocation during parsing.","archived":false,"fork":false,"pushed_at":"2022-12-29T17:52:16.000Z","size":601,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-14T21:37:58.859Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yak32.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-12T22:31:43.000Z","updated_at":"2024-01-12T18:08:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"80dfb54f-1519-4d09-86b0-75657d729d28","html_url":"https://github.com/yak32/glw_json","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yak32%2Fglw_json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yak32%2Fglw_json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yak32%2Fglw_json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yak32%2Fglw_json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yak32","download_url":"https://codeload.github.com/yak32/glw_json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252986966,"owners_count":21836266,"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":[],"created_at":"2024-08-03T02:01:13.948Z","updated_at":"2025-05-08T02:32:25.346Z","avatar_url":"https://github.com/yak32.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"# glw_json  ![](https://github.com/yak32/glw_json/workflows/status/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/yak32/glw_json/badge.svg)](https://coveralls.io/github/yak32/glw_json) [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)\nHeader-only, cross-platform and compact json serialization for C++ with no memory allocations for parsing.\n\n# Usage\nJust drop the header (glw_json.h) into your C++ project and include it into a source file\n\n```C++\n#include \"glw_json.h\"\n```\n\n# License\nLicensed under [BSD](https://opensource.org/licenses/BSD-2-Clause) to simplify the usage.\n\n### How to build and run tests\n\nInstall cmake\n\n```\ngit clone https://github.com/yak32/glw_json.git\ncd glw_json\ngit submodule update --init\nmkdir build\ncd build\ncmake ..\ncmake --build . \n ./../tests/tests\n```\n(use Git Bash on Windows)\n\n### Example\n```c++\n#include \"glw_json.h\"\n\nusing namespace json;\n\nstruct vec3{\n  float x,y,z;\n};\n\ntemplate\u003ctypename T\u003e\nbool serialize(T\u0026 t, vec3\u0026 v){\n\tSERIALIZE(x);\n\tSERIALIZE(y);\n\tSERIALIZE(z);\n\treturn true;\n}\n\nint main(){\n\n\tvec3 v;\n\tif (load_object_from_file(\"test.json\", v) != JSON_OK)\n\t\treturn 0;\n\n\tif (save_object_to_file(\"test2.json\", v) != JSON_OK)\n\t\treturn 0;\n\n\treturn 1;\n}\n```\n\nTo avoid hash maps with allocations, values should be serialized in alphabetical order.\nBTW, initial implementation with hash map was 2x times slower (for average sized json object with around 50 properties)\nTo sort properties inside of serialize function - With Sublime Text 2/3: select everything inside serialize() and press F9 - it will sort properties automatically.\n\n### Dependencies\n STL, c-runtime.\n\n### Performance\nJSON loading - allocation-free parsing, N Log(N) performance where N is number of properties in a JSON object.\n   Parsing speed is comparable to RapidJSON, saving is slower.\n\nSaving to JSON  - linear performance, std::ofstream is used to save data ( some allocations )\n\nbenchmark.cpp contains some logic to compare performance with RapidJson (currently parsing and loading only). Current results:\n\nRun on 10 year old PC (8 X 3342 MHz CPU s) - glw_json is 25% slower than Rapid JSON for parsing:\n\n| Benchmark        | Time          | CPU      | Iterations  |\n| ---------------- |:-------------:| --------:| --------:   |\n| BM_glw_json_load | 51367 ns      | 51618 ns | 11200       |\n| BM_glw_json_save | 306253 ns     |299944 ns | 2240        |\n| BM_rapid_json_load | 40850 ns    | 40981 ns | 17920       |\n\n### Tests\n Run cmake on CMakeLists.txt in tests folder. Tests require C++11 support to compile (glw_json.h doesn't).\n\n### Compartibility\n  Tested with Visual Studio 2013, 2015, GCC 4.3.6 to GCC 7.0 (HEAD), Clang 3.0 to 3.9.0 (trunc), compartible with C++98, C++11 and C++14.\n\n### More examples\n```c++\n#include \"glw_json.h\"\n#include \u003csstream\u003e\n\nusing namespace json;\nusing namespace std;\n\nstruct array_int_var {\n\tstd::vector\u003cint\u003e val;\n};\ntemplate \u003ctypename T\u003e bool serialize(T\u0026 t, array_int_var\u0026 v) {\n\treturn SERIALIZE(val);\n}\n\nstruct obj_array_var1 {\n\tint val1;\n\tfloat val2;\n};\n\nstruct obj_array_var2 {\n\tint val1;\n\tfloat val2;\n\tvector\u003cobj_array_var1\u003e val3;\n};\n\nstruct object_pointer_in_object {\n\tint val1;\n\tfloat val2;\n\tobj_array_var1* val3;\n};\n\ntemplate \u003ctypename T\u003e bool serialize(T\u0026 t, obj_array_var1\u0026 v) {\n\tbool b = true;\n\tb \u0026= SERIALIZE(val1);\n\tb \u0026= SERIALIZE(val2);\n\treturn b;\n}\ntemplate \u003ctypename T\u003e bool serialize(T\u0026 t, obj_array_var2\u0026 v) {\n\tbool b = true;\n\tb \u0026= SERIALIZE(val1);\n\tb \u0026= SERIALIZE(val2);\n\tb \u0026= SERIALIZE(val3);\n\treturn b;\n}\n\ntemplate \u003ctypename T\u003e bool serialize(T\u0026 t, object_pointer_in_object\u0026 v) {\n\tbool b = true;\n\tb \u0026= SERIALIZE(val1);\n\tb \u0026= SERIALIZE(val2);\n\tb \u0026= SERIALIZE(val3);\n\treturn b;\n}\n\nint main(){\n\tvec3 v;\n\n\t// array\n\tstringstream ss;\n\tarray_int_var av;\n\tav.val = {1, 2, 3};\n\n\tif (save_object_to_stream(av, ss) != JSON_OK )\n\t\tprintf(\"failed\");\n\n\t// array of objects\n\tstringstream sss;\n\tobj_array_var2 oav;\n\toav.val1 = 1;\n\toav.val2 = 1.1f;\n\toav.val3.resize(1);\n\toav.val3[0].val1 = 1;\n\toav.val3[0].val2 = 1.1f;\n\n\tif (JSON_OK != save_object_to_stream(oav, sss))\n\t\tprintf(\"failed\");\n\n\tobject_pointer_in_object obj_in_obj;\n\tif (JSON_OK != load_object_from_file(\"object in object test\", obj_in_obj))\n\t\tprintf(\"failed\");\n\n\treturn 1;\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyak32%2Fglw_json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyak32%2Fglw_json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyak32%2Fglw_json/lists"}