-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add getJson and outputJson for plugin io util #25
Conversation
const FromJson = Json(T); | ||
const input = FromJson{ .parsed = out, .slice = bytes }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know of a way to better handle this such that the end-user can get the automatic type from the JSON, and have enough control over freeing the memory so it doesn't deallocate before a value is used.
In the example here:
const Input = struct {
name: []const u8,
age: u16,
};
export fn json_input() i32 {
const plugin = Plugin.init(allocator);
const json = plugin.getJson(Input, .{}) catch unreachable; // you must call .deinit() when done
defer json.deinit();
const input: Input = json.value();
const out = std.fmt.allocPrint(allocator, "Hello, {s}. You are {d} years old!\n", .{ input.name, input.age }) catch unreachable;
plugin.output(out);
return 0;
}
The input.name
(a []const u8
) would be freed unless json.deinit()
is called after this function returns and the value is used outside this function. Initially I thought I could handle this for the end-user, but I don't think I can.
So, this returns the Json
wrapped type, with methods .value()
(to get the parsed struct), and .deinit()
to give the end-user the ability to free the slice used to parse the struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can avoid this by using alloc_always
when parsing the JSON:
std.json.parseFromSlice(Config, allocator, data, .{.allocate = .alloc_always})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zshipko thanks, this works great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, now there is a getJson
which returns the comptime T
and handles the memory management. kind of the "expected / easy-mode" path.
and, there's a getJsonOpt
which is the previous implementation and allows the end user to control the parsing and allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Going to update the README to match these changes (and today's updates to Zig 😆) and will merge. |
Looks like
zig@master
changed some build APIs, so i'll figure that out and update this PR (and likely the SDK repo too)