{"id":17922579,"url":"https://github.com/rreverser/serde-wasm-bindgen","last_synced_at":"2025-05-14T22:09:39.866Z","repository":{"id":37550029,"uuid":"184602746","full_name":"RReverser/serde-wasm-bindgen","owner":"RReverser","description":"Native integration of Serde with wasm-bindgen","archived":false,"fork":false,"pushed_at":"2024-02-27T21:32:30.000Z","size":900,"stargazers_count":584,"open_issues_count":4,"forks_count":36,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-05-14T18:56:32.042Z","etag":null,"topics":["javascript","rust","rust-wasm","serde","wasm","webassembly"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/serde-wasm-bindgen","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/RReverser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":"RReverser","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2019-05-02T15:09:28.000Z","updated_at":"2025-05-14T05:34:53.000Z","dependencies_parsed_at":"2023-12-06T15:56:01.231Z","dependency_job_id":"1412faf4-50d0-490a-9ca2-600e40fb5545","html_url":"https://github.com/RReverser/serde-wasm-bindgen","commit_stats":{"total_commits":187,"total_committers":13,"mean_commits":"14.384615384615385","dds":0.1925133689839572,"last_synced_commit":"e65f027ed7f80cb1fc6b32e9a28f41644faa6fda"},"previous_names":["rreverser/serde-wasm-bindgen","cloudflare/serde-wasm-bindgen"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RReverser%2Fserde-wasm-bindgen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RReverser%2Fserde-wasm-bindgen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RReverser%2Fserde-wasm-bindgen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RReverser%2Fserde-wasm-bindgen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RReverser","download_url":"https://codeload.github.com/RReverser/serde-wasm-bindgen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254235701,"owners_count":22036964,"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":["javascript","rust","rust-wasm","serde","wasm","webassembly"],"created_at":"2024-10-28T20:39:50.947Z","updated_at":"2025-05-14T22:09:34.852Z","avatar_url":"https://github.com/RReverser.png","language":"Rust","funding_links":["https://github.com/sponsors/RReverser"],"categories":[],"sub_categories":[],"readme":"[![Crates.io](https://img.shields.io/crates/d/serde-wasm-bindgen?logo=rust)](https://crates.io/crates/serde-wasm-bindgen)\n[![docs.rs](https://img.shields.io/docsrs/serde-wasm-bindgen)](https://docs.rs/serde-wasm-bindgen/)\n[![GitHub Sponsors](https://img.shields.io/github/sponsors/rreverser)](https://github.com/sponsors/RReverser)\n\nThis is a native integration of [Serde](https://serde.rs/) with [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen). It allows to convert Rust data types into native JavaScript types and vice versa.\n\nInitially this library was created while working for [@Cloudflare](https://github.com/cloudflare) as [an alternative implementation](https://github.com/rustwasm/wasm-bindgen/issues/1258) to the JSON-based Serde support built into the `wasm-bindgen` but, [nowadays](https://github.com/rustwasm/wasm-bindgen/pull/3031) `serde-wasm-bindgen` is the officially preferred approach. It provides much smaller code size overhead than JSON, and, in most common cases, provides much faster serialization/deserialization as well.\n\n## Usage\n\nCopied almost verbatim from the [`wasm-bindgen` guide](https://rustwasm.github.io/wasm-bindgen/reference/arbitrary-data-with-serde.html#serializing-and-deserializing-arbitrary-data-into-and-from-jsvalue-with-serde):\n\n### Add dependencies\n\nTo use `serde-wasm-bindgen`, you first have to add it as a dependency in your\n`Cargo.toml`. You also need the `serde` crate, with the `derive` feature\nenabled, to allow your types to be serialized and deserialized with Serde.\n\n```toml\n[dependencies]\nserde = { version = \"1.0\", features = [\"derive\"] }\nserde-wasm-bindgen = \"0.4\"\n```\n\n### Derive the `Serialize` and `Deserialize` Traits\n\nAdd `#[derive(Serialize, Deserialize)]` to your type. All of your type\nmembers must also be supported by Serde, i.e. their types must also implement\nthe `Serialize` and `Deserialize` traits.\n\nNote that you don't need to use the `#[wasm_bindgen]` macro.\n\n```rust\nuse serde::{Serialize, Deserialize};\n\n#[derive(Serialize, Deserialize)]\npub struct Example {\n    pub field1: HashMap\u003cu32, String\u003e,\n    pub field2: Vec\u003cVec\u003cf32\u003e\u003e,\n    pub field3: [f32; 4],\n}\n```\n\n### Send it to JavaScript with `serde_wasm_bindgen::to_value`\n\n```rust\n#[wasm_bindgen]\npub fn send_example_to_js() -\u003e Result\u003cJsValue, JsValue\u003e {\n    let mut field1 = HashMap::new();\n    field1.insert(0, String::from(\"ex\"));\n\n    let example = Example {\n        field1,\n        field2: vec![vec![1., 2.], vec![3., 4.]],\n        field3: [1., 2., 3., 4.]\n    };\n\n    Ok(serde_wasm_bindgen::to_value(\u0026example)?)\n}\n```\n\n### Receive it from JavaScript with `serde_wasm_bindgen::from_value`\n\n```rust\n#[wasm_bindgen]\npub fn receive_example_from_js(val: JsValue) -\u003e Result\u003c(), JsValue\u003e {\n    let example: Example = serde_wasm_bindgen::from_value(val)?;\n    /* …do something with `example`… */\n    Ok(())\n}\n```\n\n### JavaScript Usage\n\nIn the `JsValue` that JavaScript gets, `field1` will be a `Map\u003cnumber, string\u003e`,\n`field2` will be an `Array\u003cArray\u003cnumber\u003e\u003e`, and `field3` will be an `Array\u003cnumber\u003e`.\n\n```js\nimport { send_example_to_js, receive_example_from_js } from \"example\";\n\n// Get the example object from wasm.\nlet example = send_example_to_js();\n\n// Add another \"Vec\" element to the end of the \"Vec\u003cVec\u003cf32\u003e\u003e\"\nexample.field2.push([5, 6]);\n\n// Send the example object back to wasm.\nreceive_example_from_js(example);\n```\n\n## Supported Types\n\nNote that, even though it might often be the case, by default this library doesn't attempt\nto be strictly compatible with JSON, instead prioritising better\ncompatibility with common JavaScript idioms and representations.\n\nIf you need JSON compatibility (e.g. you want to serialize `HashMap\u003cString, …\u003e`\nas plain objects instead of JavaScript `Map` instances), use the\n[`Serializer::json_compatible()`](https://docs.rs/serde-wasm-bindgen/latest/serde_wasm_bindgen/struct.Serializer.html#method.json_compatible) preset.\n\nBy default, Rust ⬄ JavaScript conversions in `serde-wasm-bindgen` follow this table:\n\n| Rust                              | JavaScript                           | Also supported in `from_value` |\n|-----------------------------------|--------------------------------------|--------------------------------|\n| `()` and `Option\u003cT\u003e::None`        | `undefined`                          | `null`                         |\n| `bool`                            | `boolean`                            |                                |\n| `f32`, `f64`                      | `number`                             |                                |\n| `u8`, `i8`, …, `u32`, `i32`       | `number` in the [safe integer] range |                                |\n| `u64`, `i64`, `usize`, `isize`    | `number` in the [safe integer] range | `bigint`                       |\n| `u128`, `i128`                    | `bigint`                             |                                |\n| `String`                          | `string`                             |                                |\n| `char`                            | single-codepoint `string`            |                                |\n| `Enum::Variant { … }`             | [as configured in Serde]             |                                |\n| `HashMap\u003cK, V\u003e`, `BTreeMap`, etc. | `Map\u003cK, V\u003e`                          | any iterable over `[K, V]`     |\n| `Struct { key1: value1, … }`      | `{ key1: value1, … }` object         |                                |\n| tuple, `Vec\u003cT\u003e`, `HashSet`, etc.  | `T[]` array                          | any iterable over `T`          |\n| [`serde_bytes`] byte buffer       | `Uint8Array`                         | `ArrayBuffer`, `Array`         |\n\n[as configured in Serde]: https://serde.rs/enum-representations.html\n[safe integer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger\n[`serde_bytes`]: https://github.com/serde-rs/bytes\n\nThe first two columns show idiomatic representations on Rust and JavaScript sides, while the 3rd column shows which JavaScript values\nare additionally supported when deserializing from JavaScript to the Rust type.\n\n### Serializer configuration options\n\nYou can customize serialization from Rust to JavaScript by setting the following options on the [`Serializer::new()`](https://docs.rs/serde-wasm-bindgen/latest/serde_wasm_bindgen/struct.Serializer.html) instance (all default to false):\n\n- `.serialize_missing_as_null(true)`: Serialize `()`, unit structs and `Option::None` to `null` instead of `undefined`.\n- `.serialize_maps_as_objects(true)`: Serialize maps into plain JavaScript objects instead of ES2015 Maps.\n- `.serialize_large_number_types_as_bigints(true)`: Serialize `u64`, `i64`, `usize` and `isize` to `bigint`s instead of attempting to fit them into the [safe integer] `number` or failing.\n- `.serialize_bytes_as_arrays(true)`: Serialize bytes into plain JavaScript arrays instead of ES2015 Uint8Arrays.\n\nYou can also use the `Serializer::json_compatible()` preset to create a JSON compatible serializer. It enables `serialize_missing_as_null`, `serialize_maps_as_objects`, and `serialize_bytes_as_arrays` under the hood.\n\n### Preserving JavaScript values\n\nSometimes you want to preserve original JavaScript value instead of converting it into a Rust type. This is particularly useful for types that can't be converted without losing the data, such as [`Date`](https://docs.rs/js-sys/latest/js_sys/struct.Date.html), [`RegExp`](https://docs.rs/js-sys/latest/js_sys/struct.RegExp.html) or 3rd-party types.\n\n`serde_wasm_bindgen::preserve` allows you to do just that:\n\n```rust\n#[derive(Serialize, Deserialize)]\npub struct Example {\n    pub regular_field: i32,\n\n    #[serde(with = \"serde_wasm_bindgen::preserve\")]\n    pub preserved_date: js_sys::Date,\n\n    #[serde(with = \"serde_wasm_bindgen::preserve\")]\n    pub preserved_arbitrary_value: JsValue,\n}\n```\n\n## TypeScript support\n\nThere's no built-in type generation in this crate, but you can [tsify](https://github.com/madonoharu/tsify) with the `js` feature which integrates with `serde-wasm-bindgen` under the hood. Aside from generating structural typings, it also allows to derive `IntoWasmAbi` / `FromWasmAbi` so that you don't have to write `from_value` / `to_value` by hand.\n\n## License\n\nLicensed under the MIT license. See the\n[LICENSE](https://github.com/RReverser/serde-wasm-bindgen/blob/master/LICENSE)\nfile for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frreverser%2Fserde-wasm-bindgen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frreverser%2Fserde-wasm-bindgen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frreverser%2Fserde-wasm-bindgen/lists"}