Serializer that uses tabs

This commit is contained in:
Noah Pilarski 2022-08-08 18:36:23 -04:00
parent 22f4063ca0
commit c2483a3271
2 changed files with 34 additions and 3 deletions

View File

@ -225,9 +225,7 @@ namespace NewHorizons.Utility.DebugMenu
var relativePath = filePath.Replace(loadedMod.ModHelper.Manifest.ModFolderPath, "");
var json = JsonConvert.SerializeObject(loadedConfigFiles[filePath], jsonSettings);
// Add the schema line
json = "{\n\t\"$schema\": \"https://raw.githubusercontent.com/xen-42/outer-wilds-new-horizons/main/NewHorizons/Schemas/body_schema.json\"," + json.Substring(1);
var json = loadedConfigFiles[filePath].ToSerializedJson();
try
{

View File

@ -1,15 +1,48 @@
using NewHorizons.External.Configs;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using NomaiCoordinates = NewHorizons.External.Configs.StarSystemConfig.NomaiCoordinates;
namespace NewHorizons.Utility
{
public static class NewHorizonsExtensions
{
private static JsonSerializer jsonSerializer = new JsonSerializer
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
Formatting = Formatting.Indented,
};
private static StringBuilder stringBuilder = new StringBuilder();
public static string ToSerializedJson(this PlanetConfig planetConfig)
{
string json = "{}";
using (StringWriter stringWriter = new StringWriter(stringBuilder))
{
using (JsonTextWriter jsonTextWriter = new JsonTextWriter(stringWriter)
{
Formatting = Formatting.Indented,
IndentChar = '\t',
Indentation = 1
})
{
jsonSerializer.Serialize(jsonTextWriter, planetConfig);
json = "{\n\t\"$schema\": \"https://raw.githubusercontent.com/xen-42/outer-wilds-new-horizons/main/NewHorizons/Schemas/body_schema.json\"," + stringBuilder.ToString().Substring(1);
stringBuilder.Clear();
}
}
return json;
}
public static MVector3 ToMVector3(this Vector3 vector3)
{
return new MVector3(vector3.x, vector3.y, vector3.z);