-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
77 additions
and
2 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
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,68 @@ | ||
using Newtonsoft.Json.Converters; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Firebase.Auth.Requests.Converters | ||
{ | ||
/// <inheritdoc /> | ||
/// <summary> | ||
/// Defaults enum values to the base value if | ||
/// </summary> | ||
public class DefaultEnumConverter : StringEnumConverter | ||
{ | ||
/// <summary> | ||
/// The default value used to fallback on when a enum is not convertable. | ||
/// </summary> | ||
private readonly int defaultValue; | ||
|
||
/// <inheritdoc /> | ||
/// <summary> | ||
/// Default constructor. Defaults the default value to 0. | ||
/// </summary> | ||
public DefaultEnumConverter() | ||
{ } | ||
|
||
/// <inheritdoc /> | ||
/// <summary> | ||
/// Sets the default value for the enum value. | ||
/// </summary> | ||
/// <param name="defaultValue">The default value to use.</param> | ||
public DefaultEnumConverter(int defaultValue) | ||
{ | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <summary> | ||
/// Reads the provided JSON and attempts to convert using StringEnumConverter. If that fails set the value to the default value. | ||
/// </summary> | ||
/// <param name="reader">Reads the JSON value.</param> | ||
/// <param name="objectType">Current type that is being converted.</param> | ||
/// <param name="existingValue">The existing value being read.</param> | ||
/// <param name="serializer">Instance of the JSON Serializer.</param> | ||
/// <returns>The deserialized value of the enum if it exists or the default value if it does not.</returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
try | ||
{ | ||
return base.ReadJson(reader, objectType, existingValue, serializer); | ||
} | ||
catch | ||
{ | ||
return Enum.Parse(objectType, $"{defaultValue}"); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <summary> | ||
/// Validates that this converter can handle the type that is being provided. | ||
/// </summary> | ||
/// <param name="objectType">The type of the object being converted.</param> | ||
/// <returns>True if the base class says so, and if the value is an enum and has a default value to fall on.</returns> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return base.CanConvert(objectType) && objectType.GetTypeInfo().IsEnum && Enum.IsDefined(objectType, defaultValue); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...h/Requests/JavaScriptDateTimeConverter.cs → ...Converters/JavaScriptDateTimeConverter.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
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