Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#if NET6_0_OR_GREATER
namespace Stripe.Infrastructure
{
using System;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

/// <summary>
/// Converts a <see cref="Emptyable{T}"/> to and from JSON.
/// </summary>
/// <typeparam name="T">Type of the field when expanded.</typeparam>
internal class STJEmptyableConverter<T> : JsonConverter<Emptyable<T>>
{
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="options">The calling serializer's options.</param>
public override void Write(Utf8JsonWriter writer, Emptyable<T> value, JsonSerializerOptions options)
{
switch (value)
{
case null:
break;

case Emptyable<T> emptyable:
if (emptyable.Empty)
{
writer.WriteNullValue();
}
else
{
JsonSerializer.Serialize(writer, emptyable.Value, options);
}

break;

default:
throw new JsonException(string.Format(
"Unexpected value when converting Emptyable. Expected Emptyable, got {0}.",
value.GetType()));
}
}

/// <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)
{
if (!objectType.IsGenericType)
{
return false;
}

return typeof(IEmptyable).GetTypeInfo().IsAssignableFrom(objectType.GetGenericTypeDefinition());
}

/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="Utf8JsonReader"/> to read from.</param>
/// <param name="typeToConvert">Type of the object.</param>
/// <param name="options">The calling serializer's options.</param>
/// <returns>The object value.</returns>
public override Emptyable<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException("Cannot deserialize Emptyable objects.");
}
}
}
#endif
10 changes: 7 additions & 3 deletions src/Stripe.net/Services/_common/Emptyable.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Stripe
{
/// <summary>Represents a generic expandable field.</summary>
/// <typeparam name="T">Type of the field when expanded.</typeparam>
internal class Emptyable<T>
/// <summary>Represents a field that might be emptyble.</summary>
/// <typeparam name="T">Type of the field when not empty.</typeparam>
internal class Emptyable<T> : IEmptyable<T>
{
private bool empty;
private T value;
Expand All @@ -29,5 +29,9 @@ public bool Empty
}
}
}

/// <summary>Gets or sets the expanded object.</summary>
/// <value>The expanded object.</value>
object IEmptyable.Value => this.Value;
}
}
25 changes: 25 additions & 0 deletions src/Stripe.net/Services/_interfaces/IEmptyable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Stripe
{
/// <summary>
/// Represents a value that may be of one of several different types.
/// </summary>
public interface IEmptyable
{
/// <summary>Gets whether or not the field is empty.</summary>
/// <returns>True if the value is empty; false if the value is set.</returns>
public bool Empty { get; }

/// <summary>Gets the value of the current <see cref="IEmptyable"/> object.</summary>
/// <returns>The value of the current <see cref="IEmptyable"/> object.</returns>
object Value { get; }
}

/// <summary>Represents a generic expandable field.</summary>
/// <typeparam name="T">Type of the field when expanded.</typeparam>
public interface IEmptyable<T> : IEmptyable
{
/// <summary>Gets the value of the current <see cref="IEmptyable"/> object.</summary>
/// <returns>The value of the current <see cref="IEmptyable"/> object.</returns>
new T Value { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ public void Check()
continue;
}

if (stripeClass.Name.StartsWith("V1Billing"))
{
Debugger.Break();
}

if (stripeClass.IsGenericType)
{
// Handle generic types (container types) separately, because
Expand Down
5 changes: 5 additions & 0 deletions src/StripeTests/Wholesome/SystemTextJsonTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public static Tuple<string, string> HasCorrectConverterType(Type type, MemberInf
{
expectedConverterType = typeof(STJUnixDateTimeConverter);
}
else if (typeof(IEmptyable).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
{
expectedConverterType = typeof(STJEmptyableConverter<>);
expectedGenericTypeArguments = type.GenericTypeArguments;
}
else if (typeof(IAnyOf).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
{
expectedConverterType = typeof(STJAnyOfConverter);
Expand Down
Loading