{"id":13651880,"url":"https://github.com/paritytech/scale-value","last_synced_at":"2025-04-09T10:06:40.871Z","repository":{"id":37424668,"uuid":"493611725","full_name":"paritytech/scale-value","owner":"paritytech","description":"Encode and decode dynamically constructed values of arbitrary shapes to/from SCALE bytes","archived":false,"fork":false,"pushed_at":"2024-12-30T03:05:13.000Z","size":317,"stargazers_count":17,"open_issues_count":2,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-17T12:00:01.912Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paritytech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-18T10:17:06.000Z","updated_at":"2024-11-15T14:27:56.000Z","dependencies_parsed_at":"2024-01-03T05:14:38.146Z","dependency_job_id":"caadca1e-ed1f-4fdc-aa9b-23defb46ac07","html_url":"https://github.com/paritytech/scale-value","commit_stats":{"total_commits":41,"total_committers":1,"mean_commits":41.0,"dds":0.0,"last_synced_commit":"f04c1afb244a7ff47208a0b82907a0b6cc9f7e85"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paritytech%2Fscale-value","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paritytech%2Fscale-value/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paritytech%2Fscale-value/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paritytech%2Fscale-value/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paritytech","download_url":"https://codeload.github.com/paritytech/scale-value/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248018060,"owners_count":21034048,"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":[],"created_at":"2024-08-02T02:00:53.153Z","updated_at":"2025-04-09T10:06:40.841Z","avatar_url":"https://github.com/paritytech.png","language":"Rust","funding_links":[],"categories":["Tools"],"sub_categories":[],"readme":"# scale-value \u0026middot; [![CI Status][ci-badge]][ci] [![Latest Version on Crates.io][crates-badge]][crates] [![Released API docs][docs-badge]][docs]\n\nThis crate provides a `Value` type, which is a runtime representation that is compatible with [`scale_info::TypeDef`][scale-info-typedef]. It somewhat analogous to a `serde_json::Value`, which is a runtime representation of JSON values, but with a focus on SCALE encoded values instead of JSON encoded values. Unlike JSON however, SCALE encoding is not self describing, and so we need additional type information to tell us how to encode and decode values.\n\nIt is expected that this crate will commonly be used in conjunction with the [scale-info] and [frame-metadata] crates.\n\nThe [scale-info] crate allows us to define types and add them to a type registry, which in turn is used to tell us how to SCALE encode and decode `Value`s.\n\nThe [frame-metadata] crate contains all of the type information we need in order to be able to SCALE encode and decode `Value`s into the various parameters needed in extrinsics and such.\n\nCrate features (enabled by default):\n- `serde`: Allow `Value`s to be converted from and to static Rust types (where possible), or serialized and deserialized to other formats like JSON, via serde.\n- `from_string`: Allow strings to be parsed into `Values` using the same format from which values can be converted to strings via `.to_string()`. Examples:\n  - Boolean types parse from `true` and `false`.\n  - Strings and chars are supported with `\"Hello\\n there\"` and `'a'`.\n  - Numbers like `1_234_567` and `-123` are supported.\n  - Composite types (structs/tuples) look like `{ hello: 123, \"there\": true }` and `('a', 'b', true)`.\n  - Finally, enum variants look like `Hello { foo: 1, bar: 2 }` and `Foo(1,2,3)`.\n\n# Examples\n\nManually creating a type registry, and then using it to SCALE encode and decode some runtime constructed `Value` type to/from SCALE bytes.\n\n```rust\n// Turn a type into an ID and type registry using `scale-info`:\nfn make_type\u003cT: scale_info::TypeInfo + 'static\u003e() -\u003e (u32, scale_info::PortableRegistry) {\n    let m = scale_info::MetaType::new::\u003cT\u003e();\n    let mut types = scale_info::Registry::new();\n    let id = types.register_type(\u0026m);\n    let portable_registry: scale_info::PortableRegistry = types.into();\n    (id.id(), portable_registry)\n}\n\n// Some type which we have derived SCALE type information about:\n#[derive(scale_info::TypeInfo)]\nenum Foo {\n    A { is_valid: bool, name: String }\n}\n\n// We can build a type registry containing just this type:\nlet (type_id, registry) = make_type::\u003cFoo\u003e();\nuse scale_value::Value;\n\n// Next, we can construct a runtime value of a similar shape:\nlet value = Value::named_variant(\"A\", vec![\n    (\"is_valid\".into(), Value::bool(true)),\n    (\"name\".into(), Value::string(\"James\")),\n]);\n\n// Given the type registry and ID, we can try to convert our Value into SCALE bytes:\nlet mut bytes = Vec::new();\nscale_value::scale::encode_as_type(value.clone(), type_id, \u0026registry, \u0026mut bytes).unwrap();\n\n// We can also go the other way, and decode out bytes back into the same Value:\nlet new_value = scale_value::scale::decode_as_type(\u0026mut \u0026*bytes, type_id, \u0026registry).unwrap();\n\n// The two values should equal each other (`.remove_context()` just removes the additional\n// type information handed back when the value is decoded):\nassert_eq!(value, new_value.remove_context());\n```\n\nUsing the `serde` feature to convert a `Value` to/from some rust type via serde:\n\n```rust\nuse scale_value::Value;\nuse serde::{ Serialize, Deserialize };\n\n// Some type we want to be able to serialize/deserialize:\n#[derive(Serialize, Deserialize, PartialEq, Debug)]\nenum Foo {\n    A { is_valid: bool, name: String },\n    B(u8, bool)\n}\n\n// First, serialize a Value into the rust type:\nlet value = Value::named_variant(\"A\", vec![\n    (\"name\".into(), Value::string(\"James\")),\n    (\"is_valid\".into(), Value::bool(true)),\n]);\nlet foo1: Foo = scale_value::serde::from_value(value).unwrap();\nassert_eq!(foo1, Foo::A { is_valid: true, name: \"James\".into() });\n\n// Next, deserialize the rust type back into a Value:\nlet new_value = scale_value::serde::to_value(foo).unwrap();\nassert_eq!(value, new_value);\n```\n\nCheck out [the documentation][docs] for a full API reference and more examples.\n\n[ci]: https://github.com/paritytech/scale-value/actions?query=workflow%3ARust+branch%3Amaster\n[ci-badge]: https://github.com/paritytech/scale-value/workflows/Rust/badge.svg\n[crates]: https://crates.io/crates/scale-value\n[crates-badge]: https://img.shields.io/crates/v/scale-value.svg\n[docs]: https://docs.rs/scale-value\n[docs-badge]: https://docs.rs/scale-value/badge.svg\n[scale-info]: https://github.com/paritytech/scale-info\n[scale-info-typedef]: https://docs.rs/scale-info/latest/scale_info/enum.TypeDef.html\n[frame-metadata]: https://github.com/paritytech/frame-metadata","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparitytech%2Fscale-value","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparitytech%2Fscale-value","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparitytech%2Fscale-value/lists"}