-
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
33 changed files
with
834 additions
and
126 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 and from 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
13 changes: 9 additions & 4 deletions
13
src/Stripe.net/Services/BankAccounts/BankAccountCreateOptions.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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
namespace Stripe | ||
{ | ||
using Newtonsoft.Json; | ||
using Stripe.Infrastructure; | ||
|
||
public class BankAccountCreateOptions : BaseOptions | ||
{ | ||
/// <summary> | ||
/// REQUIRED. Either a token, like the ones returned by | ||
/// <a href="https://stripe.com/docs/stripe.js">Stripe.js</a>, or a | ||
/// <see cref="SourceBankAccount"/> instance containing a user’s bank account | ||
/// details. | ||
/// </summary> | ||
[JsonProperty("source")] | ||
public string SourceToken { get; set; } | ||
|
||
[JsonProperty("source")] | ||
public SourceBankAccount SourceBankAccount { get; set; } | ||
[JsonConverter(typeof(AnyOfConverter))] | ||
public AnyOf<string, SourceBankAccount> Source { get; set; } | ||
} | ||
} |
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
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
Oops, something went wrong.