-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Json Stringify option to not write out null optional fields #8908
Conversation
Please add a test. |
With this option, would the resulting JSON also be parsed correctly? |
Good question. I think as it stands std.json.parse would fail if an optional field isn't present. I'd need to double check. If this is the case though, it should be possible to fix by have it check against a comptime array of required fields for type and set missing optional fields to null |
You may also want to add this as an optional feature to the parser as well. |
FWIW I was originally intending on supporting "missing" fields by doing a union with void. const std = @import("std");
const Foo = struct {
not_optional: []const u8 = "required",
bar: struct {
optional: union (enum) {
data: []const u8,
missing: void,
} = .{.missing = {}},
} = .{},
};
test "union with void" {
const writer = std.io.getStdOut().writer();
try std.json.stringify(Foo{}, .{}, writer);
try writer.writeAll("\n");
} |
I'm not sure that it should be an optional feature of the parser. I think the parser should always set optional fields to null on result if they are not present in the json stream. In the case where the user wants to treat this as an error, they can do so simply by checking if the field on the typed result is null |
The current with having to declare a default value of |
If a web service gives them missing fields, shouldn't std.json.parse just work even if the user forgot to set defaults on their zig struct? Or perhaps they even want to default to a value other than null. I'm not sure but it seems like relying on default values leads to bad user experience. |
It would mean their specification of the object is wrong and likely code dealing with it needs to be updated aswell to correctly conform to the API. Being more lenient feels like it would cause more errors than it should. This also matches with how you initialize the object yourself with
This is what is done now with
Having better error reporting from std.json itself using #8584 (and possibly the extension to modules) or something similar should improve the experience enough to not be a problem. Currently the debugging experience isn't so great as error sets don't return enough information to find the issue without testing your way forward; automatically setting missing fields feels like it's trying to solve the issue from the wrong direction. |
What happens currently vs what do you expect should happen if we
for the stream "{}" |
This is what I would expect from the parser which is the current behaviour. |
Cool. I think I can agree that default values should not be overwritten. I had had my mind set on that they would be. |
Further testing revealed these changes only work in the very simplest of case. Closing for now |
Example usage:
test 1 output:
{"not_optional":"required","bar":{"optional":null}}
test 2 output:
{"not_optional":"required","bar":{}}