Skip to content

Commit

Permalink
Fix Json serializer to be converting consistently - making enums seri…
Browse files Browse the repository at this point in the history
…alize as integers
  • Loading branch information
einari committed Sep 18, 2024
1 parent 5d53c7c commit eb38492
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Source/DotNET/Applications/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text.Json.Serialization;
using Cratis.Applications.ModelBinding;
using Cratis.Json;
using Cratis.Reflection;
Expand Down Expand Up @@ -40,12 +41,23 @@ public static IServiceCollection AddControllersFromProjectReferencedAssembles(th
.AddTenancy()
.AddCQRS();
})
.AddJsonOptions(_ =>
.AddJsonOptions(options =>
{
_.JsonSerializerOptions.PropertyNamingPolicy = AcronymFriendlyJsonCamelCaseNamingPolicy.Instance;
options.JsonSerializerOptions.PropertyNamingPolicy = AcronymFriendlyJsonCamelCaseNamingPolicy.Instance;
// Find and remove the JsonStringEnumConverter if it exists, we want to have integers for enums. The Json converter in Fundamentals gives us integer in transport.
var converterToRemove = options.JsonSerializerOptions.Converters
.OfType<JsonStringEnumConverter>()
.FirstOrDefault();
if (converterToRemove != null)
{
options.JsonSerializerOptions.Converters.Remove(converterToRemove);
}
foreach (var converter in Globals.JsonSerializerOptions!.Converters)
{
_.JsonSerializerOptions.Converters.Add(converter);
options.JsonSerializerOptions.Converters.Add(converter);
}
});

Expand Down

0 comments on commit eb38492

Please sign in to comment.