| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text.Json; |
| | 5 | | using System.Text.Json.Serialization; |
| | 6 | |
|
| | 7 | | namespace Plainquire.Sort.JsonConverters; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// <see cref="EntitySort{TEntity}"/> specific JSON converter for Microsoft (System.Text.Json) JSON. |
| | 11 | | /// Implements <see cref="JsonConverter{T}" /> |
| | 12 | | /// </summary> |
| | 13 | | /// <typeparam name="TEntity">The type of the entity be sorted.</typeparam> |
| | 14 | | /// <seealso cref="JsonConverter{T}" /> |
| | 15 | | public class EntitySortConverter<TEntity> : JsonConverter<EntitySort<TEntity>> |
| | 16 | | { |
| | 17 | | /// <inheritdoc /> |
| | 18 | | public override EntitySort<TEntity> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions option |
| 105 | 19 | | => EntitySortConverter.Read<EntitySort<TEntity>>(ref reader, options); |
| | 20 | |
|
| | 21 | | /// <inheritdoc /> |
| | 22 | | public override void Write(Utf8JsonWriter writer, EntitySort<TEntity> value, JsonSerializerOptions options) |
| 102 | 23 | | => EntitySortConverter.Write(writer, value, options); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// <see cref="EntitySort"/> specific JSON converter for Microsoft (System.Text.Json) JSON. |
| | 28 | | /// Implements <see cref="JsonConverter{T}" /> |
| | 29 | | /// </summary> |
| | 30 | | /// <seealso cref="JsonConverter{T}" /> |
| | 31 | | public class EntitySortConverter : JsonConverter<EntitySort> |
| | 32 | | { |
| | 33 | | /// <inheritdoc /> |
| | 34 | | public override EntitySort Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 35 | | => Read<EntitySort>(ref reader, options); |
| | 36 | |
|
| | 37 | | /// <inheritdoc /> |
| | 38 | | public override void Write(Utf8JsonWriter writer, EntitySort value, JsonSerializerOptions options) |
| | 39 | | => Write(writer, value, options); |
| | 40 | |
|
| | 41 | | internal static TEntitySort Read<TEntitySort>(ref Utf8JsonReader reader, JsonSerializerOptions options) |
| | 42 | | where TEntitySort : EntitySort, new() |
| | 43 | | { |
| | 44 | | var entitySortData = JsonSerializer.Deserialize<EntitySortConverterData>(ref reader, options) ?? new EntitySortC |
| | 45 | | var propertySorts = GetPropertySorts(entitySortData); |
| | 46 | |
|
| | 47 | | return new TEntitySort |
| | 48 | | { |
| | 49 | | PropertySorts = propertySorts ?? [], |
| | 50 | | Configuration = entitySortData.Configuration |
| | 51 | | }; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | internal static void Write<TEntitySort>(Utf8JsonWriter writer, TEntitySort value, JsonSerializerOptions options) |
| | 55 | | where TEntitySort : EntitySort |
| | 56 | | { |
| | 57 | | var propertySortData = GetPropertySortData(value); |
| | 58 | |
|
| | 59 | | var entitySortData = new EntitySortConverterData |
| | 60 | | { |
| | 61 | | PropertySorts = propertySortData, |
| | 62 | | Configuration = value.Configuration |
| | 63 | | }; |
| | 64 | |
|
| | 65 | | JsonSerializer.Serialize(writer, entitySortData, options); |
| | 66 | | } |
| | 67 | |
|
| | 68 | | internal static List<PropertySort>? GetPropertySorts(EntitySortConverterData entitySortData) |
| | 69 | | => entitySortData |
| | 70 | | .PropertySorts? |
| | 71 | | .Select(filter => new PropertySort( |
| | 72 | | propertyPath: filter.PropertyPath, |
| | 73 | | direction: filter.Direction, |
| | 74 | | position: filter.Position, |
| | 75 | | configuration: entitySortData.Configuration |
| | 76 | | )) |
| | 77 | | .ToList(); |
| | 78 | |
|
| | 79 | | internal static List<PropertySortConverterData> GetPropertySortData<TEntitySort>(TEntitySort entitySort) where TEnti |
| | 80 | | => entitySort.PropertySorts |
| | 81 | | .Select(filter => new PropertySortConverterData |
| | 82 | | ( |
| | 83 | | propertyPath: filter.PropertyPath, |
| | 84 | | direction: filter.Direction, |
| | 85 | | position: filter.Position |
| | 86 | | )) |
| | 87 | | .ToList(); |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// <see cref="EntitySort{TEntity}"/> specific JSON converter factory for Microsoft (System.Text.Json) JSON. |
| | 91 | | /// Implements <see cref="JsonConverterFactory" /> |
| | 92 | | /// </summary> |
| | 93 | | /// <seealso cref="JsonConverterFactory" /> |
| | 94 | | public class Factory : JsonConverterFactory |
| | 95 | | { |
| | 96 | | /// <inheritdoc /> |
| | 97 | | public override bool CanConvert(Type typeToConvert) |
| | 98 | | => typeToConvert.IsGenericEntitySort(); |
| | 99 | |
|
| | 100 | | /// <inheritdoc /> |
| | 101 | | public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| | 102 | | { |
| | 103 | | var entityType = typeToConvert.GetGenericArguments()[0]; |
| | 104 | | var entitySortConverterType = typeof(EntitySortConverter<>).MakeGenericType(entityType); |
| | 105 | | return (JsonConverter?)Activator.CreateInstance(entitySortConverterType); |
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |