{"id":13514734,"url":"https://github.com/Stellaris-code/smoljson","last_synced_at":"2025-03-31T03:31:16.630Z","repository":{"id":84199965,"uuid":"273267191","full_name":"Stellaris-code/smoljson","owner":"Stellaris-code","description":"Blazing fast and light SIMD JSON parser in a few hundreds lines of C Code","archived":false,"fork":false,"pushed_at":"2020-06-27T13:09:23.000Z","size":2016,"stargazers_count":9,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-01T18:37:48.327Z","etag":null,"topics":["avx","c","fast","json","json-parse","json-parser","light","lightweight","performance","simd","sse"],"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/Stellaris-code.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}},"created_at":"2020-06-18T14:57:56.000Z","updated_at":"2022-12-09T02:28:06.000Z","dependencies_parsed_at":"2023-05-23T22:45:09.839Z","dependency_job_id":null,"html_url":"https://github.com/Stellaris-code/smoljson","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stellaris-code%2Fsmoljson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stellaris-code%2Fsmoljson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stellaris-code%2Fsmoljson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stellaris-code%2Fsmoljson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Stellaris-code","download_url":"https://codeload.github.com/Stellaris-code/smoljson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246413377,"owners_count":20773053,"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":["avx","c","fast","json","json-parse","json-parser","light","lightweight","performance","simd","sse"],"created_at":"2024-08-01T05:01:01.046Z","updated_at":"2025-03-31T03:31:12.619Z","avatar_url":"https://github.com/Stellaris-code.png","language":"C","readme":"# smoljson\nBlazing fast and light SIMD JSON parser in a few hundreds lines of C Code\n\nParsing up to 4 GB of JSON data per second!\n\n## What's smoljson?\nsmoljson is a single small .c file, single header C library aiming to parse JSON as fast as possible using x86 SIMD instruction sets up to AVX2 if available.\n\nIt's fully compliant to the JSON spec, tested using https://github.com/nst/JSONTestSuite, and has mostly on-par (sometimes better!) performance with the largest SIMD json parser library, simdjson (https://github.com/simdjson/simdjson).\n\nIt can also print JSON values to a string buffer, or create and manipualte JSON objects like every JSON library worthy of this name.\n\nsmoljson can work in zero-allocation mode : all you have to do, is supply buffers to the parsing function. Said buffers can be dynamically reallocated if you supply the matching callback.\n\nsmoljson also supports the JSON Pointer syntax to access data as described in https://tools.ietf.org/html/rfc6901 .\n\nsmoljson can also be thread safe if you uncomment the following define in json.c : \"//#define JSON_STACK_WORKING_MEMORY\"\n\n## How to use it\n\nThe interface : \n```c\ntypedef union json_number_t\n{\n    double num_real;\n    uint64_t num_int;\n} json_number_t;\n\ntypedef struct json_value_t\n{\n    enum\n    {\n        JSON_OBJECT,\n        JSON_ARRAY,\n        JSON_STRING,\n        JSON_NUMBER_INT,\n        JSON_NUMBER_FLOAT,\n        JSON_BOOL,\n        JSON_NULL\n    } type;\n    union\n    {\n        json_object_t object;\n        json_array_t array;\n        struct\n        {\n            unsigned int str_idx;\n            unsigned int str_len;\n        };\n        json_number_t num;\n        bool boolean;\n    };\n} json_value_t;\n\ntypedef struct json_context_t\n{\n    char* string_buffer; unsigned int string_buffer_size; realloc_callback_t* string_realloc;\n    json_key_value_t* key_val_buffer; unsigned int key_val_buffer_size; realloc_callback_t* key_val_realloc;\n    unsigned int string_buffer_brk; unsigned int key_val_buffer_brk;\n} json_context_t;\n```\n```c\njson_result_t parse_json        (const char* input, int input_size, json_context_t* context);\nunsigned int  print_json        (json_value_t* value, json_context_t* context, bool pretty, char* buffer, unsigned int buffer_size);\njson_value_t *json_pointer      (const char* pointer, json_value_t* root, json_context_t* context);\nconst char   *json_get_string   (json_value_t* val, json_context_t* context);\njson_value_t *json_create_string(const char* str, json_context_t* context);\njson_value_t *json_create_value (json_context_t* context, uint8_t type);\nvoid          json_object_add   (json_context_t* context, json_value_t* object, const char* key, json_value_t* value);\nvoid          json_array_add    (json_context_t* context, json_value_t* object, json_value_t* value);\n```\n\nParsing a json file:\n```c\n  #define KEYVAL_SIZE (65536*256)\n  #define STRING_SIZE (65536*256)\n\n  json_key_value_t keyval_buf[KEYVAL_SIZE];\n  char string_buf[STRING_SIZE];\n\n  const uint8_t* read_buffer = read_file(\"your_file.json\");\n  \n  json_context_t context;\n  context.string_buffer = string_buf; context.string_buffer_size = STRING_SIZE; context.string_realloc = NULL; // no dynamic reallocation\n  context.key_val_buffer = keyval_buf, context.key_val_buffer_size = KEYVAL_SIZE; context.key_val_realloc = NULL;\n\n  json_result_t result = parse_json(data, len, \u0026context);\n  if (!result.accepted)\n  {\n      printf(\"error : %s\\n\", result.error.reason);\n  }\n```\nPrinting it: \n```c\n    static char print_buf[65536*128];\n    print_json(\u0026result.value, \u0026context, false, print_buf, 65536*128);\n    printf(\"out: %s\\n\", print_buf);\n```\n\nManipulating json data:\n```c\n  json_value_t* value = json_pointer(\"/example_key\", \u0026result.value, \u0026context);\n  printf(\"value : %s\\n\", json_get_string(value, \u0026context);\n  \n  json_value_t* root = json_create_value(context, JSON_OBJECT);\n  json_object_add(context, root, \"hello world key\", json_create_string(\"hello world!\", context));\n  static char print_buf[65536*128];\n  print_json(\u0026result.value, \u0026context, false, print_buf, 65536*128);\n  printf(\"out: %s\\n\", print_buf);\n  /* prints :\n  * {\n  * \"hello world key\" : \"hello world!\"\n  * }\n  */\n```\n\n## Performance\n\nBenchmarks performed on Windows 10 with GCC 5.1.0 (-O3, -march=native) on an Intel 4690K @ 3.9GHz.\n\nValues are parsing bandwith in MB/s, tests were performed with the test .json files in the test_files/ subfolder.\n\n![bench1](https://i.imgur.com/mMadFKm.png)\n![bench2](https://i.imgur.com/cOsuTck.png)\n![bench3](https://i.imgur.com/6r11wlg.png)\n","funding_links":[],"categories":["C"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStellaris-code%2Fsmoljson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FStellaris-code%2Fsmoljson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStellaris-code%2Fsmoljson/lists"}