-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AnyOf<> generic class to handle polymorphic parameters
- Loading branch information
Showing
16 changed files
with
665 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/Stripe.net/Infrastructure/JsonConverters/AnyOfConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
namespace Stripe.Infrastructure | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
/// <summary> | ||
/// Converts a <see cref="IAnyOf"/> to JSON. | ||
/// </summary> | ||
public class AnyOfConverter : JsonConverter | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON. | ||
/// </summary> | ||
/// <value> | ||
/// <c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>. | ||
/// </value> | ||
public override bool CanWrite => true; | ||
|
||
/// <summary> | ||
/// Writes the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> | ||
/// <param name="value">The value.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
switch (value) | ||
{ | ||
case null: | ||
serializer.Serialize(writer, null); | ||
break; | ||
|
||
case IAnyOf anyOf: | ||
serializer.Serialize(writer, anyOf.GetValue()); | ||
break; | ||
|
||
default: | ||
throw new JsonSerializationException(string.Format( | ||
"Unexpected value when converting AnyOf. Expected IAnyOf, got {0}.", | ||
value.GetType())); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Reads the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <param name="existingValue">The existing value of object being read.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
/// <returns>The object value.</returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.Null) | ||
{ | ||
return null; | ||
} | ||
|
||
object o = null; | ||
|
||
// Try to deserialize with each possible type | ||
var jToken = JToken.Load(reader); | ||
foreach (var type in objectType.GenericTypeArguments) | ||
{ | ||
try | ||
{ | ||
using (var subReader = jToken.CreateReader()) | ||
{ | ||
o = serializer.Deserialize(subReader, type); | ||
} | ||
|
||
// If deserialization succeeds, stop | ||
break; | ||
} | ||
catch (JsonException) | ||
{ | ||
// Do nothing, just try the next type | ||
} | ||
} | ||
|
||
if (o == null) | ||
{ | ||
throw new JsonSerializationException(string.Format( | ||
"Cannot deserialize the current JSON object into any of the expected types ({0}).", | ||
string.Join(", ", objectType.GenericTypeArguments.Select(t => t.FullName)))); | ||
} | ||
|
||
return Activator.CreateInstance(objectType, o); | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether this instance can convert the specified object type. | ||
/// </summary> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <returns> | ||
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>. | ||
/// </returns> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(IAnyOf).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,39 @@ | ||
namespace Stripe | ||
{ | ||
using Newtonsoft.Json; | ||
using Stripe.Infrastructure; | ||
|
||
public class TokenCreateOptions : BaseOptions | ||
{ | ||
/// <summary> | ||
/// The customer (owned by the application's account) for which to create a token. For use | ||
/// only with <a href="https://stripe.com/docs/connect">Stripe Connect</a>. Also, this can | ||
/// be used only with an <a href="https://stripe.com/docs/connect/standard-accounts">OAuth | ||
/// access token</a> or | ||
/// <a href="https://stripe.com/docs/connect/authentication">Stripe-Account header</a>. For | ||
/// more details, see <a href="https://stripe.com/docs/connect/shared-customers">Shared | ||
/// Customers</a>. | ||
/// </summary> | ||
[JsonProperty("customer")] | ||
public string CustomerId { get; set; } | ||
|
||
/// <summary> | ||
/// The card this token will represent. If you also pass in a customer, the card must be the | ||
/// ID of a card belonging to the customer. Otherwise, if you do not pass in a customer, | ||
/// this is a <see cref="CreditCardOptions"/> instance containing a user's credit card | ||
/// details. | ||
/// </summary> | ||
[JsonProperty("card")] | ||
public CreditCardOptions Card { get; set; } | ||
|
||
[JsonProperty("card")] | ||
public string CardId { get; set; } | ||
|
||
[JsonProperty("bank_account")] | ||
public BankAccountOptions BankAccount { get; set; } | ||
public AnyOf<string, CreditCardOptions> Card { get; set; } | ||
|
||
/// <summary> | ||
/// The bank account this token will represent. If you also pass in a customer, the bank | ||
/// account must be the ID of a bank account belonging to the customer. Otherwise, if you do | ||
/// not pass in a customer, this is a <see cref="BankAccountOptions"/> instance containing a | ||
/// user's bank account details. | ||
/// </summary> | ||
[JsonProperty("bank_account")] | ||
public string BankAccountId { get; set; } | ||
[JsonConverter(typeof(AnyOfConverter))] | ||
public AnyOf<string, BankAccountOptions> BankAccount { get; set; } | ||
} | ||
} |
Oops, something went wrong.