-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f3d17d
commit 4746661
Showing
224 changed files
with
956 additions
and
275 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
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
File renamed without changes.
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,5 +1,7 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
#nullable enable | ||
|
||
namespace Mercoa.Client; | ||
|
||
public class BankAddress | ||
|
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
using Mercoa.Client; | ||
|
||
#nullable enable | ||
|
||
namespace Mercoa.Client; | ||
|
||
public class CommonsClient | ||
|
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,5 +1,7 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
#nullable enable | ||
|
||
namespace Mercoa.Client; | ||
|
||
public class Address | ||
|
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,5 +1,7 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
#nullable enable | ||
|
||
namespace Mercoa.Client; | ||
|
||
public class BirthDate | ||
|
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,5 +1,7 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
#nullable enable | ||
|
||
namespace Mercoa.Client; | ||
|
||
public class FullName | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
#nullable enable | ||
|
||
namespace Mercoa.Client; | ||
|
||
public class PhoneNumber | ||
|
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,5 +1,7 @@ | ||
using Mercoa.Client.Core; | ||
|
||
#nullable enable | ||
|
||
namespace Mercoa.Client; | ||
|
||
public partial class ClientOptions | ||
|
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,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Mercoa.Client; | ||
|
||
/// <summary> | ||
/// Json collection converter. | ||
/// </summary> | ||
/// <typeparam name="TDatatype">Type of item to convert.</typeparam> | ||
/// <typeparam name="TConverterType">Converter to use for individual items.</typeparam> | ||
public class CollectionItemSerializer<TDatatype, TConverterType> | ||
: JsonConverter<IEnumerable<TDatatype>> | ||
where TConverterType : JsonConverter | ||
{ | ||
/// <summary> | ||
/// Reads a json string and deserializes it into an object. | ||
/// </summary> | ||
/// <param name="reader">Json reader.</param> | ||
/// <param name="typeToConvert">Type to convert.</param> | ||
/// <param name="options">Serializer options.</param> | ||
/// <returns>Created object.</returns> | ||
public override IEnumerable<TDatatype> Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
return default(IEnumerable<TDatatype>); | ||
} | ||
|
||
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(options); | ||
jsonSerializerOptions.Converters.Clear(); | ||
jsonSerializerOptions.Converters.Add(Activator.CreateInstance<TConverterType>()); | ||
|
||
List<TDatatype> returnValue = new List<TDatatype>(); | ||
|
||
while (reader.TokenType != JsonTokenType.EndArray) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartArray) | ||
{ | ||
returnValue.Add( | ||
(TDatatype) | ||
JsonSerializer.Deserialize( | ||
ref reader, | ||
typeof(TDatatype), | ||
jsonSerializerOptions | ||
) | ||
); | ||
} | ||
|
||
reader.Read(); | ||
} | ||
|
||
return returnValue; | ||
} | ||
|
||
/// <summary> | ||
/// Writes a json string. | ||
/// </summary> | ||
/// <param name="writer">Json writer.</param> | ||
/// <param name="value">Value to write.</param> | ||
/// <param name="options">Serializer options.</param> | ||
public override void Write( | ||
Utf8JsonWriter writer, | ||
IEnumerable<TDatatype> value, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNullValue(); | ||
return; | ||
} | ||
|
||
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(options); | ||
jsonSerializerOptions.Converters.Clear(); | ||
jsonSerializerOptions.Converters.Add(Activator.CreateInstance<TConverterType>()); | ||
|
||
writer.WriteStartArray(); | ||
|
||
foreach (TDatatype data in value) | ||
{ | ||
JsonSerializer.Serialize(writer, data, jsonSerializerOptions); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
} |
75 changes: 0 additions & 75 deletions
75
src/Mercoa.Client/Core/JsonEnumMemberStringEnumConverter.cs
This file was deleted.
Oops, something went wrong.
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,67 @@ | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using OneOf; | ||
|
||
namespace Mercoa.Client; | ||
|
||
public class OneOfSerializer<TOneOf> : JsonConverter<TOneOf> | ||
where TOneOf : IOneOf | ||
{ | ||
public override TOneOf Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
if (reader.TokenType is JsonTokenType.Null) | ||
return default; | ||
|
||
foreach (var (type, cast) in s_types) | ||
{ | ||
try | ||
{ | ||
Utf8JsonReader readerCopy = reader; | ||
var result = JsonSerializer.Deserialize(ref readerCopy, type, options); | ||
reader.Skip(); | ||
return (TOneOf)cast.Invoke(null, new[] { result })!; | ||
} | ||
catch (JsonException) { } | ||
} | ||
|
||
throw new JsonException( | ||
$"Cannot deserialize into one of the supported types for {typeToConvert}" | ||
); | ||
} | ||
|
||
private static readonly (Type type, MethodInfo cast)[] s_types = GetOneOfTypes(); | ||
|
||
public override void Write(Utf8JsonWriter writer, TOneOf value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value.Value, options); | ||
} | ||
|
||
private static (Type type, MethodInfo cast)[] GetOneOfTypes() | ||
{ | ||
var casts = typeof(TOneOf) | ||
.GetRuntimeMethods() | ||
.Where(m => m.IsSpecialName && m.Name == "op_Implicit") | ||
.ToArray(); | ||
var type = typeof(TOneOf); | ||
while (type != null) | ||
{ | ||
if ( | ||
type.IsGenericType | ||
&& (type.Name.StartsWith("OneOf`") || type.Name.StartsWith("OneOfBase`")) | ||
) | ||
{ | ||
return type.GetGenericArguments() | ||
.Select(t => (t, casts.First(c => c.GetParameters()[0].ParameterType == t))) | ||
.ToArray(); | ||
} | ||
|
||
type = type.BaseType; | ||
} | ||
throw new InvalidOperationException($"{typeof(TOneOf)} isn't OneOf or OneOfBase"); | ||
} | ||
} |
Oops, something went wrong.