-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Support more Literal types in JSON::Serializable's use_json_discriminator macro #9222
Support more Literal types in JSON::Serializable's use_json_discriminator macro #9222
Conversation
… used for keys in use_json_discriminator
Thank you! Do you have an example API in mind where this can be useful? |
@asterite My specific use case is discriminating Channel objects from Discord's API into their respective types. Discord uses integer enums explicitly to describe object types. I tried implementing an enum similar to the example I give in the original comment, but the old implementation expects a string over an integer for the field value. Implementing |
Strange, I think enum should support deserialization from int and string, instead of this PR. |
The main issue is that previously |
Oooooh... got it! Sorry, I should have looked at the link you sent, I was on mobile. Yeah, it makes perfect sense then. 🙏 |
src/json/serialization.cr
Outdated
when {{key.id.stringify}} | ||
{% else %} | ||
{% if key.is_a?(StringLiteral) %} | ||
when {{key.id.stringify}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think key.id.stringify
is just key
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you're right! I just kept it this way because I was looking at the NamedTupleLiteral
stringification.
pardon my asking @bcardiff, but is this going in 1.0.0? |
Probably should do this for |
Probably. Didn't think of it/need it when I opened this PR. I may get around to it in some time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, this felt out of my radar so far.
Looks good. I have an optional comment for completeness.
It needs a rebase, but I am ok to include for 1.0
src/json/serialization.cr
Outdated
@@ -393,7 +393,7 @@ module JSON | |||
def self.new(pull : ::JSON::PullParser) | |||
location = pull.location | |||
|
|||
discriminator_value = nil | |||
discriminator_value : String | Int64 | Bool | Nil = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we don't need Float64 here, but it seems the atomic values of JSON::Any are wanted here, except for Float64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable really doesn't need any type restrictions at all.
This PR supports the following Literal types as keys for hashes passed to
use_json_discriminator
:StringLiteral
NumberLiteral
BoolLiteral
Path
This allows more types to be expected from a discriminator value. Previously,
use_json_discriminator
was hard-coded to expect a string value. String, boolean, and integer values are now supported. An example: