Skip to content

Commit

Permalink
Add AllowTrailingCommas to JsonSerializerOptions (#4082)
Browse files Browse the repository at this point in the history
Additionally moved all serializers to single static instance in ACE.Common
  • Loading branch information
LtRipley36706 authored Jan 2, 2024
1 parent a41e9a1 commit 0d6f9dd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
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

0 comments on commit 0d6f9dd

Please sign in to comment.