-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added comma split enum string json converter
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
CryptoExchange.Net/Converters/SystemTextJson/CommaSplitEnumConverter.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,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace CryptoExchange.Net.Converters.SystemTextJson | ||
{ | ||
/// <summary> | ||
/// Converter for comma seperated enum values | ||
/// </summary> | ||
public class CommaSplitEnumConverter<T> : JsonConverter<IEnumerable<T>> where T : Enum | ||
{ | ||
/// <inheritdoc /> | ||
public override IEnumerable<T>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return (reader.GetString()?.Split(',').Select(x => EnumConverter.ParseString<T>(x)).ToArray() ?? new T[0])!; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, IEnumerable<T> value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(string.Join(",", value.Select(x => EnumConverter.GetString(x)))); | ||
} | ||
} | ||
} |
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