Skip to content

Commit

Permalink
ReferenceDataMultiDictionaryConverterFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
chullybun committed Jan 14, 2024
1 parent 8f0d72d commit 29901e9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Represents the **NuGet** versions.
- *Enhancement*: Upgraded `UnitTestEx` dependency to `4.0.2` to enable _isolated_ function testing.
- *Enhancement*: Enabled `IJsonSerializer` support for `CompositeKey` JSON serialization/deserialization.
- *Enhancement*: Added `IEventDataFormatter` which when implemented by the value set as the `EventData.Value` allows additional formatting to be applied by the `EventDataFormatter`.
- *Fixed*: `EventDataFormatter` and `CloudEventSerializerBase` updated to correctly set the `Key` where applicable.
- *Fixed*: Added `ReferenceDataMultiDictionaryConverterFactory` to ensure each `IReferenceDataCollection` is serialized correctly according to its underlying type.
- *Fixed*: `EventDataFormatter` and `CloudEventSerializerBase` updated to correctly set the `Key` property where applicable.
- *Internal:* Upgraded `NUnit` dependency to `4.0.1` for all `CoreEx` unit test; also, all unit tests now leverage the [_NUnit constraint model_](https://docs.nunit.org/articles/nunit/writing-tests/assertions/assertion-models/constraint.html) testing approach.

## v3.8.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void C100_Named()
using var test = ApiTester.Create<Startup>().UseJsonSerializer(new ReferenceDataContentJsonSerializer().ToUnitTestEx());

var r = test.Controller<ReferenceDataController>()
.Run(c => c.GetNamed(), new HttpRequestOptions { UrlQueryString = "gender&usstate" })
.Run(c => c.GetNamed(), requestOptions: new HttpRequestOptions { UrlQueryString = "gender&usstate" })
.AssertOK();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/CoreEx/Text/Json/ReferenceDataContentJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ReferenceDataContentJsonSerializer(Stj.JsonSerializerOptions? optio
/// <item><description><see cref="Stj.JsonSerializerOptions.WriteIndented"/> = <c>false</c></description></item>
/// <item><description><see cref="Stj.JsonSerializerOptions.DictionaryKeyPolicy"/> = <see cref="SubstituteNamingPolicy.Substitute"/>.</description></item>
/// <item><description><see cref="Stj.JsonSerializerOptions.PropertyNamingPolicy"/> = <see cref="SubstituteNamingPolicy.Substitute"/>.</description></item>
/// <item><description><see cref="Stj.JsonSerializerOptions.Converters"/> = <see cref="JsonStringEnumConverter"/>, <see cref="ExceptionConverterFactory"/>, <see cref="CollectionResultConverterFactory"/> and <see cref="ResultConverterFactory"/>.</description></item>
/// <item><description><see cref="Stj.JsonSerializerOptions.Converters"/> = <see cref="JsonStringEnumConverter"/>, <see cref="ExceptionConverterFactory"/>, <see cref="CollectionResultConverterFactory"/>, <see cref="ResultConverterFactory"/> and <see cref="ReferenceDataMultiDictionaryConverterFactory"/>.</description></item>
/// </list>
/// </remarks>
public static new Stj.JsonSerializerOptions DefaultOptions { get; set; } = new Stj.JsonSerializerOptions(Stj.JsonSerializerDefaults.Web)
Expand All @@ -32,7 +32,7 @@ public class ReferenceDataContentJsonSerializer(Stj.JsonSerializerOptions? optio
WriteIndented = false,
DictionaryKeyPolicy = SubstituteNamingPolicy.Substitute,
PropertyNamingPolicy = SubstituteNamingPolicy.Substitute,
Converters = { new JsonStringEnumConverter(), new ExceptionConverterFactory(), new CollectionResultConverterFactory(), new ResultConverterFactory() }
Converters = { new JsonStringEnumConverter(), new ExceptionConverterFactory(), new CollectionResultConverterFactory(), new ResultConverterFactory(), new ReferenceDataMultiDictionaryConverterFactory() }
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx

using CoreEx.RefData;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace CoreEx.Text.Json
{
/// <summary>
/// Performs JSON value conversion for <see cref="ReferenceDataMultiDictionary"/> values.
/// </summary>
/// <remarks>This is required to ensure each <see cref="IReferenceDataCollection"/> is serialized correctly according to its underlying type.</remarks>
public class ReferenceDataMultiDictionaryConverterFactory : JsonConverterFactory
{
/// <inheritdoc/>
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(ReferenceDataMultiDictionary);

/// <inheritdoc/>
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) => new ReferenceDataMultiDictionaryConverter();

/// <summary>
/// Performs the "actual" JSON value conversion for <see cref="ReferenceDataMultiDictionary"/> values.
/// </summary>
private class ReferenceDataMultiDictionaryConverter : JsonConverter<ReferenceDataMultiDictionary>
{
/// <inheritdoc/>
public override ReferenceDataMultiDictionary Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotSupportedException($"Deserialization of Type {nameof(ReferenceDataMultiDictionary)} is not supported.");

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, ReferenceDataMultiDictionary value, JsonSerializerOptions options)
{
writer.WriteStartObject();

foreach (var kvp in value)
{
writer.WritePropertyName(options.DictionaryKeyPolicy?.ConvertName(kvp.Key) ?? kvp.Key);
System.Text.Json.JsonSerializer.Serialize(writer, kvp.Value, kvp.Value.GetType(), options);
}

writer.WriteEndObject();
}
}
}
}

0 comments on commit 29901e9

Please sign in to comment.