{"id":13730360,"url":"https://github.com/dougbinks/sjson","last_synced_at":"2025-05-08T02:32:22.837Z","repository":{"id":14721581,"uuid":"17442204","full_name":"dougbinks/sjson","owner":"dougbinks","description":"sjson: no {} needed around the whole file; \"=\" is allowed instead of \":\"; quotes around the key are optional; commas after values are optional; read access through compile time hashes much faster than string compares","archived":false,"fork":true,"pushed_at":"2014-06-10T11:33:22.000Z","size":271,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-14T21:37:58.454Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"questor/sjson","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dougbinks.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":"2014-03-05T13:56:19.000Z","updated_at":"2019-09-21T12:28:22.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/dougbinks/sjson","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/dougbinks%2Fsjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dougbinks%2Fsjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dougbinks%2Fsjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dougbinks%2Fsjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dougbinks","download_url":"https://codeload.github.com/dougbinks/sjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252986965,"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.753Z","updated_at":"2025-05-08T02:32:22.456Z","avatar_url":"https://github.com/dougbinks.png","language":"C","readme":"\n  Copyright (c) 2009 Dave Gamble\n\n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files (the \"Software\"), to deal\n  in the Software without restriction, including without limitation the rights\n  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n  copies of the Software, and to permit persons to whom the Software is\n  furnished to do so, subject to the following conditions:\n\n  The above copyright notice and this permission notice shall be included in\n  all copies or substantial portions of the Software.\n\n  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n  THE SOFTWARE.\n\nWelcome to sJSON based on cJSON\n-------------------------------\n\nsjson: - no {} needed around the whole file - \"=\" is allowed instead of \":\" - quotes around the key are optional - commas after values are optional\n\nThe rest of the api-docu from cJSON:\n\nWelcome to cJSON.\n-----------------\n\ncJSON aims to be the dumbest possible parser that you can get your job done with.\nIt's a single file of C, and a single header file.\n\nJSON is described best here: http://www.json.org/\nIt's like XML, but fat-free. You use it to move data around, store things, or just\ngenerally represent your program's state.\n\nFirst up, how do I build?\nAdd cJSON.c to your project, and put cJSON.h somewhere in the header search path.\nFor example, to build the test app:\n\n    gcc cJSON.c test.c -o test -lm\n    ./test\n\nAs a library, cJSON exists to take away as much legwork as it can, but not get in your way.\nAs a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it\nin one of two modes: Auto and Manual. Let's have a quick run-through.\n\nI lifted some JSON from this page: http://www.json.org/fatfree.html\nThat page inspired me to write cJSON, which is a parser that tries to share the same\nphilosophy as JSON itself. Simple, dumb, out of the way.\n\nSome JSON:\n----------\n\n    {\n        \"name\": \"Jack (\\\"Bee\\\") Nimble\", \n        \"format\": {\n            \"type\":       \"rect\", \n            \"width\":      1920, \n            \"height\":     1080, \n            \"interlace\":  false, \n            \"frame rate\": 24\n        }\n    }\n\nAssume that you got this from a file, a webserver, or magic JSON elves, whatever,\nyou have a char * to it. Everything is a cJSON struct.\nGet it parsed:\n\n  cJSON * root = cJSON_Parse(my_json_string);\n\nThis is an object. We're in C. We don't have objects. But we do have structs.\nWhat's the framerate?\n\n    cJSON * format = cJSON_GetObjectItem(root,\"format\");\n    int framerate = cJSON_GetObjectItem(format,\"frame rate\")-\u003evalueint;\n\nWant to change the framerate?\n\n    cJSON_GetObjectItem(format,\"frame rate\")-\u003evalueint = 25;\n\nBack to disk?\n\n    char * rendered = cJSON_Print(root);\n\nFinished? Delete the root (this takes care of everything else).\n\n    cJSON_Delete(root);\n\nThat's AUTO mode. If you're going to use Auto mode, you really ought to check pointers\nbefore you dereference them. If you want to see how you'd build this struct in code?\n\n    cJSON *root,*fmt;\n    root = cJSON_CreateObject();  \n    cJSON_AddItemToObject(root, \"name\", cJSON_CreateString(\"Jack (\\\"Bee\\\") Nimble\"));\n    cJSON_AddItemToObject(root, \"format\", fmt = cJSON_CreateObject());\n    cJSON_AddStringToObject(fmt, \"type\", \"rect\");\n    cJSON_AddNumberToObject(fmt, \"width\", 1920);\n    cJSON_AddNumberToObject(fmt, \"height\", 1080);\n    cJSON_AddFalseToObject (fmt, \"interlace\");\n    cJSON_AddNumberToObject(fmt, \"frame rate\", 24);\n\nHopefully we can agree that's not a lot of code? There's no overhead, no unnecessary setup.\nLook at test.c for a bunch of nice examples, mostly all ripped off the json.org site, and\na few from elsewhere.\n\nWhat about manual mode? First up you need some detail.\nLet's cover how the cJSON objects represent the JSON data.\ncJSON doesn't distinguish arrays from objects in handling; just type.\nEach cJSON has, potentially, a child, siblings, value, a name.\n\nThe root object has: Object Type and a Child\nThe Child has name \"name\", with value \"Jack (\"Bee\") Nimble\", and a sibling:\nSibling has type Object, name \"format\", and a child.\nThat child has type String, name \"type\", value \"rect\", and a sibling:\nSibling has type Number, name \"width\", value 1920, and a sibling:\nSibling has type Number, name \"height\", value 1080, and a sibling:\nSibling hs type False, name \"interlace\", and a sibling:\nSibling has type Number, name \"frame rate\", value 24\n\nHere's the structure:\n---------------------\n\n    typedef struct cJSON {\n      struct cJSON *next,*prev;\n      struct cJSON *child;\n\n      int type;\n\n      char *valuestring;\n      int valueint;\n      double valuedouble;\n\n      char *string;\n    } cJSON;\n\nBy default all values are 0 unless set by virtue of being meaningful.\n\nnext/prev is a doubly linked list of siblings. next takes you to your sibling,\nprev takes you back from your sibling to you.\nOnly objects and arrays have a \"child\", and it's the head of the doubly linked list.\nA \"child\" entry will have prev==0, but next potentially points on. The last sibling has next=0.\nThe type expresses Null/True/False/Number/String/Array/Object, all of which are #defined in\ncJSON.h\n\nA Number has valueint and valuedouble. If you're expecting an int, read valueint, if not read\nvaluedouble.\n\nAny entry which is in the linked list which is the child of an object will have a \"string\"\nwhich is the \"name\" of the entry. When I said \"name\" in the above example, that's \"string\".\n\"string\" is the JSON name for the 'variable name' if you will.\n\nNow you can trivially walk the lists, recursively, and parse as you please.\nYou can invoke cJSON_Parse to get cJSON to parse for you, and then you can take\nthe root object, and traverse the structure (which is, formally, an N-tree),\nand tokenise as you please. If you wanted to build a callback style parser, this is how\nyou'd do it (just an example, since these things are very specific):\n\n    void parse_and_callback(cJSON *item,const char *prefix)\n    {\n      while (item)\n      {\n        char *newprefix = malloc(strlen(prefix) + strlen(item-\u003ename) + 2);\n        sprintf(newprefix,\"%s/%s\",prefix,item-\u003ename);\n        int dorecurse = callback(newprefix, item-\u003etype, item);\n        if (item-\u003echild \u0026\u0026 dorecurse) parse_and_callback(item-\u003echild, newprefix);\n        item = item-\u003enext;\n        free(newprefix);\n      }\n    }\n\nThe prefix process will build you a separated list, to simplify your callback handling.\nThe 'dorecurse' flag would let the callback decide to handle sub-arrays on it's own, or\nlet you invoke it per-item. For the item above, your callback might look like this:\n\n    int callback(const char *name,int type,cJSON *item)\n    {\n      if (!strcmp(name,\"name\"))  { /* populate name */ }\n      else if (!strcmp(name,\"format/type\")  { /* handle \"rect\" */ }\n      else if (!strcmp(name,\"format/width\")  { /* 800 */ }\n      else if (!strcmp(name,\"format/height\")  { /* 600 */ }\n      else if (!strcmp(name,\"format/interlace\")  { /* false */ }\n      else if (!strcmp(name,\"format/frame rate\")  { /* 24 */ }\n      return 1;\n    }\n\nAlternatively, you might like to parse iteratively.\nYou'd use:\n\n    void parse_object(cJSON *item)\n    {\n      int i;\n      for (i = 0 ; i \u003c cJSON_GetArraySize(item) ; i++)\n      {\n        cJSON * subitem = cJSON_GetArrayItem(item, i);\n        // handle subitem.  \n      }\n    }\n\nOr, for PROPER manual mode:\n\n    void parse_object(cJSON * item)\n    {\n      cJSON *subitem = item-\u003echild;\n      while (subitem)\n      {\n        // handle subitem\n        if (subitem-\u003echild) parse_object(subitem-\u003echild);\n    \n        subitem = subitem-\u003enext;\n      }\n    }\n\nOf course, this should look familiar, since this is just a stripped-down version\nof the callback-parser.\n\nThis should cover most uses you'll find for parsing. The rest should be possible\nto infer.. and if in doubt, read the source! There's not a lot of it! ;)\n\nIn terms of constructing JSON data, the example code above is the right way to do it.\nYou can, of course, hand your sub-objects to other functions to populate.\nAlso, if you find a use for it, you can manually build the objects.\nFor instance, suppose you wanted to build an array of objects?\n\n    cJSON * objects[24];\n\n    cJSON * Create_array_of_anything(cJSON ** items, int num)\n    {\n      int i;\n      cJSON * prev, * root = cJSON_CreateArray();\n      for (i = 0 ; i \u003c 24 ; i++)\n      {\n        if (!i) root-\u003echild = objects[i];\n        else prev-\u003enext = objects[i], objects[i]-\u003eprev = prev;\n        prev = objects[i];\n      }\n      return root;\n    }\n  \nand simply: Create_array_of_anything(objects, 24);\n\ncJSON doesn't make any assumptions about what order you create things in.\nYou can attach the objects, as above, and later add children to each\nof those objects.\n\nAs soon as you call cJSON_Print, it renders the structure to text.\n\nThe test.c code shows how to handle a bunch of typical cases. If you uncomment\nthe code, it'll load, parse and print a bunch of test files, also from json.org,\nwhich are more complex than I'd care to try and stash into a const char array[].\n\nEnjoy cJSON!\n-----------------------\n\n- Dave Gamble, Aug 2009","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdougbinks%2Fsjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdougbinks%2Fsjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdougbinks%2Fsjson/lists"}