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

Add default "null" to JSON::Builder#field #10563

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

straight-shoota
Copy link
Member

The method JSON::Builder#field(name, &) expects the block to append a value to the builder. If this doesn't happen (for example because the block is empty), the builder enters an invalid state.

This patch adds detection for this.

  • It adds a default null value if the builder state still expects the name of an object property (which suggests the block didn't do anything to the builder).
  • It raises on any other than ObjectState, because that indicates the block appended an incomplete value.

This could be even further enhanced and actually check the validity of the state stack. But that's hardly necessary.
The main use case is code that for some reason just forgets appending a property value in the block.
Defaulting to null makes that it still produces valid JSON output which can then be used to investigate the error if a value was expected instead of null.

Resolves #5218

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

Successfully merging this pull request may close these issues.

Sanity check for JSON::Builder.field(name, &block)
1 participant