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

Add AllowTrailingCommas to JsonSerializerOptions #4082

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Source/ACE.Common/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void Initialize(string path = @"Config.js")

var fileText = File.ReadAllText(pathToUse);

Config = JsonSerializer.Deserialize<MasterConfiguration>(fileText, new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip, NumberHandling = JsonNumberHandling.AllowReadingFromString });
Config = JsonSerializer.Deserialize<MasterConfiguration>(fileText, SerializerOptions);
}
catch (Exception exception)
{
Expand All @@ -72,5 +72,13 @@ public static void Initialize(string path = @"Config.js")
throw;
}
}

public static JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
{
AllowTrailingCommas = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true
};
}
}
4 changes: 2 additions & 2 deletions Source/ACE.Server/Factories/StarterGearFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

using ACE.Common;
using ACE.Server.Entity;

using log4net;
Expand Down Expand Up @@ -36,7 +36,7 @@ private static StarterGearConfiguration LoadConfigFromResource()
{
var starterGearText = File.ReadAllText(starterGearFile);

config = JsonSerializer.Deserialize<StarterGearConfiguration>(starterGearText, new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip, NumberHandling = JsonNumberHandling.AllowReadingFromString });
config = JsonSerializer.Deserialize<StarterGearConfiguration>(starterGearText, ConfigManager.SerializerOptions);

return config;
}
Expand Down
4 changes: 3 additions & 1 deletion Source/ACE.Server/Mods/ModContainer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using ACE.Common;

using log4net;

using McMaster.NETCore.Plugins;
Expand Down Expand Up @@ -204,7 +206,7 @@ private bool TryCreateModInstance()

public void SaveMetadata()
{
var json = JsonSerializer.Serialize(Meta, new JsonSerializerOptions { WriteIndented = true });
var json = JsonSerializer.Serialize(Meta, ConfigManager.SerializerOptions);
var info = new FileInfo(MetadataPath);

if (!info.RetryWrite(json))
Expand Down
4 changes: 2 additions & 2 deletions Source/ACE.Server/Mods/ModManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ACE.Common;
using ACE.Server.Managers;
using ACE.Server.WorldObjects;

Expand All @@ -9,7 +10,6 @@
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ACE.Server.Mods
{
Expand Down Expand Up @@ -144,7 +144,7 @@ private static bool TryLoadModContainer(string metadataPath, out ModContainer co

try
{
var metadata = JsonSerializer.Deserialize<ModMetadata>(File.ReadAllText(metadataPath), new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip, NumberHandling = JsonNumberHandling.AllowReadingFromString });
var metadata = JsonSerializer.Deserialize<ModMetadata>(File.ReadAllText(metadataPath), ConfigManager.SerializerOptions);

container = new ModContainer()
{
Expand Down
5 changes: 2 additions & 3 deletions Source/ACE.Server/Program_Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO.Compression;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;

using ACE.Common;
Expand Down Expand Up @@ -38,7 +37,7 @@ private static void DoOutOfBoxSetup(string configFile)
}

var fileText = File.ReadAllText(configFile);
config = JsonSerializer.Deserialize<MasterConfiguration>(fileText, new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip, NumberHandling = JsonNumberHandling.AllowReadingFromString });
config = JsonSerializer.Deserialize<MasterConfiguration>(fileText, ConfigManager.SerializerOptions);
}

Console.WriteLine("Performing setup for ACEmulator...");
Expand Down Expand Up @@ -260,7 +259,7 @@ private static void DoOutOfBoxSetup(string configFile)

Console.WriteLine("commiting configuration to disk...");

var jsonString = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
var jsonString = JsonSerializer.Serialize(config, ConfigManager.SerializerOptions);
File.WriteAllText(configFile, jsonString);


Expand Down