Skip to content

Commit bce8f2f

Browse files
committed
documentation
1 parent 986f833 commit bce8f2f

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,101 @@ fn process_schema(text: [u8; NUM_BYTES])
8585
}
8686
```
8787

88+
## Getters API
89+
90+
#### Function name differences
91+
92+
- **Checked vs Unchecked**
93+
- `get_*`/`get_*_var` return `Option<T>`; use when a key or index may be absent.
94+
- `get_*_unchecked`/`get_*_unchecked_var` return `T` and will error on missing/mismatched types.
95+
96+
- **Fixed key vs Variable key**
97+
- Functions taking `key: [u8; KeyBytes]` expect a fixed-length key known at compile time.
98+
- Functions with `_var` take `key: BoundedVec<u8, KeyBytes>` for variable-length keys up to `KeyBytes`.
99+
100+
### Object getters API
101+
102+
- **`get_object<let KeyBytes>(key: [u8; KeyBytes])``Option<JSON>`**: From an object root, returns `Some(JSON)` if `key` exists and is an object, `None` if `key` is missing; errors if `key` exists but is not an object, or if called when the current root is an array.
103+
104+
- **`get_object_unchecked<let KeyBytes>(key: [u8; KeyBytes])``JSON`**: From an object root, returns the object at `key`; errors otherwise.
105+
106+
- **`get_object_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``Option<JSON>`**: Same as `get_object`, but the key can be variable-length up to `KeyBytes`.
107+
108+
- **`get_object_unchecked_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``JSON`**: Same as `get_object_unchecked`, but with a variable-length key.
109+
110+
- **`get_object_from_array(index: Field)``Option<JSON>`**: When the current root is an array, returns the object at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element is not an object.
111+
112+
- **`get_object_from_array_unchecked(index: Field)``JSON`**: When the current root is an array, returns the object at `index`. Errors if `index` is out of bounds or the element is not an object.
113+
114+
### Array getters API
115+
116+
- **`get_length()``u32`**: Returns its length when the current root is an array; errors otherwise.
117+
118+
- **`get_array<let KeyBytes>(key: [u8; KeyBytes])``Option<JSON>`**: From an object root, returns Some(JSON) if key exists and is an array, None if key is missing; errors if key exists but is not an array, or if called when the current root is an array.
119+
120+
- **`get_array_unchecked<let KeyBytes>(key: [u8; KeyBytes])``JSON`**: From an object root, returns the array at key; errors otherwise.
121+
122+
- **`get_array_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``Option<JSON>`**: Same as `get_array`, but the key can be variable-length up to `KeyBytes`.
123+
124+
- **`get_array_unchecked_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``JSON`**: Same as `get_array_unchecked`, but with a variable-length key.
125+
126+
- **`get_array_from_array(index: Field)``Option<JSON>`**: When the current root is an array, returns the nested array at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element is not an array.
127+
128+
- **`get_array_from_array_unchecked(index: Field)``JSON`**: When the current root is an array, returns the nested array at `index`. Errors if `index` is out of bounds or the element is not an array.
129+
130+
### Literal getters API
131+
132+
Literals are the JSON keywords `true`, `false`, and `null`. They are represented by `JSONLiteral`, which supports:
133+
- `is_true()`, `is_false()`, `is_null()`
134+
- `to_bool()` (maps `true``true`, `false`/`null``false`)
135+
136+
- **`get_literal<let KeyBytes>(key: [u8; KeyBytes])``Option<JSONLiteral>`**: From an object root, returns `Some(JSONLiteral)` if `key` exists and is a literal; `None` if `key` is missing; errors if `key` exists but is not a literal, or if called when the current root is an array.
137+
138+
- **`get_literal_unchecked<let KeyBytes>(key: [u8; KeyBytes])``JSONLiteral`**: From an object root, returns the literal at `key`; errors if the key is missing, not a literal, or if called when the current root is an array.
139+
140+
- **`get_literal_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``Option<JSONLiteral>`**: Same as `get_literal`, but accepts a variable-length key up to `KeyBytes`.
141+
142+
- **`get_literal_unchecked_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``JSONLiteral`**: Same as `get_literal_unchecked`, with a variable-length key.
143+
144+
- **`get_literal_from_array(index: Field)``Option<JSONLiteral>`**: When the current root is an array, returns the literal at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a literal.
145+
146+
- **`get_literal_from_array_unchecked(index: Field)``JSONLiteral`**: When the current root is an array, returns the literal at `index`; errors if `index` is out of bounds or the element is not a literal.
147+
148+
### Number getters API
149+
150+
Numbers are parsed as unsigned integers and must fit into `u64` (no decimals).
151+
152+
- **`get_number<let KeyBytes>(key: [u8; KeyBytes])``Option<u64>`**: From an object root, returns `Some(u64)` if `key` exists and is a number; `None` if `key` is missing; errors if `key` exists but is not a number, or if called when the current root is an array.
153+
154+
- **`get_number_unchecked<let KeyBytes>(key: [u8; KeyBytes])``u64`**: From an object root, returns the number at `key`; errors if the key is missing, not a number, or if called when the current root is an array.
155+
156+
- **`get_number_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``Option<u64>`**: Same as `get_number`, but accepts a variable-length key up to `KeyBytes`.
157+
158+
- **`get_number_unchecked_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``u64`**: Same as `get_number_unchecked`, with a variable-length key.
159+
160+
- **`get_number_from_array(index: Field)``Option<u64>`**: When the current root is an array, returns the number at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a number.
161+
162+
- **`get_number_from_array_unchecked(index: Field)``u64`**: When the current root is an array, returns the number at `index`; errors if `index` is out of bounds or the element is not a number.
163+
164+
### String getters API
165+
166+
Strings are returned as `BoundedVec<u8, StringBytes>` where `StringBytes` is the maximum allowed length of the returned string.
167+
168+
- **`get_string<let KeyBytes, let StringBytes>(key: [u8; KeyBytes])``Option<BoundedVec<u8, StringBytes>>`**: From an object root, returns `Some(string)` if `key` exists and is a string; `None` if `key` is missing; errors if `key` exists but is not a string, or if called when the current root is an array.
169+
170+
- **`get_string_unchecked<let KeyBytes, let StringBytes>(key: [u8; KeyBytes])``BoundedVec<u8, StringBytes>`**: From an object root, returns the string at `key`; errors otherwise.
171+
172+
- **`get_string_var<let KeyBytes, let StringBytes>(key: BoundedVec<u8, KeyBytes>)``Option<BoundedVec<u8, StringBytes>>`**: Same as `get_string`, but accepts a variable-length key up to `KeyBytes`.
173+
174+
- **`get_string_unchecked_var<let KeyBytes, let StringBytes>(key: BoundedVec<u8, KeyBytes>)``BoundedVec<u8, StringBytes>`**: Same as `get_string_unchecked`, with a variable-length key.
175+
176+
- **`get_string_from_array<let StringBytes>(index: Field)``Option<BoundedVec<u8, StringBytes>>`**: When the current root is an array, returns the string at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a string.
177+
178+
- **`get_string_from_array_unchecked<let StringBytes>(index: Field)``BoundedVec<u8, StringBytes>`**: When the current root is an array, returns the string at `index`; errors if `index` is out of bounds or the element is not a string.
179+
180+
- **`get_string_from_path<let KeyBytes, let StringBytes, let PathDepth>(keys: [BoundedVec<u8, KeyBytes>; PathDepth])``Option<BoundedVec<u8, StringBytes>>`**: Traverses nested objects by the given keys; returns Some(string) if the final value exists and is a string, otherwise None.
181+
182+
88183
# Edge Cases
89184

90185
### single value JSON

0 commit comments

Comments
 (0)