{"id":13817042,"url":"https://github.com/near/assemblyscript-json","last_synced_at":"2025-08-10T07:38:21.371Z","repository":{"id":38426287,"uuid":"164299726","full_name":"near/assemblyscript-json","owner":"near","description":"JSON encoder / decoder for AssemblyScript","archived":false,"fork":false,"pushed_at":"2023-12-21T23:06:04.000Z","size":652,"stargazers_count":178,"open_issues_count":44,"forks_count":29,"subscribers_count":20,"default_branch":"main","last_synced_at":"2025-05-15T19:40:33.086Z","etag":null,"topics":["assemblyscript","json","webassembly"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/near.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}},"created_at":"2019-01-06T11:15:03.000Z","updated_at":"2025-03-24T14:49:45.000Z","dependencies_parsed_at":"2023-12-22T01:07:16.145Z","dependency_job_id":"c8b9e125-3cec-4043-b9d1-521b320416a4","html_url":"https://github.com/near/assemblyscript-json","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/near/assemblyscript-json","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/near%2Fassemblyscript-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/near%2Fassemblyscript-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/near%2Fassemblyscript-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/near%2Fassemblyscript-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/near","download_url":"https://codeload.github.com/near/assemblyscript-json/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/near%2Fassemblyscript-json/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269693451,"owners_count":24460233,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["assemblyscript","json","webassembly"],"created_at":"2024-08-04T06:00:32.181Z","updated_at":"2025-08-10T07:38:21.351Z","avatar_url":"https://github.com/near.png","language":"TypeScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"# assemblyscript-json\n\n![npm version](https://img.shields.io/npm/v/assemblyscript-json) ![npm downloads per month](https://img.shields.io/npm/dm/assemblyscript-json)\n\nJSON encoder / decoder for AssemblyScript.\n\nSpecial thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing infra for AssemblyScript.\n\n## Installation\n\n`assemblyscript-json` is available as a [npm package](https://www.npmjs.com/package/assemblyscript-json). You can install `assemblyscript-json` in your AssemblyScript project by running:\n\n`npm install --save assemblyscript-json`\n\n## Usage\n\n### Parsing JSON\n\n```typescript\nimport { JSON } from \"assemblyscript-json\"; \n\n// Parse an object using the JSON object\nlet jsonObj: JSON.Obj = \u003cJSON.Obj\u003e(JSON.parse('{\"hello\": \"world\", \"value\": 24}'));\n\n// We can then use the .getX functions to read from the object if you know it's type\n// This will return the appropriate JSON.X value if the key exists, or null if the key does not exist\nlet worldOrNull: JSON.Str | null = jsonObj.getString(\"hello\"); // This will return a JSON.Str or null\nif (worldOrNull != null) {\n  // use .valueOf() to turn the high level JSON.Str type into a string\n  let world: string = worldOrNull.valueOf();\n}\n\nlet numOrNull: JSON.Num | null = jsonObj.getNum(\"value\");\nif (numOrNull != null) {\n  // use .valueOf() to turn the high level JSON.Num type into a f64\n  let value: f64 = numOrNull.valueOf();\n}\n\n// If you don't know the value type, get the parent JSON.Value\nlet valueOrNull: JSON.Value | null = jsonObj.getValue(\"hello\");\n  if (valueOrNull != null) {\n  let value = \u003cJSON.Value\u003evalueOrNull;\n\n  // Next we could figure out what type we are\n  if(value.isString) { \n    // value.isString would be true, so we can cast to a string\n    let innerString = (\u003cJSON.Str\u003evalue).valueOf();\n    let jsonString = (\u003cJSON.Str\u003evalue).stringify();\n\n    // Do something with string value\n  }\n}\n```\n\n### Encoding JSON\n\n\n```typescript\nimport { JSONEncoder } from \"assemblyscript-json\";\n\n// Create encoder\nlet encoder = new JSONEncoder();\n\n// Construct necessary object\nencoder.pushObject(\"obj\");\nencoder.setInteger(\"int\", 10);\nencoder.setString(\"str\", \"\");\nencoder.popObject();\n\n// Get serialized data\nlet json: Uint8Array = encoder.serialize();\n\n// Or get serialized data as string\nlet jsonString: string = encoder.stringify();\n\nassert(jsonString, '\"obj\": {\"int\": 10, \"str\": \"\"}'); // True!\n```\n\n### Custom JSON Deserializers\n\n```typescript\nimport { JSONDecoder, JSONHandler } from \"assemblyscript-json\";\n\n// Events need to be received by custom object extending JSONHandler.\n// NOTE: All methods are optional to implement.\nclass MyJSONEventsHandler extends JSONHandler {\n  setString(name: string, value: string): void {\n    // Handle field\n  }\n\n  setBoolean(name: string, value: bool): void {\n    // Handle field\n  }\n\n  setNull(name: string): void {\n    // Handle field\n  }\n\n  setInteger(name: string, value: i64): void {\n    // Handle field\n  }\n\n  setFloat(name: string, value: f64): void {\n    // Handle field\n  }\n\n  pushArray(name: string): bool {\n    // Handle array start\n    // true means that nested object needs to be traversed, false otherwise\n    // Note that returning false means JSONDecoder.startIndex need to be updated by handler\n    return true;\n  }\n\n  popArray(): void {\n    // Handle array end\n  }\n\n  pushObject(name: string): bool {\n    // Handle object start\n    // true means that nested object needs to be traversed, false otherwise\n    // Note that returning false means JSONDecoder.startIndex need to be updated by handler\n    return true;\n  }\n\n  popObject(): void {\n    // Handle object end\n  }\n}\n\n// Create decoder\nlet decoder = new JSONDecoder\u003cMyJSONEventsHandler\u003e(new MyJSONEventsHandler());\n\n// Create a byte buffer of our JSON. NOTE: Deserializers work on UTF8 string buffers.\nlet jsonString = '{\"hello\": \"world\"}';\nlet jsonBuffer = Uint8Array.wrap(String.UTF8.encode(jsonString));\n\n// Parse JSON\ndecoder.deserialize(jsonBuffer); // This will send events to MyJSONEventsHandler\n```\n\nFeel free to look through the [tests](https://github.com/nearprotocol/assemblyscript-json/tree/master/assembly/__tests__) for more usage examples.\n\n## Reference Documentation\n\nReference API Documentation can be found in the [docs directory](./docs).\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnear%2Fassemblyscript-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnear%2Fassemblyscript-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnear%2Fassemblyscript-json/lists"}