Skip to content
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 #8979

Merged
merged 4 commits into from Nov 15, 2021

Conversation

hazeycode
Copy link
Sponsor Contributor

Fixed version of #8908 with added test to parse stringify output

@hazeycode
Copy link
Sponsor Contributor Author

Found another failure case. Needs more testing.

@daurnimator
Copy link
Collaborator

I wonder if a better option might be a skip_defaults to avoid rendering all defaults.
Then you could opt-in per optional field by setting their default value to null.

lib/std/json.zig Outdated Show resolved Hide resolved
@daurnimator daurnimator added the standard library This issue involves writing Zig code for the standard library. label Jun 4, 2021
@hazeycode
Copy link
Sponsor Contributor Author

hazeycode commented Jun 6, 2021

I wonder if a better option might be a skip_defaults to avoid rendering all defaults.
Then you could opt-in per optional field by setting their default value to null.

I'm unconvinced. It does make sense but is there a use-case where you don't want to stringify nulls that go OTW but would want to stringify some local default that doesn't make sense as a default on the other end? i.e.

const Message = struct {
    user_id: u32 = 1337, // should stringify
    optional: []const u8 = null, // should not stringify 
    required: []const u8, // should stringify
}

Of course, the problem here is easily avoided, however, not convinced that it's a good user experience.

@hazeycode
Copy link
Sponsor Contributor Author

This appears to be working properly now!
Commits likely want squashing but I had all kinds of bother trying to rebase

if (child_options.whitespace) |child_whitespace| {
try out_stream.writeByte('\n');
try child_whitespace.outputIndent(out_stream);
var emit_field = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks redundant with field_output

@andrewrk andrewrk merged commit 5d2a77c into ziglang:master Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants