fix #2435 - remove NewtonSoft.Json dependency

This commit is contained in:
Shaun Walker 2022-10-11 08:34:33 -04:00
parent 2e32b65421
commit 2ea054dc72
5 changed files with 55 additions and 39 deletions

View File

@ -1,7 +1,6 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Oqtane.Models;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
@ -12,6 +11,7 @@ using Oqtane.Shared;
using Oqtane.Infrastructure;
using Oqtane.Enums;
using System.Net.Http.Headers;
using System.Text.Json;
// ReSharper disable PartialTypeWithSinglePart
namespace Oqtane.Controllers
@ -106,11 +106,7 @@ namespace Oqtane.Controllers
var stream = await response.Content.ReadAsStreamAsync();
using (var streamReader = new StreamReader(stream))
{
using (var jsonTextReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
return serializer.Deserialize<T>(jsonTextReader);
}
return await JsonSerializer.DeserializeAsync<T>(stream);
}
}
return default(T);

View File

@ -1,9 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Oqtane.Shared;
namespace Oqtane.Infrastructure
@ -52,9 +52,9 @@ namespace Oqtane.Infrastructure
try
{
var path = Path.Combine(Directory.GetCurrentDirectory(), file);
dynamic jsonObj = JsonConvert.DeserializeObject(File.ReadAllText(path));
SetValueRecursively(key, jsonObj, value, "set");
File.WriteAllText(path, JsonConvert.SerializeObject(jsonObj, Formatting.Indented));
JsonNode node = JsonNode.Parse(File.ReadAllText(path));
SetValueRecursively(node, key, value);
File.WriteAllText(path, JsonSerializer.Serialize(node, new JsonSerializerOptions() { WriteIndented = true }));
if (reload) Reload();
}
catch (Exception ex)
@ -73,9 +73,9 @@ namespace Oqtane.Infrastructure
try
{
var path = Path.Combine(Directory.GetCurrentDirectory(), file);
dynamic jsonObj = JsonConvert.DeserializeObject(File.ReadAllText(path));
SetValueRecursively(key, jsonObj, "", "remove");
File.WriteAllText(path, JsonConvert.SerializeObject(jsonObj, Formatting.Indented));
JsonNode node = JsonNode.Parse(File.ReadAllText(path));
RemovePropertyRecursively(node, key);
File.WriteAllText(path, JsonSerializer.Serialize(node, new JsonSerializerOptions() { WriteIndented = true }));
if (reload) Reload();
}
catch (Exception ex)
@ -84,30 +84,53 @@ namespace Oqtane.Infrastructure
}
}
private void SetValueRecursively<T>(string key, dynamic jsonObj, T value, string action)
private void SetValueRecursively<T>(JsonNode json, string key, T value)
{
var remainingSections = key.Split(":", 2);
if (json != null && key != null && value != null)
{
var remainingSections = key.Split(":", 2);
var currentSection = remainingSections[0];
if (remainingSections.Length > 1)
{
var nextSection = remainingSections[1];
jsonObj[currentSection] ??= new JObject();
SetValueRecursively(nextSection, jsonObj[currentSection], value, action);
}
else
{
switch (action)
var currentSection = remainingSections[0];
if (remainingSections.Length > 1)
{
case "set":
jsonObj[currentSection] = JToken.FromObject(value);
break;
case "remove":
if (jsonObj.Property(currentSection) != null)
{
jsonObj.Property(currentSection).Remove();
}
break;
var nextSection = remainingSections[1];
SetValueRecursively(json[currentSection] ??= new JsonObject(), nextSection, value);
}
else
{
if (value.GetType() == typeof(string) && (value.ToString()!.StartsWith("[") || value.ToString()!.StartsWith("{")))
{
json[currentSection] = JsonNode.Parse(value.ToString()!);
}
else
{
json[currentSection] = JsonValue.Create(value);
}
}
}
}
private void RemovePropertyRecursively(JsonNode json, string key)
{
if (json != null && key != null)
{
var remainingSections = key.Split(":", 2);
var currentSection = remainingSections[0];
if (remainingSections.Length > 1)
{
var nextSection = remainingSections[1];
if (json[currentSection] != null)
{
RemovePropertyRecursively(json[currentSection], nextSection);
}
}
else
{
if (json.AsObject().ContainsKey(currentSection))
{
json.AsObject().Remove(currentSection);
}
}
}
}

View File

@ -16,7 +16,6 @@ using Oqtane.Models;
using Oqtane.Repository;
using Oqtane.Shared;
using Oqtane.Enums;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
// ReSharper disable MemberCanBePrivate.Global
@ -825,7 +824,7 @@ namespace Oqtane.Infrastructure
databases += "{ \"Name\": \"MySQL\", \"ControlType\": \"Oqtane.Installer.Controls.MySQLConfig, Oqtane.Client\", \"DBTYpe\": \"Oqtane.Database.MySQL.SqlServerDatabase, Oqtane.Database.MySQL\" },";
databases += "{ \"Name\": \"PostgreSQL\", \"ControlType\": \"Oqtane.Installer.Controls.PostgreSQLConfig, Oqtane.Client\", \"DBTYpe\": \"Oqtane.Database.PostgreSQL.PostgreSQLDatabase, Oqtane.Database.PostgreSQL\" }";
databases += "]";
_configManager.AddOrUpdateSetting(SettingKeys.AvailableDatabasesSection, JsonConvert.DeserializeObject<dynamic>(databases), true);
_configManager.AddOrUpdateSetting(SettingKeys.AvailableDatabasesSection, databases, true);
}
}
}

View File

@ -34,7 +34,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.1.0" />

View File

@ -133,7 +133,6 @@ namespace Oqtane
{
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
})
.AddNewtonsoftJson()
.AddOqtaneApplicationParts() // register any Controllers from custom modules
.ConfigureOqtaneMvc(); // any additional configuration from IStartup classes