Skip to content

Commit 37418e0

Browse files
authored
docs: add section about json helpers
1 parent 3f59e07 commit 37418e0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,28 @@ export fn add() i32 {
176176
}
177177
```
178178

179+
To use a json helper, you can accomplish the same as the above with:
180+
181+
```zig
182+
export fn add() i32 {
183+
const Add = struct {
184+
a: i32,
185+
b: i32,
186+
};
187+
188+
const Sum = struct {
189+
sum: i32,
190+
};
191+
192+
const plugin = Plugin.init(allocator);
193+
// automatically deserialize & alloc/free the json input to `Add` struct
194+
const add = plugin.getJson(Add) catch unreachable;
195+
const sum = Sum{ .sum = add.a + add.b };
196+
// automatically serialize and alloc/free the `Sum` struct to json and send it to the output
197+
plugin.outputJson(sum, .{}) catch unreachable;
198+
return 0;
199+
```
200+
179201
```bash
180202
extism call ./zig-out/bin/my-plugin.wasm add --input='{"a": 20, "b": 21}'
181203
# => {"sum":41}

0 commit comments

Comments
 (0)