From 2ea054dc722e65821d77cab4759fb6738a21c246 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Tue, 11 Oct 2022 08:34:33 -0400 Subject: [PATCH] fix #2435 - remove NewtonSoft.Json dependency --- .../Controllers/PackageController.cs | 8 +- Oqtane.Server/Infrastructure/ConfigManager.cs | 81 ++++++++++++------- .../Infrastructure/DatabaseManager.cs | 3 +- Oqtane.Server/Oqtane.Server.csproj | 1 - Oqtane.Server/Startup.cs | 1 - 5 files changed, 55 insertions(+), 39 deletions(-) diff --git a/Oqtane.Server/Controllers/PackageController.cs b/Oqtane.Server/Controllers/PackageController.cs index d290d60b..ebba1ff7 100644 --- a/Oqtane.Server/Controllers/PackageController.cs +++ b/Oqtane.Server/Controllers/PackageController.cs @@ -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(jsonTextReader); - } + return await JsonSerializer.DeserializeAsync(stream); } } return default(T); diff --git a/Oqtane.Server/Infrastructure/ConfigManager.cs b/Oqtane.Server/Infrastructure/ConfigManager.cs index a9390912..85278929 100644 --- a/Oqtane.Server/Infrastructure/ConfigManager.cs +++ b/Oqtane.Server/Infrastructure/ConfigManager.cs @@ -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(string key, dynamic jsonObj, T value, string action) + private void SetValueRecursively(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); + } } } } diff --git a/Oqtane.Server/Infrastructure/DatabaseManager.cs b/Oqtane.Server/Infrastructure/DatabaseManager.cs index e0694417..0889a84c 100644 --- a/Oqtane.Server/Infrastructure/DatabaseManager.cs +++ b/Oqtane.Server/Infrastructure/DatabaseManager.cs @@ -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(databases), true); + _configManager.AddOrUpdateSetting(SettingKeys.AvailableDatabasesSection, databases, true); } } } diff --git a/Oqtane.Server/Oqtane.Server.csproj b/Oqtane.Server/Oqtane.Server.csproj index 886290c5..153c28fd 100644 --- a/Oqtane.Server/Oqtane.Server.csproj +++ b/Oqtane.Server/Oqtane.Server.csproj @@ -34,7 +34,6 @@ - diff --git a/Oqtane.Server/Startup.cs b/Oqtane.Server/Startup.cs index 7b349ec4..dac8d85c 100644 --- a/Oqtane.Server/Startup.cs +++ b/Oqtane.Server/Startup.cs @@ -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