{"id":13652732,"url":"https://github.com/lcsmuller/json-build","last_synced_at":"2025-10-23T04:31:24.866Z","repository":{"id":42045022,"uuid":"452311367","full_name":"lcsmuller/json-build","owner":"lcsmuller","description":"Tiny, zero-allocation JSON serializer written in ANSI C","archived":false,"fork":false,"pushed_at":"2024-12-07T18:16:22.000Z","size":95,"stargazers_count":39,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-30T17:28:12.239Z","etag":null,"topics":["ansi","c89","embedded","json","serializer","stack","zero-allocation"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lcsmuller.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":"2022-01-26T14:39:58.000Z","updated_at":"2025-01-18T10:06:37.000Z","dependencies_parsed_at":"2022-08-12T03:11:10.353Z","dependency_job_id":null,"html_url":"https://github.com/lcsmuller/json-build","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcsmuller%2Fjson-build","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcsmuller%2Fjson-build/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcsmuller%2Fjson-build/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcsmuller%2Fjson-build/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lcsmuller","download_url":"https://codeload.github.com/lcsmuller/json-build/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237780141,"owners_count":19365132,"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":["ansi","c89","embedded","json","serializer","stack","zero-allocation"],"created_at":"2024-08-02T02:01:02.127Z","updated_at":"2025-10-23T04:31:24.847Z","avatar_url":"https://github.com/lcsmuller.png","language":"C","readme":"# JSON-BUILD\n\njson-build is a zero-allocation JSON serializer written in ANSI C. Its\ntokenizer counterpart can be found at\n[jsmn-find](https://github.com/lcsmuller/jsmn-find).\n\n## Features\n\n* compatible with C89\n* no dependencies\n* no dynamic memory allocation\n\n## Usage\n\nDownload `json-build.h`, include it, done.\n\n```c\n#include \"json-build.h\"\n\n...\njsonb b;\nchar buf[1024];\n\njsonb_init(\u0026b);\njsonb_object(\u0026b, buf, sizeof(buf));\n{\n    jsonb_key(\u0026b, buf, sizeof(buf), \"foo\", strlen(\"foo\"));\n    jsonb_array(\u0026b, buf, sizeof(buf));\n    {\n        jsonb_number(\u0026b, buf, sizeof(buf), 1);\n        jsonb_string(\u0026b, buf, sizeof(buf), \"hi\", 2);\n        jsonb_bool(\u0026b, buf, sizeof(buf), 0);\n        jsonb_null(\u0026b, buf, sizeof(buf));\n        jsonb_array_pop(\u0026b, buf, sizeof(buf));\n    }\n    jsonb_object_pop(\u0026b, buf, sizeof(buf));\n}\nprintf(\"JSON: %s\", buf); // JSON: {\"foo\":[1,\"hi\",false,null]}\n```\n\n## Automatic Buffer Management\n\nFor dynamic buffer management, json-build provides `_auto` counterparts for all serializer functions.\nThese functions will automatically reallocate the buffer when more space is needed:\n\n```c\n#include \"json-build.h\"\n#include \u003cstdlib.h\u003e\n\n...\njsonb b;\nchar *buf = malloc(64); // Initial small buffer\nsize_t bufsize = 64;\n\njsonb_init(\u0026b);\njsonb_object_auto(\u0026b, \u0026buf, \u0026bufsize); // Note: passing pointers to buffer and size\n{\n    jsonb_key_auto(\u0026b, \u0026buf, \u0026bufsize, \"foo\", strlen(\"foo\"));\n    jsonb_array_auto(\u0026b, \u0026buf, \u0026bufsize);\n    {\n        jsonb_number_auto(\u0026b, \u0026buf, \u0026bufsize, 1);\n        jsonb_string_auto(\u0026b, \u0026buf, \u0026bufsize, \"hi\", 2);\n        jsonb_bool_auto(\u0026b, \u0026buf, \u0026bufsize, 0);\n        jsonb_null_auto(\u0026b, \u0026buf, \u0026bufsize);\n        jsonb_array_pop_auto(\u0026b, \u0026buf, \u0026bufsize);\n    }\n    jsonb_object_pop_auto(\u0026b, \u0026buf, \u0026bufsize);\n}\nprintf(\"JSON: %s (buffer size: %zu)\\n\", buf, bufsize);\nfree(buf);\n```\n\n**IMPORTANT**: Do not mix regular and `_auto` functions on the same buffer. Always use either the regular functions with a fixed-size buffer or the `_auto` functions with a dynamically allocated buffer throughout your code.\n\nSince json-build is a single-header, header-only library, for more complex use\ncases you might need to define additional macros. `#define JSONB_STATIC`hides all\njson-build API symbols by making them static. Also, if you want to include `json-build.h`\nfor multiple C files, to avoid duplication of symbols you may define `JSONB_HEADER` macro.\n\n```c\n/* In every .c file that uses json-build include only declarations: */\n#define JSONB_HEADER\n#include \"json-build.h\"\n\n/* Additionally, create one json-build.c file for json-build implementation: */\n#include \"json-build.h\"\n```\n\n## API\n\n* `jsonb_init()` - initialize a jsonb handle\n* `jsonb_reset()` - reset the buffer's position tracker for streaming purposes\n* `jsonb_object()` - push an object to the builder stack\n* `jsonb_object_pop()` - pop an object from the builder stack\n* `jsonb_key()` - push an object key field to the builder stack\n* `jsonb_array()` - push an array to the builder stack\n* `jsonb_array_pop()` - pop an array from the builder stack\n* `jsonb_token()` - push a raw token to the builder stack\n* `jsonb_bool()` - push a boolean token to the builder stack\n* `jsonb_null()` - push a null token to the builder stack\n* `jsonb_string()` - push a string token to the builder stack\n* `jsonb_number()` - push a number token to the builder stack\n\nThe following are the possible return codes for the builder functions:\n* `JSONB_OK` - operation was a success, user can proceed with the next operation\n* `JSONB_END` - operation was a success, JSON is complete and expects no more operations\n* `JSONB_ERROR_NOMEM` - buffer is not large enough, or `_auto` function couldn't reallocate\n* `JSONB_ERROR_INPUT` - user action don't match expected next token\n* `JSONB_ERROR_STACK` - user action would lead to out of boundaries access, increase `JSONB_MAX_DEPTH`!\n* `JSONB_ERROR_OVERFLOW` - automatic buffer increase would lead to an overflow, only use with `_auto` functions\n\nIts worth mentioning that all `JSONB_ERROR_` prefixed codes are negative.\n\nIf you get `JSONB_ERROR_NOMEM` you can either:\n1. re-allocate a larger buffer and call the builder function once more\n2. call `jsonb_reset()` to reset the buffer's position tracker and call the builder function once more (useful for streaming with a fixed sized buffer!)\n\n## Other info\n\nThis software is distributed under [MIT license](www.opensource.org/licenses/mit-license.php),\nso feel free to integrate it in your commercial products.\n","funding_links":[],"categories":["JSON","Libraries","Packages, Libraries and RTOSes","C Libraries"],"sub_categories":["Serialization"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flcsmuller%2Fjson-build","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flcsmuller%2Fjson-build","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flcsmuller%2Fjson-build/lists"}