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

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Oqtane.Shared; using Oqtane.Shared;
namespace Oqtane.Infrastructure namespace Oqtane.Infrastructure
@ -52,9 +52,9 @@ namespace Oqtane.Infrastructure
try try
{ {
var path = Path.Combine(Directory.GetCurrentDirectory(), file); var path = Path.Combine(Directory.GetCurrentDirectory(), file);
dynamic jsonObj = JsonConvert.DeserializeObject(File.ReadAllText(path)); JsonNode node = JsonNode.Parse(File.ReadAllText(path));
SetValueRecursively(key, jsonObj, value, "set"); SetValueRecursively(node, key, value);
File.WriteAllText(path, JsonConvert.SerializeObject(jsonObj, Formatting.Indented)); File.WriteAllText(path, JsonSerializer.Serialize(node, new JsonSerializerOptions() { WriteIndented = true }));
if (reload) Reload(); if (reload) Reload();
} }
catch (Exception ex) catch (Exception ex)
@ -73,9 +73,9 @@ namespace Oqtane.Infrastructure
try try
{ {
var path = Path.Combine(Directory.GetCurrentDirectory(), file); var path = Path.Combine(Directory.GetCurrentDirectory(), file);
dynamic jsonObj = JsonConvert.DeserializeObject(File.ReadAllText(path)); JsonNode node = JsonNode.Parse(File.ReadAllText(path));
SetValueRecursively(key, jsonObj, "", "remove"); RemovePropertyRecursively(node, key);
File.WriteAllText(path, JsonConvert.SerializeObject(jsonObj, Formatting.Indented)); File.WriteAllText(path, JsonSerializer.Serialize(node, new JsonSerializerOptions() { WriteIndented = true }));
if (reload) Reload(); if (reload) Reload();
} }
catch (Exception ex) 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]; var currentSection = remainingSections[0];
if (remainingSections.Length > 1) if (remainingSections.Length > 1)
{
var nextSection = remainingSections[1];
jsonObj[currentSection] ??= new JObject();
SetValueRecursively(nextSection, jsonObj[currentSection], value, action);
}
else
{
switch (action)
{ {
case "set": var nextSection = remainingSections[1];
jsonObj[currentSection] = JToken.FromObject(value); SetValueRecursively(json[currentSection] ??= new JsonObject(), nextSection, value);
break; }
case "remove": else
if (jsonObj.Property(currentSection) != null) {
{ if (value.GetType() == typeof(string) && (value.ToString()!.StartsWith("[") || value.ToString()!.StartsWith("{")))
jsonObj.Property(currentSection).Remove(); {
} json[currentSection] = JsonNode.Parse(value.ToString()!);
break; }
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.Repository;
using Oqtane.Shared; using Oqtane.Shared;
using Oqtane.Enums; using Oqtane.Enums;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
// ReSharper disable MemberCanBePrivate.Global // 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\": \"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 += "{ \"Name\": \"PostgreSQL\", \"ControlType\": \"Oqtane.Installer.Controls.PostgreSQLConfig, Oqtane.Client\", \"DBTYpe\": \"Oqtane.Database.PostgreSQL.PostgreSQLDatabase, Oqtane.Database.PostgreSQL\" }";
databases += "]"; databases += "]";
_configManager.AddOrUpdateSetting(SettingKeys.AvailableDatabasesSection, JsonConvert.DeserializeObject<dynamic>(databases), true); _configManager.AddOrUpdateSetting(SettingKeys.AvailableDatabasesSection, databases, true);
} }
} }
} }

View File

@ -34,7 +34,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.3" /> <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.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.Identity.EntityFrameworkCore" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.1.0" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="4.1.0" />

View File

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