-
Notifications
You must be signed in to change notification settings - Fork 573
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
New FromJson
method
#1484
New FromJson
method
#1484
Conversation
413ef53
to
de68dc1
Compare
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 looks good to me, I think at least! A bit sad we have to explicitly pass the class name to StripeEntity
though
I could add a method that instantiates the correct type based on the |
Do you think it'd be better? It would prevent deserializing some hashes that are not real API resources right? |
de68dc1
to
2cc2852
Compare
2cc2852
to
da91233
Compare
Such a method could only deserialize "real" API resources, i.e. resources with an To be clear, all of these are just convenience helper methods. We won't be relying on them internally, we're just exposing them to users to make it a bit simpler to manually deserialize resources. |
Can we have an async equivalent of this method? |
@bugejakurt Unfortunately, I don't think that'd be possible. The library uses Newtonsoft.Json to deserialize JSON, and Newtonsoft.Json's EDIT: unless you meant adding overload methods that accept a |
r? @remi-stripe
cc @stripe/api-libraries
Adds a new
FromJson()
utility method to deserialize JSON strings into Stripe objects.The method can be used in two ways:
var charge = StripeEntity.FromJson<Charge>(json);
var charge = Charge.FromJson(json);
(The latter is made possible by using the curiously recurring template pattern.)
EDIT: Also possible:
var charge = StripeEntity.FromJson(json)
. At compile time,charge
will be anIHasObject
, and will be instantiated to the correct concrete type at runtime depending on the contents ofjson
. Of course this only works if the JSON string has anobject
key with a known value, otherwise this returnsnull
.