Add default "null" to JSON::Builder#field
#10563
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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).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 ofnull
.Resolves #5218