File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -176,6 +176,28 @@ export fn add() i32 {
176
176
}
177
177
```
178
178
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
+
179
201
``` bash
180
202
extism call ./zig-out/bin/my-plugin.wasm add --input=' {"a": 20, "b": 21}'
181
203
# => {"sum":41}
You can’t perform that action at this time.
0 commit comments