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,35 @@
namespace Stripe.Infrastructure
{
using System;
using Newtonsoft.Json;

/// <summary>
/// Newtonsoft converter that serializes any <see cref="StringEnum"/>
/// subclass as its <see cref="StringEnum.Value"/> string.
/// </summary>
internal class NewtonsoftStringEnumConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(StringEnum).IsAssignableFrom(objectType);
}
Comment thread
mbroshi-stripe marked this conversation as resolved.

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(((StringEnum)value).Value);
}
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotSupportedException(
$"Deserialization of {objectType.Name} (StringEnum) is not supported.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Stripe.Infrastructure
{
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

/// <summary>
/// STJ converter factory that serializes any <see cref="StringEnum"/>
/// subclass as its <see cref="StringEnum.Value"/> string.
/// </summary>
internal class STJStringEnumConverterFactory : JsonConverterFactory
Comment thread
mbroshi-stripe marked this conversation as resolved.
{
public override bool CanConvert(Type typeToConvert)
{
return typeof(StringEnum).IsAssignableFrom(typeToConvert);
}

public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
return (JsonConverter)Activator.CreateInstance(
typeof(STJStringEnumConverterInner<>).MakeGenericType(typeToConvert));
}
Comment thread
mbroshi-stripe marked this conversation as resolved.

private class STJStringEnumConverterInner<T> : JsonConverter<T>
where T : StringEnum
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException(
$"Deserialization of {typeToConvert.Name} (StringEnum) is not supported.");
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.Value);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Stripe
{
using STJS = System.Text.Json.Serialization;

[STJS.JsonConverter(typeof(Infrastructure.STJStringEnumConverterFactory))]
public class UpcomingInvoiceSubscriptionResumeAt : StringEnum
{
/// <summary>When viewing an upcoming invoice for a subscription, simulates it resuming to the current time.</summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Stripe
{
using STJS = System.Text.Json.Serialization;

[STJS.JsonConverter(typeof(Infrastructure.STJStringEnumConverterFactory))]
public class SubscriptionBillingCycleAnchor : StringEnum
{
/// <summary>Resets the subscription's billing cycle anchor to the current time.</summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Stripe.net/Services/_base/StringEnum.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Stripe
{
using System.Text.Json.Serialization;

/// <summary>
/// Abstract base class for string enum parameters.
/// </summary>
Expand All @@ -25,6 +23,8 @@ namespace Stripe
/// }
/// </code>
/// </example>
[Newtonsoft.Json.JsonConverter(typeof(Infrastructure.NewtonsoftStringEnumConverter))]
[NoSystemTextJsonAttributesNeeded("STJ converter is on each concrete subclass because STJ does not inherit converter attributes from base classes.")]
public abstract class StringEnum
{
/// <summary>Initializes a new instance of the <see cref="StringEnum"/> class.</summary>
Expand All @@ -36,7 +36,7 @@ protected StringEnum(string value)

/// <summary>Gets or sets the serialized value.</summary>
/// <returns>The serialized value.</returns>
[JsonPropertyName("Value")]
[System.Text.Json.Serialization.JsonPropertyName("Value")]
public string Value { get; protected set; }

/// <summary>Returns the serialized value.</summary>
Expand Down
Loading