{"id":22813322,"url":"https://github.com/southclaws/pawn-json","last_synced_at":"2025-04-22T16:53:35.786Z","repository":{"id":55052999,"uuid":"250832814","full_name":"Southclaws/pawn-json","owner":"Southclaws","description":"JSON for Pawn.","archived":false,"fork":false,"pushed_at":"2025-03-22T12:42:09.000Z","size":54,"stargazers_count":20,"open_issues_count":4,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T16:51:13.308Z","etag":null,"topics":["json","pawn-package"],"latest_commit_sha":null,"homepage":null,"language":"Rust","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/Southclaws.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":"2020-03-28T15:42:45.000Z","updated_at":"2025-03-22T12:42:13.000Z","dependencies_parsed_at":"2025-01-16T20:36:49.168Z","dependency_job_id":"11464914-e9e6-4422-83e0-7eb69e35c4d4","html_url":"https://github.com/Southclaws/pawn-json","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fpawn-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fpawn-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fpawn-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fpawn-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Southclaws","download_url":"https://codeload.github.com/Southclaws/pawn-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250283247,"owners_count":21405126,"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","pawn-package"],"created_at":"2024-12-12T12:26:36.068Z","updated_at":"2025-04-22T16:53:35.759Z","avatar_url":"https://github.com/Southclaws.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pawn-json\n\n[![GitHub](https://shields.southcla.ws/badge/sampctl-pawn--json-2f2f2f.svg?style=for-the-badge)](https://github.com/Southclaws/pawn-json)\n\nThis package provides an API for parsing from and serialising to JSON.\n\n## Installation\n\nSimply install to your project:\n\n```bash\nsampctl package install Southclaws/pawn-json\n```\n\nInclude in your code and begin using the library:\n\n```pawn\n#include \u003cjson\u003e\n```\n\n## Usage\n\nIf you don't already know what JSON is, a good place to start is\n[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON).\nIt's pretty much a web API standard nowadays (Twitter, Discord, GitHub and just\nabout every other API uses it to represent data). I'll briefly go over it before\ngetting into the API.\n\nThis plugin stores JSON values as \"Nodes\". Each node represents a value of one\ntype. Here are some examples of the representations of different node types:\n\n- `{}` - Object that is empty\n- `{\"key\": \"value\"}` - Object with one key that points to a String node\n- `\"hello\"` - String\n- `1` - Number (integer)\n- `1.5` - Number (floating point)\n- `[1, 2, 3]` - Array, of Number nodes\n- `[{}, {}]` - Array of empty Object nodes\n- `true` - Boolean\n\nThe main point here is that everything is a node, even Objects and Arrays that\ncontain other nodes.\n\n### Building an Object\n\nTo build a JSON object, you most likely want to start with `JSON_Object` however\nyou can use any node as the root node, it depends on where you're sending the\ndata but for this example I'll use an Object as the root node.\n\n```pawn\nnew Node:node = JSON_Object();\n```\n\nThis just constructs an empty object and if you \"stringify\" it (stringify simply\nmeans to turn into a string) you get:\n\n```json\n{}\n```\n\nSo to add more nodes to this object, simply add parameters, as key-value pairs:\n\n```pawn\nnew Node:node = JSON_Object(\n    \"key\", JSON_String(\"value\")\n);\n```\n\nThis would stringify as:\n\n```json\n{\n  \"key\": \"value\"\n}\n```\n\nYou can nest objects within objects too:\n\n```pawn\nnew Node:node = JSON_Object(\n    \"key\", JSON_Object(\n        \"key\", JSON_String(\"value\")\n    )\n);\n```\n\n```json\n{\n  \"key\": {\n    \"key\": \"value\"\n  }\n}\n```\n\nAnd do arrays of any node:\n\n```pawn\nnew Node:node = JSON_Object(\n    \"key\", JSON_Array(\n        JSON_String(\"one\"),\n        JSON_String(\"two\"),\n        JSON_String(\"three\"),\n        JSON_Object(\n            \"more_stuff1\", JSON_String(\"uno\"),\n            \"more_stuff2\", JSON_String(\"dos\"),\n            \"more_stuff3\", JSON_String(\"tres\")\n        )\n    )\n);\n```\n\nSee the\n[unit tests](https://github.com/Southclaws/pawn-json/blob/master/test.pwn)\nfor more examples of JSON builders.\n\n#### Accessing Data\n\nWhen you get JSON data, it's provided as a `Node:` in the callback. Most of\nthe time, you'll get an object back but depending on the application that\nresponded this could differ.\n\nLets assume you have the following data:\n\n```json\n{\n  \"name\": \"Southclaws\",\n  \"score\": 45,\n  \"vip\": true,\n  \"inventory\": [\n    {\n      \"name\": \"M4\",\n      \"ammo\": 341\n    },\n    {\n      \"name\": \"Desert Eagle\",\n      \"ammo\": 32\n    }\n  ]\n}\n```\n\n```pawn\npublic OnSomeResponse(Node:json) {\n    new ret;\n\n    new name[MAX_PLAYER_NAME];\n    ret = JSON_GetString(node, \"name\", name);\n    if(ret) {\n        err(\"failed to get name, error: %d\", ret);\n        return 1;\n    }\n\n    new score;\n    ret = JSON_GetInt(node, \"score\", score);\n    if(ret) {\n        err(\"failed to get score, error: %d\", ret);\n        return 1;\n    }\n\n    new bool:vip;\n    ret = JSON_GetBool(node, \"vip\", vip);\n    if(ret) {\n        err(\"failed to get vip, error: %d\", ret);\n        return 1;\n    }\n\n    new Node:inventory;\n    ret = JSON_GetArray(node, \"inventory\", inventory);\n    if(ret) {\n        err(\"failed to get inventory, error: %d\", ret);\n        return 1;\n    }\n\n    new length;\n    ret = JSON_ArrayLength(inventory, length);\n    if(ret) {\n        err(\"failed to get inventory array length, error: %d\", ret);\n        return 1;\n    }\n\n    for(new i; i \u003c length; ++i) {\n        new Node:item;\n        ret = JSON_ArrayObject(inventory, i, item);\n        if(ret) {\n            err(\"failed to get inventory item %d, error: %d\", i, ret);\n            return 1;\n        }\n\n        new itemName[32];\n        ret = JSON_GetString(item, \"name\", itemName);\n        if(ret) {\n            err(\"failed to get inventory item %d, error: %d\", i, ret);\n            return 1;\n        }\n\n        new itemAmmo;\n        ret = JSON_GetInt(item, \"name\", itemAmmo);\n        if(ret) {\n            err(\"failed to get inventory item %d, error: %d\", i, ret);\n            return 1;\n        }\n\n        printf(\"item %d name: %s ammo: %d\", itemName, itemAmmo);\n    }\n\n    return 0;\n}\n```\n\nIn this example, we extract each field from the JSON object with full error\nchecking. This example shows usage of object and array access as well as\nprimitives such as strings, integers and a boolean.\n\nIf you're not a fan of the overly terse and explicit error checking, you can\nalternatively just check your errors at the end but this will mean you won't\nknow exactly _where_ an error occurred, just that it did.\n\n```pawn\nnew ret;\nret += JSON_GetString(node, \"key1\", value1);\nret += JSON_GetString(node, \"key2\", value2);\nret += JSON_GetString(node, \"key3\", value3);\nif(ret) {\n    err(\"some error occurred: %d\", ret);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsouthclaws%2Fpawn-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsouthclaws%2Fpawn-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsouthclaws%2Fpawn-json/lists"}