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: support @[json_null] tag to enforce null, when encoding none option values #23319

Merged
merged 2 commits into from
Dec 30, 2024

Conversation

felipensp
Copy link
Member

@felipensp felipensp commented Dec 30, 2024

Implementing feature request.

Huly®: V_0.6-21751

@spytheman spytheman changed the title json: add @[json_is_null] attr to enforce null when encoding none option values json: add @[json_null] attr to enforce null when encoding none option values Dec 30, 2024
@spytheman spytheman changed the title json: add @[json_null] attr to enforce null when encoding none option values json: support @[json_null] tag to enforce null, when encoding none option values Dec 30, 2024
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

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

Excellent work.

@enghitalo
Copy link
Contributor

What about just @[nulltable] so this can make sense to others encoders as well

@enghitalo
Copy link
Contributor

enghitalo commented Dec 30, 2024

IMHO, @[required], or @[required; skip], or @[required; omitempty] should do the same work (What I think would be better)

@spytheman
Copy link
Member

IMHO, @[required], or @[required; skip], or @[required; omitempty] should do the same work (What I think would be better)

No. The current name @[json_null] is better - it has json in it, and so it is restricted to only the json modules. This avoids the conflict with the decisions of other modules, that may also do serialization.

@spytheman
Copy link
Member

omitempty in particular does the opposite, and is only valid for non option fields.

@spytheman spytheman marked this pull request as ready for review December 30, 2024 18:05
@JalonSolov
Copy link
Contributor

Logically, @[omitempty] should apply to Option fields as well. If the Option is set to none (the default), then it is "empty". Same as a string field set to ''.

@spytheman
Copy link
Member

The default for option fields, with a value of none, is that they are already omitted when serialized to json => adding a @[omitempty] tag to them is redundant to that default, and non effective for them.

@spytheman spytheman merged commit 8cdb507 into vlang:master Dec 30, 2024
72 checks passed
@felipensp felipensp deleted the json_is_null_attr branch December 30, 2024 19:14
@JalonSolov
Copy link
Contributor

That sounds like "magic". It would make more sense to output null unless omitempty was used.

@spytheman
Copy link
Member

No, it does not sound like "magic" - the default behavior is to not produce anything for option values that are none, and that saves resources, and that is not changed by the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants