Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register JsonSerializerOptions in the IoC Container #15328

Merged
merged 15 commits into from
Feb 21, 2024
Merged
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.AdminMenu.Services;
using OrchardCore.Deployment;
using OrchardCore.Json;

namespace OrchardCore.AdminMenu.Deployment
{
Expand All @@ -14,15 +12,10 @@ public class AdminMenuDeploymentSource : IDeploymentSource
private readonly IAdminMenuService _adminMenuService;
private readonly JsonSerializerOptions _serializationOptions;

public AdminMenuDeploymentSource(IAdminMenuService adminMenuService, IOptions<JsonDerivedTypesOptions> derivedTypesOptions)
public AdminMenuDeploymentSource(IAdminMenuService adminMenuService, IOptions<JsonSerializerOptions> serializationOptions)
{
_adminMenuService = adminMenuService;

// The recipe step contains polymorphic types which need to be resolved
_serializationOptions = new()
{
TypeInfoResolver = new PolymorphicJsonTypeInfoResolver(derivedTypesOptions.Value)
};
_serializationOptions = serializationOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.AdminMenu.Services;
using OrchardCore.Json;
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;

Expand All @@ -20,15 +18,12 @@ public class AdminMenuStep : IRecipeStepHandler
private readonly IAdminMenuService _adminMenuService;
private readonly JsonSerializerOptions _serializationOptions;

public AdminMenuStep(IAdminMenuService adminMenuService, IOptions<JsonDerivedTypesOptions> derivedTypesOptions)
public AdminMenuStep(IAdminMenuService adminMenuService, IOptions<JsonSerializerOptions> serializationOptions)
{
_adminMenuService = adminMenuService;

// The recipe step contains polymorphic types (menu items) which need to be resolved
_serializationOptions = new()
{
TypeInfoResolver = new PolymorphicJsonTypeInfoResolver(derivedTypesOptions.Value)
};
_serializationOptions = serializationOptions.Value;
}

public async Task ExecuteAsync(RecipeExecutionContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.Deployment;

Expand All @@ -9,10 +11,14 @@ namespace OrchardCore.ContentTypes.Deployment
public class ContentDefinitionDeploymentSource : IDeploymentSource
{
private readonly IContentDefinitionStore _contentDefinitionStore;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public ContentDefinitionDeploymentSource(IContentDefinitionStore contentDefinitionStore)
public ContentDefinitionDeploymentSource(
IContentDefinitionStore contentDefinitionStore,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_contentDefinitionStore = contentDefinitionStore;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand All @@ -37,8 +43,8 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
result.Steps.Add(new JsonObject
{
["name"] = "ContentDefinition",
["ContentTypes"] = JArray.FromObject(contentTypes),
["ContentParts"] = JArray.FromObject(contentParts),
["ContentTypes"] = JArray.FromObject(contentTypes, _jsonSerializerOptions),
["ContentParts"] = JArray.FromObject(contentParts, _jsonSerializerOptions),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.Deployment;

Expand All @@ -9,10 +11,14 @@ namespace OrchardCore.ContentTypes.Deployment
public class ReplaceContentDefinitionDeploymentSource : IDeploymentSource
{
private readonly IContentDefinitionStore _contentDefinitionStore;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public ReplaceContentDefinitionDeploymentSource(IContentDefinitionStore contentDefinitionStore)
public ReplaceContentDefinitionDeploymentSource(
IContentDefinitionStore contentDefinitionStore,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_contentDefinitionStore = contentDefinitionStore;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand All @@ -37,8 +43,8 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
result.Steps.Add(new JsonObject
{
["name"] = "ReplaceContentDefinition",
["ContentTypes"] = JArray.FromObject(contentTypes),
["ContentParts"] = JArray.FromObject(contentParts),
["ContentTypes"] = JArray.FromObject(contentTypes, _jsonSerializerOptions),
["ContentParts"] = JArray.FromObject(contentParts, _jsonSerializerOptions),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.Deployment;

Expand All @@ -9,10 +11,14 @@ namespace OrchardCore.Contents.Deployment.AddToDeploymentPlan
public class ContentItemDeploymentSource : IDeploymentSource
{
private readonly IContentManager _contentManager;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public ContentItemDeploymentSource(IContentManager contentManager)
public ContentItemDeploymentSource(
IContentManager contentManager,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_contentManager = contentManager;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand All @@ -31,7 +37,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
return;
}

var jContentItem = JObject.FromObject(contentItem);
var jContentItem = JObject.FromObject(contentItem, _jsonSerializerOptions);
jContentItem.Remove(nameof(ContentItem.Id));

var contentStep = result.Steps.FirstOrDefault(s => s["name"]?.ToString() == "Content");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using OrchardCore.Deployment;
Expand All @@ -10,10 +12,14 @@ namespace OrchardCore.Contents.Deployment
public class AllContentDeploymentSource : IDeploymentSource
{
private readonly ISession _session;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public AllContentDeploymentSource(ISession session)
public AllContentDeploymentSource(
ISession session,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_session = session;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand All @@ -34,7 +40,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan

foreach (var contentItem in await _session.Query<ContentItem, ContentItemIndex>(x => x.Published).ListAsync())
{
var objectData = JObject.FromObject(contentItem);
var objectData = JObject.FromObject(contentItem, _jsonSerializerOptions);

// Don't serialize the Id as it could be interpreted as an updated object when added back to YesSql
objectData.Remove(nameof(ContentItem.Id));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using OrchardCore.Deployment;
Expand All @@ -11,10 +13,14 @@ namespace OrchardCore.Contents.Deployment
public class ContentDeploymentSource : IDeploymentSource
{
private readonly ISession _session;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public ContentDeploymentSource(ISession session)
public ContentDeploymentSource(
ISession session,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_session = session;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand All @@ -32,7 +38,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan

foreach (var contentItem in await _session.Query<ContentItem, ContentItemIndex>(x => x.Published && x.ContentType.IsIn(contentStep.ContentTypes)).ListAsync())
{
var objectData = JObject.FromObject(contentItem);
var objectData = JObject.FromObject(contentItem, _jsonSerializerOptions);

// Don't serialize the Id as it could be interpreted as an updated object when added back to YesSql.
objectData.Remove(nameof(ContentItem.Id));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using OrchardCore.Deployment;
Expand All @@ -17,15 +19,18 @@ public class ExportContentToDeploymentTargetDeploymentSource : IDeploymentSource
private readonly IContentManager _contentManager;
private readonly ISession _session;
private readonly IUpdateModelAccessor _updateModelAccessor;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public ExportContentToDeploymentTargetDeploymentSource(
IContentManager contentManager,
ISession session,
IUpdateModelAccessor updateModelAccessor)
IUpdateModelAccessor updateModelAccessor,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_contentManager = contentManager;
_session = session;
_updateModelAccessor = updateModelAccessor;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand All @@ -52,7 +57,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
var contentItem = await _contentManager.GetAsync(model.ContentItemId, model.Latest ? VersionOptions.Latest : VersionOptions.Published);
if (contentItem != null)
{
var objectData = JObject.FromObject(contentItem);
var objectData = JObject.FromObject(contentItem, _jsonSerializerOptions);
objectData.Remove(nameof(ContentItem.Id));
data.Add(objectData);
}
Expand All @@ -64,7 +69,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan

foreach (var contentItem in checkedContentItems)
{
var objectData = JObject.FromObject(contentItem);
var objectData = JObject.FromObject(contentItem, _jsonSerializerOptions);
objectData.Remove(nameof(ContentItem.Id));
data.Add(objectData);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.CustomSettings.Services;
using OrchardCore.Deployment;

Expand All @@ -10,10 +12,14 @@ namespace OrchardCore.CustomSettings.Deployment
public class CustomSettingsDeploymentSource : IDeploymentSource
{
private readonly CustomSettingsService _customSettingsService;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public CustomSettingsDeploymentSource(CustomSettingsService customSettingsService)
public CustomSettingsDeploymentSource(
CustomSettingsService customSettingsService,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_customSettingsService = customSettingsService;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand Down Expand Up @@ -44,7 +50,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
foreach (var settingsType in settingsTypes)
{
var settings = await _customSettingsService.GetSettingsAsync(settingsType);
settingsList.Add(new(settings.ContentType, JObject.FromObject(settings)));
settingsList.Add(new(settings.ContentType, JObject.FromObject(settings, _jsonSerializerOptions)));
}

// Adding custom settings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;

namespace OrchardCore.Deployment.Deployment
{
public class DeploymentPlanDeploymentSource : IDeploymentSource
{
private readonly IDeploymentPlanService _deploymentPlanService;
private readonly IEnumerable<IDeploymentStepFactory> _deploymentStepFactories;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public DeploymentPlanDeploymentSource(
IDeploymentPlanService deploymentPlanService,
IEnumerable<IDeploymentStepFactory> deploymentStepFactories)
IEnumerable<IDeploymentStepFactory> deploymentStepFactories,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_deploymentPlanService = deploymentPlanService;
_deploymentStepFactories = deploymentStepFactories;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep deploymentStep, DeploymentPlanResult result)
Expand Down Expand Up @@ -52,7 +57,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep deploymentStep, Depl
result.Steps.Add(new JsonObject
{
["name"] = "deployment",
["Plans"] = JArray.FromObject(plans),
["Plans"] = JArray.FromObject(plans, _jsonSerializerOptions),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ public class CustomFileDeploymentSource : IDeploymentSource
{
public Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
{
var customFile = step as CustomFileDeploymentStep;

if (customFile == null)
if (step is not CustomFileDeploymentStep customFile)
{
return Task.CompletedTask;
}
Expand Down
Loading
Loading