{"id":13817039,"url":"https://github.com/nearprotocol/assemblyscript-bson","last_synced_at":"2025-05-15T19:30:55.464Z","repository":{"id":54544204,"uuid":"159898148","full_name":"nearprotocol/assemblyscript-bson","owner":"nearprotocol","description":"BSON encoder / decoder for AssemblyScript","archived":false,"fork":false,"pushed_at":"2021-02-11T16:28:27.000Z","size":79,"stargazers_count":36,"open_issues_count":2,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-19T14:42:44.241Z","etag":null,"topics":["assemblyscript","bson","webassembly"],"latest_commit_sha":null,"homepage":"https://nearprotocol.com","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/nearprotocol.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":"2018-12-01T01:44:45.000Z","updated_at":"2024-11-10T23:08:24.000Z","dependencies_parsed_at":"2022-08-13T19:10:53.314Z","dependency_job_id":null,"html_url":"https://github.com/nearprotocol/assemblyscript-bson","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/nearprotocol%2Fassemblyscript-bson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nearprotocol%2Fassemblyscript-bson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nearprotocol%2Fassemblyscript-bson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nearprotocol%2Fassemblyscript-bson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nearprotocol","download_url":"https://codeload.github.com/nearprotocol/assemblyscript-bson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254407239,"owners_count":22066196,"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":["assemblyscript","bson","webassembly"],"created_at":"2024-08-04T06:00:32.050Z","updated_at":"2025-05-15T19:30:55.163Z","avatar_url":"https://github.com/nearprotocol.png","language":"TypeScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"# assemblyscript-bson\n\nBSON encoder / decoder for AssemblyScript somewhat based on https://github.com/mpaland/bsonfy.\n\nSpecial thanks to https://github.com/MaxGraey/bignum.wasm for basic unit testing infra for AssemblyScript.\n\n# Limitations\n\nThis is developed for use in smart contracts written in AssemblyScript for https://github.com/nearprotocol/nearcore.\nThis imposes such limitations:\n- Only limited data types are supported:\n    - arrays\n    - objects\n    - 32-bit integers\n    - strings\n    - booleans\n    - null\n    - `Uint8Array`\n- We assume that memory never needs to be deallocated (cause these contracts are short-lived).\n\nNote that this mostly just defines the way it's currently implemented. Contributors are welcome to fix limitations.\n\n\n# Usage\n\n## Encoding BSON\n\n```ts\n// Make sure memory allocator is available\nimport \"allocator/arena\";\n// Import encoder\nimport { BSONEncoder } from \"path/to/module\";\n\n// Create encoder\nlet encoder = new BSONEncoder();\n\n// Construct necessary object\nencoder.pushObject(\"obj\");\nencoder.setInteger(\"int\", 10);\nencoder.setString(\"str\", \"\");\nencoder.popObject();\n\n// Get serialized data\nlet bson: Uint8Array = encoder.serialize();\n\n```\n\n## Parsing BSON\n\n```ts\n// Make sure memory allocator is available\nimport \"allocator/arena\";\n// Import decoder\nimport { BSONDecoder, BSONHandler } from \"path/to/module\";\n\n// Events need to be received by custom object extending BSONHandler.\n// NOTE: All methods are optional to implement.\nclass MyBSONEventsHandler extends BSONHandler {\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: i32): void {\n        // Handle field\n    }\n\n    setUint8Array(name: string, value: Uint8Array): void {\n        // Handle field\n    }\n\n    pushArray(name: string): bool {\n        // Handle array start\n        return true; // true means that nested object needs to be traversed, false otherwise\n    }\n\n    popArray(): void {\n        // Handle array end\n    }\n\n    pushObject(name: string): bool {\n        // Handle object start\n        return true; // true means that nested object needs to be traversed, false otherwise\n    }\n\n    popObject(): void {\n        // Handle object end\n    }\n}\n\n// Create decoder\nlet decoder = new BSONDecoder\u003cMyBSONEventsHandler\u003e(new MyBSONEventsHandler());\n\n// Let's assume BSON data is available in this variable\nlet bson: Uint8Array = ...;\n\n// Parse BSON\ndecoder.deserialize(bson); // This will send events to MyBSONEventsHandler\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnearprotocol%2Fassemblyscript-bson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnearprotocol%2Fassemblyscript-bson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnearprotocol%2Fassemblyscript-bson/lists"}