Optimize JSON parsing of unions #2741
Merged
+123
−2
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.
This includes two things:
Int32 | Nil
toJSON.mapping
, instead of having to use the more verbose{type: Int32, nilable: true}
.I did a small benchmark for this:
Before the optimization:
After the optimization:
So I'd say these optimizations, even though they make the code just a bit longer, are very good :-)
Point 1 means we can now do:
instead of:
That's definitely better. But, wouldn't it be nice, since this notation is used in other places, to be able to just write:
I understand that it's simpler not adding parser support for
Int32?
outside the type grammar, but one of Crystal's philosophy is pragmatism and developer happiness. I believe parsingT?
asUnion(T, ::Nil)
everywhere will make things less verbose and more consistent. I'll send that in a separate PR and later we can see if we merge it.