├── .cargo
└── config
├── .editorconfig
├── .github
└── workflows
│ └── Basic.yml
├── .gitignore
├── .prettierignore
├── .prettierrc.json
├── CHANGELOG.md
├── Cargo.lock
├── Cargo.toml
├── LICENSE-APACHE
├── LICENSE-MIT
├── README.md
├── devtools
└── format_md.sh
└── src
├── de
├── enum_.rs
├── errors.rs
├── map.rs
├── mod.rs
├── seq.rs
└── unescape.rs
├── lib.rs
└── ser
├── map.rs
├── mod.rs
├── seq.rs
└── struct_.rs
/.cargo/config:
--------------------------------------------------------------------------------
1 | [alias]
2 | wasm = "build --release --target wasm32-unknown-unknown"
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig is awesome: https://EditorConfig.org
2 |
3 | # top-most EditorConfig file
4 | root = true
5 |
6 | [*]
7 | indent_style = space
8 | indent_size = 2
9 | charset = utf-8
10 | trim_trailing_whitespace = true
11 | insert_final_newline = true
12 |
13 | [*.rs]
14 | indent_size = 4
15 |
--------------------------------------------------------------------------------
/.github/workflows/Basic.yml:
--------------------------------------------------------------------------------
1 | # Based on https://github.com/actions-rs/example/blob/master/.github/workflows/quickstart.yml
2 |
3 | on: [push, pull_request]
4 |
5 | name: Basic
6 |
7 | jobs:
8 |
9 | tests:
10 | name: Tests
11 | runs-on: ubuntu-latest
12 | steps:
13 | - name: Checkout sources
14 | uses: actions/checkout@v2
15 |
16 | - name: Install stable toolchain
17 | uses: actions-rs/toolchain@v1
18 | with:
19 | profile: minimal
20 | toolchain: 1.59.0
21 | target: wasm32-unknown-unknown
22 | override: true
23 |
24 | - name: Run tests
25 | uses: actions-rs/cargo@v1
26 | with:
27 | command: test
28 | args: --locked
29 | env:
30 | RUST_BACKTRACE: 1
31 | RUSTFLAGS: "-D warnings"
32 |
33 | - name: Compile to Wasm
34 | uses: actions-rs/cargo@v1
35 | with:
36 | command: wasm
37 | args: --locked
38 | env:
39 | RUST_BACKTRACE: 1
40 | RUSTFLAGS: "-D warnings"
41 |
42 | sanity:
43 | name: Sanity
44 | runs-on: ubuntu-latest
45 | steps:
46 | - name: Checkout sources
47 | uses: actions/checkout@v2
48 |
49 | - name: Install stable toolchain
50 | uses: actions-rs/toolchain@v1
51 | with:
52 | profile: minimal
53 | toolchain: 1.59.0
54 | override: true
55 | components: rustfmt, clippy
56 |
57 | - name: Run cargo fmt
58 | uses: actions-rs/cargo@v1
59 | with:
60 | command: fmt
61 | args: -- --check
62 |
63 | - name: Run cargo clippy
64 | uses: actions-rs/cargo@v1
65 | with:
66 | command: clippy
67 | args: --all-targets -- -D warnings
68 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | **/*.rs.bk
2 | .#*
3 | /target
4 | .idea
5 |
--------------------------------------------------------------------------------
/.prettierignore:
--------------------------------------------------------------------------------
1 | # Used for *.md formatting in devtools/format_md.sh
2 | target/
3 |
--------------------------------------------------------------------------------
/.prettierrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "overrides": [
3 | {
4 | "files": "*.md",
5 | "options": {
6 | "proseWrap": "always"
7 | }
8 | }
9 | ]
10 | }
11 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to this project will be documented in this file.
4 |
5 | The format is based on [Keep a Changelog](http://keepachangelog.com/) and this
6 | project adheres to [Semantic Versioning](http://semver.org/).
7 |
8 | ## [Unreleased]
9 |
10 | ## [1.0.1] - 2024-01-23
11 |
12 | ### Changed
13 |
14 | - Add recursion limit to deserialization ([#65]).
15 |
16 | [#65]: https://github.com/CosmWasm/serde-json-wasm/pull/65
17 |
18 | ## [1.0.0] - 2023-08-04
19 |
20 | ### Changed
21 |
22 | - Introduce `std` (enabled by default) and `unstable` features ([#58]). They
23 | enable corresponding serde’s features and if either is enabled, `Error`
24 | implements `std::error::Error` trait. By itself, `serde-json-wasm` is now
25 | `no_std`; it’s up to serde’s features whether the entire build is.
26 | **Please note:** this potentially breaks `default-features = false` builds. If
27 | you need the `Error` trait impl, enable one of the two features explicitly.
28 |
29 | - Serialize / deserialize `u128`/`i128` types as numbers instead of strings
30 | ([#59]). **Please note:** this breaks deserialization of `u128`/`i128`
31 | serialized with older versions of `serde-json-wasm`.
32 |
33 | [#58]: https://github.com/CosmWasm/serde-json-wasm/pull/58
34 | [#59]: https://github.com/CosmWasm/serde-json-wasm/pull/59
35 |
36 | ## [0.5.1] - 2023-04-11
37 |
38 | ### Added
39 |
40 | - Add support for `collect_str` serialization ([#51], [#55]).
41 |
42 | [#51]: https://github.com/CosmWasm/serde-json-wasm/pull/51
43 | [#55]: https://github.com/CosmWasm/serde-json-wasm/pull/55
44 |
45 | ## [0.5.0] - 2022-12-06
46 |
47 | ### Added
48 |
49 | - Add support for map (de)serialization.
50 | - Add support for `#[serde(flatten)]` (de)serialization ([#20]).
51 |
52 | [#20]: https://github.com/CosmWasm/serde-json-wasm/issues/20
53 |
54 | ### Changed
55 |
56 | - Bump min supported Rust version to 1.59.0 (same as cosmwasm-std)
57 | - Upgrade codebase to Rust edition 2021
58 |
59 | ## [0.4.1] - 2022-05-05
60 |
61 | ### Changed
62 |
63 | - Properly serialize `u128`/`i128` types when embedded in structs
64 |
65 | ## [0.4.0] - 2022-03-29
66 |
67 | ### Added
68 |
69 | - Add support for `#[serde(untagged)]` enums representation
70 |
71 | ## [0.3.2] - 2021-12-14
72 |
73 | ### Added
74 |
75 | - Add support for u128/i128 serialization and deserialization ([#32],
76 | [#33]). **Please note:** this is
77 | [incompatible with serde-json and schemars](https://github.com/CosmWasm/cosmwasm/issues/1605)
78 | and for this reason discouraged to use.
79 |
80 | [#32]: https://github.com/CosmWasm/serde-json-wasm/issues/32
81 | [#33]: https://github.com/CosmWasm/serde-json-wasm/pull/33
82 |
83 | ## [0.3.1] - 2021-01-19
84 |
85 | ### Added
86 |
87 | - Add support for Unit () deserialization.
88 |
89 | ## [0.3.0] - 2021-01-14
90 |
91 | ### Changed
92 |
93 | Maintenance release:
94 |
95 | - Update clippy version in CI to 1.49.0.
96 | - Fix `clippy::manual-non-exhaustive` warnings.
97 |
98 | ## [0.2.3] - 2021-01-14
99 |
100 | ### Changed
101 |
102 | - Optimize string serialization / deserialization.
103 |
104 | ## [0.2.2] - 2021-01-13
105 |
106 | ### Added
107 |
108 | - Add support for unit structs serialization / deserialization.
109 | - Add support for tuple variants serialization / deserialization.
110 | - Add support for unit serialization / deserialization.
111 |
112 | ## [0.2.1] - 2020-05-07
113 |
114 | ### Changed
115 |
116 | - Remove unused Travis CI config
117 | - Polish Cargo.toml
118 |
119 | ## [0.2.0] - 2020-05-07
120 |
121 | ### Fixed
122 |
123 | - The end of strings is now detected correctly in deserialization (#11)
124 |
125 | ### Changed
126 |
127 | - Strings are now escaped during serialization (#10)
128 | - `from_str`/`from_slice` now work for `T: DeserializeOwned` instead of
129 | `T: Deserialize<'de>`, making it impossible to deserialize into non-owned
130 | reference fields. This is necessary since string unescaping requires creating
131 | a mutated copy of the source data and only JSON strings without escape
132 | sequences can be deserialized copy-free. The same limitation applies to
133 | serde_json, where the problem shows up at
134 | [runtime instead of compile time](https://github.com/serde-rs/json/issues/530).
135 | - Strings are now unescaped during deserialization (#13)
136 |
137 | ## [0.1.3] - 2020-03-12
138 |
139 | - Expose deserializer and serializer
140 |
141 | ## [0.1.2] - 2019-12-20
142 |
143 | - Add newtype string support
144 |
145 | ## [0.1.1] - 2019-10-27
146 |
147 | - Fix embeded enums
148 |
149 | ## [0.1.0] - 2019-10-27
150 |
151 | Initial release after forking from
152 | [serde-json-core](https://github.com/japaric/serde-json-core) at
153 | [bf5533a0](https://github.com/japaric/serde-json-core/commit/bf5533a042a0).
154 |
155 | [unreleased]: https://github.com/CosmWasm/serde-json-wasm/compare/v1.0.1...HEAD
156 | [1.0.1]: https://github.com/CosmWasm/serde-json-wasm/compare/v1.0.0...v1.0.1
157 | [1.0.0]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.5.1...v1.0.0
158 | [0.5.1]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.5.0...v0.5.1
159 | [0.5.0]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.4.1...v0.5.0
160 | [0.4.1]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.4.0...v0.4.1
161 | [0.4.0]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.3.2...v0.4.0
162 | [0.3.2]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.3.1...v0.3.2
163 | [0.3.1]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.3.0...v0.3.1
164 | [0.3.0]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.2.3...v0.3.0
165 | [0.2.3]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.2.2...v0.2.3
166 | [0.2.2]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.2.1...v0.2.2
167 | [0.2.1]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.2.0...v0.2.1
168 | [0.2.0]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.1.3...v0.2.0
169 | [0.1.3]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.1.2...v0.1.3
170 | [0.1.2]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.1.1...v0.1.2
171 | [0.1.1]: https://github.com/CosmWasm/serde-json-wasm/compare/v0.1.0...v0.1.1
172 | [0.1.0]: https://github.com/CosmWasm/serde-json-wasm/tree/v0.1.0
173 |
--------------------------------------------------------------------------------
/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | version = 3
4 |
5 | [[package]]
6 | name = "itoa"
7 | version = "1.0.1"
8 | source = "registry+https://github.com/rust-lang/crates.io-index"
9 | checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
10 |
11 | [[package]]
12 | name = "proc-macro2"
13 | version = "1.0.36"
14 | source = "registry+https://github.com/rust-lang/crates.io-index"
15 | checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
16 | dependencies = [
17 | "unicode-xid",
18 | ]
19 |
20 | [[package]]
21 | name = "quote"
22 | version = "1.0.17"
23 | source = "registry+https://github.com/rust-lang/crates.io-index"
24 | checksum = "632d02bff7f874a36f33ea8bb416cd484b90cc66c1194b1a1110d067a7013f58"
25 | dependencies = [
26 | "proc-macro2",
27 | ]
28 |
29 | [[package]]
30 | name = "ryu"
31 | version = "1.0.9"
32 | source = "registry+https://github.com/rust-lang/crates.io-index"
33 | checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
34 |
35 | [[package]]
36 | name = "serde"
37 | version = "1.0.181"
38 | source = "registry+https://github.com/rust-lang/crates.io-index"
39 | checksum = "6d3e73c93c3240c0bda063c239298e633114c69a888c3e37ca8bb33f343e9890"
40 |
41 | [[package]]
42 | name = "serde-json-wasm"
43 | version = "1.0.1"
44 | dependencies = [
45 | "serde",
46 | "serde_derive",
47 | "serde_json",
48 | ]
49 |
50 | [[package]]
51 | name = "serde_derive"
52 | version = "1.0.136"
53 | source = "registry+https://github.com/rust-lang/crates.io-index"
54 | checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
55 | dependencies = [
56 | "proc-macro2",
57 | "quote",
58 | "syn",
59 | ]
60 |
61 | [[package]]
62 | name = "serde_json"
63 | version = "1.0.99"
64 | source = "registry+https://github.com/rust-lang/crates.io-index"
65 | checksum = "46266871c240a00b8f503b877622fe33430b3c7d963bdc0f2adc511e54a1eae3"
66 | dependencies = [
67 | "itoa",
68 | "ryu",
69 | "serde",
70 | ]
71 |
72 | [[package]]
73 | name = "syn"
74 | version = "1.0.90"
75 | source = "registry+https://github.com/rust-lang/crates.io-index"
76 | checksum = "704df27628939572cd88d33f171cd6f896f4eaca85252c6e0a72d8d8287ee86f"
77 | dependencies = [
78 | "proc-macro2",
79 | "quote",
80 | "unicode-xid",
81 | ]
82 |
83 | [[package]]
84 | name = "unicode-xid"
85 | version = "0.2.2"
86 | source = "registry+https://github.com/rust-lang/crates.io-index"
87 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
88 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | authors = [
3 | "Jorge Aparicio ",
4 | "Ethan Frey ",
5 | "Simon Warta ",
6 | "Mauro Lacy ",
7 | "Bartłomiej Kuras ",
8 | ]
9 | categories = ["wasm"]
10 | description = "serde_json for Wasm programs (small, deterministic, no floats)"
11 | documentation = "https://docs.rs/serde-json-wasm"
12 | edition = "2021"
13 | keywords = ["serde", "json", "wasm"]
14 | license = "MIT OR Apache-2.0"
15 | name = "serde-json-wasm"
16 | readme = "README.md"
17 | repository = "https://github.com/CosmWasm/serde-json-wasm"
18 | version = "1.0.1"
19 | exclude = [
20 | ".cargo/",
21 | ".github/",
22 | ".gitignore",
23 | ]
24 |
25 | [dependencies]
26 | serde = { version = "^1.0.181", default-features = false, features = ["alloc"] }
27 |
28 | [dev-dependencies]
29 | serde_derive = "^1.0.80"
30 | serde_json = "^1.0.99"
31 |
32 | [features]
33 | default = ["std"]
34 | std = ["serde/std"]
35 | unstable = ["serde/unstable"]
36 |
--------------------------------------------------------------------------------
/LICENSE-APACHE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/LICENSE-MIT:
--------------------------------------------------------------------------------
1 | Copyright (c) 2018 Jorge Aparicio
2 |
3 | Permission is hereby granted, free of charge, to any
4 | person obtaining a copy of this software and associated
5 | documentation files (the "Software"), to deal in the
6 | Software without restriction, including without
7 | limitation the rights to use, copy, modify, merge,
8 | publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software
10 | is furnished to do so, subject to the following
11 | conditions:
12 |
13 | The above copyright notice and this permission notice
14 | shall be included in all copies or substantial portions
15 | of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 | DEALINGS IN THE SOFTWARE.
26 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # serde-json-wasm
2 |
3 | [](https://crates.io/crates/serde-json-wasm)
4 | [](https://docs.rs/serde-json-wasm)
5 |
6 | A [serde-json] alternative for [CosmWasm] smart contracts.
7 |
8 | [serde-json]: https://crates.io/crates/serde_json
9 | [cosmwasm]: https://cosmwasm.com/
10 |
11 | ## License
12 |
13 | Licensed under either of
14 |
15 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
16 | http://www.apache.org/licenses/LICENSE-2.0)
17 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
18 |
19 | at your option.
20 |
21 | ### Contribution
22 |
23 | Unless you explicitly state otherwise, any contribution intentionally submitted
24 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
25 | dual licensed as above, without any additional terms or conditions.
26 |
--------------------------------------------------------------------------------
/devtools/format_md.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | set -o errexit -o nounset -o pipefail
3 | command -v shellcheck >/dev/null && shellcheck "$0"
4 |
5 | # Running with -c makes the script only validate instead of editing in place.
6 | op="write"
7 | while getopts c option; do
8 | case "${option}" in
9 | c) op="check" ;;
10 | *) ;;
11 | esac
12 | done
13 |
14 | npx prettier@2.7.1 --$op "./**/*.md"
15 |
--------------------------------------------------------------------------------
/src/de/enum_.rs:
--------------------------------------------------------------------------------
1 | use serde::de;
2 |
3 | use crate::de::{Deserializer, Error, Result};
4 |
5 | pub(crate) struct UnitVariantAccess<'a, 'b> {
6 | de: &'a mut Deserializer<'b>,
7 | }
8 |
9 | impl<'a, 'b> UnitVariantAccess<'a, 'b> {
10 | pub(crate) fn new(de: &'a mut Deserializer<'b>) -> Self {
11 | UnitVariantAccess { de }
12 | }
13 | }
14 |
15 | impl<'a, 'de> de::EnumAccess<'de> for UnitVariantAccess<'a, 'de> {
16 | type Error = Error;
17 | type Variant = Self;
18 |
19 | fn variant_seed(self, seed: V) -> Result<(V::Value, Self)>
20 | where
21 | V: de::DeserializeSeed<'de>,
22 | {
23 | let variant = seed.deserialize(&mut *self.de)?;
24 | Ok((variant, self))
25 | }
26 | }
27 |
28 | impl<'de, 'a> de::VariantAccess<'de> for UnitVariantAccess<'a, 'de> {
29 | type Error = Error;
30 |
31 | fn unit_variant(self) -> Result<()> {
32 | Ok(())
33 | }
34 |
35 | fn newtype_variant_seed(self, _seed: T) -> Result
36 | where
37 | T: de::DeserializeSeed<'de>,
38 | {
39 | Err(Error::InvalidType)
40 | }
41 |
42 | fn tuple_variant(self, _len: usize, _visitor: V) -> Result
43 | where
44 | V: de::Visitor<'de>,
45 | {
46 | Err(Error::InvalidType)
47 | }
48 |
49 | fn struct_variant(self, _fields: &'static [&'static str], _visitor: V) -> Result
50 | where
51 | V: de::Visitor<'de>,
52 | {
53 | Err(Error::InvalidType)
54 | }
55 | }
56 |
57 | pub(crate) struct StructVariantAccess<'a, 'b> {
58 | de: &'a mut Deserializer<'b>,
59 | }
60 |
61 | impl<'a, 'b> StructVariantAccess<'a, 'b> {
62 | pub fn new(de: &'a mut Deserializer<'b>) -> Self {
63 | StructVariantAccess { de }
64 | }
65 | }
66 |
67 | impl<'a, 'de> de::EnumAccess<'de> for StructVariantAccess<'a, 'de> {
68 | type Error = Error;
69 | type Variant = Self;
70 |
71 | fn variant_seed(self, seed: V) -> Result<(V::Value, Self)>
72 | where
73 | V: de::DeserializeSeed<'de>,
74 | {
75 | let val = seed.deserialize(&mut *self.de)?;
76 | self.de.parse_object_colon()?;
77 | Ok((val, self))
78 | }
79 | }
80 |
81 | impl<'a, 'de> de::VariantAccess<'de> for StructVariantAccess<'a, 'de> {
82 | type Error = Error;
83 |
84 | fn unit_variant(self) -> Result<()> {
85 | Err(Error::InvalidType)
86 | }
87 |
88 | fn newtype_variant_seed(self, seed: T) -> Result
89 | where
90 | T: de::DeserializeSeed<'de>,
91 | {
92 | let value = seed.deserialize(&mut *self.de)?;
93 | // we remove trailing '}' to be consistent with struct_variant algorithm
94 | match self
95 | .de
96 | .parse_whitespace()
97 | .ok_or(Error::EofWhileParsingValue)?
98 | {
99 | b'}' => {
100 | self.de.eat_char();
101 | Ok(value)
102 | }
103 | _ => Err(Error::ExpectedSomeValue),
104 | }
105 | }
106 |
107 | fn tuple_variant(self, len: usize, visitor: V) -> Result
108 | where
109 | V: de::Visitor<'de>,
110 | {
111 | let value = de::Deserializer::deserialize_tuple(&mut *self.de, len, visitor)?;
112 | match self
113 | .de
114 | .parse_whitespace()
115 | .ok_or(Error::EofWhileParsingValue)?
116 | {
117 | b'}' => {
118 | self.de.eat_char();
119 | Ok(value)
120 | }
121 | _ => Err(Error::ExpectedSomeValue),
122 | }
123 | }
124 |
125 | fn struct_variant(self, fields: &'static [&'static str], visitor: V) -> Result
126 | where
127 | V: de::Visitor<'de>,
128 | {
129 | let value = de::Deserializer::deserialize_struct(&mut *self.de, "", fields, visitor)?;
130 | match self
131 | .de
132 | .parse_whitespace()
133 | .ok_or(Error::EofWhileParsingValue)?
134 | {
135 | b'}' => {
136 | self.de.eat_char();
137 | Ok(value)
138 | }
139 | _ => Err(Error::ExpectedSomeValue),
140 | }
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/src/de/errors.rs:
--------------------------------------------------------------------------------
1 | use alloc::string::{String, ToString};
2 |
3 | use serde::de;
4 |
5 | /// Deserialization result
6 | pub type Result = core::result::Result;
7 |
8 | /// This type represents all possible errors that can occur when deserializing
9 | /// JSON data
10 | ///
11 | /// It implements [`std::error::Error`] trait so long as either `std` or
12 | /// `unstable` features are enabled. `std` is enabled by default and disabling
13 | /// it makes the crate `no_std`. `unstable` makes it necessary to build code
14 | /// with nightly compiler.
15 | #[derive(Debug, PartialEq, Eq)]
16 | #[non_exhaustive]
17 | pub enum Error {
18 | /// Control character (U+0000 to U+001F) found in string. Those must always be escaped.
19 | ControlCharacterInString,
20 |
21 | /// EOF while parsing a list.
22 | EofWhileParsingList,
23 |
24 | /// EOF while parsing an object.
25 | EofWhileParsingObject,
26 |
27 | /// EOF while parsing a string.
28 | EofWhileParsingString,
29 |
30 | /// EOF while parsing a JSON value.
31 | EofWhileParsingValue,
32 |
33 | /// Expected this character to be a `':'`.
34 | ExpectedColon,
35 |
36 | /// Expected a high surrogate (D800–DBFF) but found something else
37 | ExpectedHighSurrogate,
38 |
39 | /// Expected this character to be either a `','` or a `']'`.
40 | ExpectedListCommaOrEnd,
41 |
42 | /// Expected a low surrogate (DC00–DFFF) but found something else
43 | ExpectedLowSurrogate,
44 |
45 | /// Expected this character to be either a `','` or a `'}'`.
46 | ExpectedObjectCommaOrEnd,
47 |
48 | /// Expected to parse either a `true`, `false`, or a `null`.
49 | ExpectedSomeIdent,
50 |
51 | /// Expected this character to start a JSON value.
52 | ExpectedSomeValue,
53 |
54 | /// Invalid escape sequence
55 | InvalidEscape,
56 |
57 | /// Invalid number.
58 | InvalidNumber,
59 |
60 | /// Invalid type
61 | InvalidType,
62 |
63 | /// Invalid unicode code point.
64 | InvalidUnicodeCodePoint,
65 |
66 | /// Object key is not a string.
67 | KeyMustBeAString,
68 |
69 | /// Found a lone surrogate, which can exist in JSON but cannot be encoded to UTF-8
70 | LoneSurrogateFound,
71 |
72 | /// JSON has non-whitespace trailing characters after the value.
73 | TrailingCharacters,
74 |
75 | /// JSON has a comma after the last value in an array or map.
76 | TrailingComma,
77 |
78 | /// JSON is nested too deeply, exceeded the recursion limit.
79 | RecursionLimitExceeded,
80 |
81 | /// Custom error message from serde
82 | Custom(String),
83 | }
84 |
85 | impl de::StdError for Error {}
86 |
87 | impl de::Error for Error {
88 | fn custom(msg: T) -> Self {
89 | Error::Custom(msg.to_string())
90 | }
91 | }
92 |
93 | impl core::fmt::Display for Error {
94 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
95 | write!(
96 | f,
97 | "{}",
98 | match self {
99 | Error::ControlCharacterInString => "Control character found in string.",
100 | Error::EofWhileParsingList => "EOF while parsing a list.",
101 | Error::EofWhileParsingObject => "EOF while parsing an object.",
102 | Error::EofWhileParsingString => "EOF while parsing a string.",
103 | Error::EofWhileParsingValue => "EOF while parsing a JSON value.",
104 | Error::ExpectedColon => "Expected this character to be a `':'`.",
105 | Error::ExpectedHighSurrogate => "Expected a high surrogate (D800–DBFF).",
106 | Error::ExpectedListCommaOrEnd => {
107 | "Expected this character to be either a `','` or\
108 | a \
109 | `']'`."
110 | }
111 | Error::ExpectedLowSurrogate => "Expected a low surrogate (DC00–DFFF).",
112 | Error::ExpectedObjectCommaOrEnd => {
113 | "Expected this character to be either a `','` \
114 | or a \
115 | `'}'`."
116 | }
117 | Error::ExpectedSomeIdent => {
118 | "Expected to parse either a `true`, `false`, or a \
119 | `null`."
120 | }
121 | Error::ExpectedSomeValue => "Expected this character to start a JSON value.",
122 | Error::InvalidEscape => "Invalid escape sequence.",
123 | Error::InvalidNumber => "Invalid number.",
124 | Error::InvalidType => "Invalid type",
125 | Error::InvalidUnicodeCodePoint => "Invalid unicode code point.",
126 | Error::KeyMustBeAString => "Object key is not a string.",
127 | Error::LoneSurrogateFound => "Found a lone surrogate, which can exist in JSON but cannot be encoded to UTF-8.",
128 | Error::TrailingCharacters => {
129 | "JSON has non-whitespace trailing characters after \
130 | the \
131 | value."
132 | }
133 | Error::TrailingComma => "JSON has a comma after the last value in an array or map.",
134 | Error::RecursionLimitExceeded => "JSON is nested too deeply, exceeded the recursion limit.",
135 | Error::Custom(msg) => msg,
136 | }
137 | )
138 | }
139 | }
140 |
--------------------------------------------------------------------------------
/src/de/map.rs:
--------------------------------------------------------------------------------
1 | use crate::de::{Deserializer, Error};
2 | use serde::de::{self, Visitor};
3 |
4 | pub struct MapAccess<'a, 'b> {
5 | de: &'a mut Deserializer<'b>,
6 | first: bool,
7 | }
8 |
9 | impl<'a, 'b> MapAccess<'a, 'b> {
10 | pub(crate) fn new(de: &'a mut Deserializer<'b>) -> Self {
11 | MapAccess { de, first: true }
12 | }
13 | }
14 |
15 | macro_rules! deserialize_signed_key {
16 | ($self:ident, $visitor:ident, $ixx:ident, $visit_ixx:ident) => {{
17 | let de = $self.de;
18 | match de.parse_whitespace().ok_or(Error::EofWhileParsingValue)? {
19 | b'"' => de.eat_char(),
20 | _ => return Err(Error::InvalidType),
21 | };
22 |
23 | let result = match de.peek() {
24 | // after rust merged or-patterns feature, these two clause can be merged.
25 | // error[E0658]: or-patterns syntax is experimental
26 | Some(b'0'..=b'9') => super::deserialize_signed!(de, $visitor, $ixx, $visit_ixx),
27 | Some(b'-') => super::deserialize_signed!(de, $visitor, $ixx, $visit_ixx),
28 | _ => return Err(Error::InvalidType),
29 | };
30 | match de.peek() {
31 | Some(b'"') => {
32 | de.eat_char();
33 | result
34 | }
35 | _ => Err(Error::InvalidType),
36 | }
37 | }};
38 | }
39 |
40 | macro_rules! deserialize_unsigned_key {
41 | ($self:ident, $visitor:ident, $ixx:ident, $visit_ixx:ident) => {{
42 | let de = $self.de;
43 | match de.parse_whitespace().ok_or(Error::EofWhileParsingValue)? {
44 | b'"' => de.eat_char(),
45 | _ => return Err(Error::InvalidType),
46 | };
47 |
48 | let result = match de.peek() {
49 | // after rust merged or-patterns feature, these two clause can be merged.
50 | // error[E0658]: or-patterns syntax is experimental
51 | Some(b'0'..=b'9') => super::deserialize_unsigned!(de, $visitor, $ixx, $visit_ixx),
52 | Some(b'-') => super::deserialize_unsigned!(de, $visitor, $ixx, $visit_ixx),
53 | _ => return Err(Error::InvalidType),
54 | };
55 | match de.peek() {
56 | Some(b'"') => {
57 | de.eat_char();
58 | result
59 | }
60 | _ => Err(Error::InvalidType),
61 | }
62 | }};
63 | }
64 |
65 | impl<'a, 'de> de::MapAccess<'de> for MapAccess<'a, 'de> {
66 | type Error = Error;
67 |
68 | fn next_key_seed(&mut self, seed: K) -> Result