{"id":26514864,"url":"https://github.com/devcyclehq/assemblyscript-json","last_synced_at":"2025-03-21T05:29:39.691Z","repository":{"id":104786742,"uuid":"501382214","full_name":"DevCycleHQ/assemblyscript-json","owner":"DevCycleHQ","description":"DevCycle - AssemblyScript-JSON Fork","archived":false,"fork":false,"pushed_at":"2024-12-18T18:01:10.000Z","size":396,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-12-18T18:42:02.152Z","etag":null,"topics":["continuous-delivery","continuous-deployment","devcycle","devops","feature-flags","feature-toggles","openfeature"],"latest_commit_sha":null,"homepage":"https://docs.devcycle.com/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"near/assemblyscript-json","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DevCycleHQ.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":"2022-06-08T19:20:24.000Z","updated_at":"2024-12-18T17:59:31.000Z","dependencies_parsed_at":"2024-04-26T14:48:18.064Z","dependency_job_id":"c6d99e09-68ab-45d1-8a33-878fc1db5075","html_url":"https://github.com/DevCycleHQ/assemblyscript-json","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevCycleHQ%2Fassemblyscript-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevCycleHQ%2Fassemblyscript-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevCycleHQ%2Fassemblyscript-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevCycleHQ%2Fassemblyscript-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DevCycleHQ","download_url":"https://codeload.github.com/DevCycleHQ/assemblyscript-json/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244745219,"owners_count":20503040,"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":["continuous-delivery","continuous-deployment","devcycle","devops","feature-flags","feature-toggles","openfeature"],"created_at":"2025-03-21T05:29:38.882Z","updated_at":"2025-03-21T05:29:39.686Z","avatar_url":"https://github.com/DevCycleHQ.png","language":"TypeScript","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 \"@devcycle/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 \"@devcycle/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 \"@devcycle/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","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevcyclehq%2Fassemblyscript-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevcyclehq%2Fassemblyscript-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevcyclehq%2Fassemblyscript-json/lists"}