{"_id":"@devcycle/assemblyscript-json","_rev":"4-f321ddc6b797c61796e8c58cef8c63f0","name":"@devcycle/assemblyscript-json","dist-tags":{"latest":"2.0.0"},"versions":{"2.0.0":{"name":"@devcycle/assemblyscript-json","version":"2.0.0","_id":"@devcycle/assemblyscript-json@2.0.0","maintainers":[{"name":"devcycleuser","email":"support@devcycle.com"},{"name":"jonathannorris","email":"jonathannorris@gmail.com"},{"name":"elottap","email":"elliot@taplytics.com"},{"name":"jstapl","email":"jsalaber@taplytics.com"},{"name":"rohit-tap","email":"rohit@taplytics.com"},{"name":"taplyticsuser","email":"infra.secure@taplytics.com"}],"dist":{"shasum":"61ecedc825e7f7c0d7f166abb30a4a603b4ffc3e","tarball":"https://registry.npmjs.org/@devcycle/assemblyscript-json/-/assemblyscript-json-2.0.0.tgz","fileCount":20,"integrity":"sha512-TCISIe5PAUX3JHRKN0qYix83D1ClvpqKSTNxThon0XEIKSnoFCIkrZkiciyEpMHB3qI//TBd7q1itxEC0Q9oZg==","signatures":[{"sig":"MEUCIQCkv7IQmy5UDBx+LXhNAQpdrtTe9EZEsOTkGu+j100AbAIgaOfD6psk2DePGAFeLkRakLlyPdqOAnq0gJd/OiPsuTI=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":47729},"husky":{"hooks":{"pre-commit":"yarn lint || (yarn lint --fix && exit 1)"}},"types":"assembly/index.ts","gitHead":"d2ce65ec18b305e1f3db8a4fe4394b417631b024","scripts":{"docs":"typedoc --plugin typedoc-plugin-markdown --out temp-docs --tsconfig assembly/tsconfig.json --exclude \"node_modules/**/*\" --readme none assembly/index.ts","lint":"eslint --max-warnings 0 --ext ts \"assembly/**/*.ts\"","test":"asp","asbuild":"yarn asbuild:untouched && yarn asbuild:optimized","lint:fix":"yarn lint --fix","docs:deploy":"cp -r temp-docs/* docs","asbuild:optimized":"asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize","asbuild:untouched":"asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug"},"_npmUser":{"name":"devcycleuser","email":"support@devcycle.com"},"_npmVersion":"9.8.1","description":"Patched version of near/assemblyscript-json to remove template literals.","directories":{},"_nodeVersion":"18.12.1","_hasShrinkwrap":false,"devDependencies":{"husky":"^4.3.7","eslint":"^7.21.0","typedoc":"^0.21.2","typescript":"^4.1.3","@as-pect/cli":"^6.2.4","assemblyscript":"^0.19.5","typedoc-plugin-markdown":"^3.10.2","@typescript-eslint/parser":"^4.16.0","@typescript-eslint/eslint-plugin":"^4.16.0"},"_npmOperationalInternal":{"tmp":"tmp/assemblyscript-json_2.0.0_1692107964674_0.5220290696685566","host":"s3://npm-registry-packages"}}},"time":{"created":"2023-08-15T13:59:24.541Z","modified":"2025-10-15T13:28:34.280Z","2.0.0":"2023-08-15T13:59:24.892Z"},"description":"Patched version of near/assemblyscript-json to remove template literals.","maintainers":[{"email":"jonathannorris@gmail.com","name":"jonathannorris"},{"email":"support@devcycle.com","name":"devcycleuser"}],"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 = <JSON.Obj>(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 = <JSON.Value>valueOrNull;\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 = (<JSON.Str>value).valueOf();\n    let jsonString = (<JSON.Str>value).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<MyJSONEventsHandler>(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","readmeFilename":"README.md"}