Smaller JSON fixes

This commit is contained in:
erri120 2020-04-12 18:06:37 +02:00
parent 137e9e73d1
commit 64c2780a6f
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135

View File

@ -1,11 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Wabbajack.Common.Serialization.Json; using Wabbajack.Common.Serialization.Json;
@ -19,7 +17,7 @@ namespace Wabbajack.Common
{ {
new HashJsonConverter(), new HashJsonConverter(),
new RelativePathConverter(), new RelativePathConverter(),
new AbolutePathConverter(), new AbsolutePathConverter(),
new HashRelativePathConverter(), new HashRelativePathConverter(),
new FullPathConverter(), new FullPathConverter(),
new GameConverter(), new GameConverter(),
@ -33,7 +31,7 @@ namespace Wabbajack.Common
Converters = Converters}; Converters = Converters};
public static JsonSerializerSettings GenericJsonSettings => public static JsonSerializerSettings GenericJsonSettings =>
new JsonSerializerSettings { }; new JsonSerializerSettings();
public static void ToJson<T>(this T obj, string filename) public static void ToJson<T>(this T obj, string filename)
@ -62,16 +60,12 @@ namespace Wabbajack.Common
return JsonConvert.SerializeObject(obj, JsonSettings); return JsonConvert.SerializeObject(obj, JsonSettings);
} }
public static T FromJson<T>(this AbsolutePath filename, public static T FromJson<T>(this AbsolutePath filename)
TypeNameHandling handling = TypeNameHandling.All,
TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
{ {
return JsonConvert.DeserializeObject<T>(filename.ReadAllText(), JsonSettings)!; return JsonConvert.DeserializeObject<T>(filename.ReadAllText(), JsonSettings)!;
} }
public static T FromJsonString<T>(this string data, public static T FromJsonString<T>(this string data)
TypeNameHandling handling = TypeNameHandling.Objects,
TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
{ {
return JsonConvert.DeserializeObject<T>(data, JsonSettings)!; return JsonConvert.DeserializeObject<T>(data, JsonSettings)!;
} }
@ -116,7 +110,7 @@ namespace Wabbajack.Common
} }
} }
private class AbolutePathConverter : JsonConverter<AbsolutePath> private class AbsolutePathConverter : JsonConverter<AbsolutePath>
{ {
public override void WriteJson(JsonWriter writer, AbsolutePath value, JsonSerializer serializer) public override void WriteJson(JsonWriter writer, AbsolutePath value, JsonSerializer serializer)
{ {
@ -246,11 +240,11 @@ namespace Wabbajack.Common
{ {
private static Dictionary<string, Type> _nameToType = new Dictionary<string, Type>(); private static Dictionary<string, Type> _nameToType = new Dictionary<string, Type>();
private static Dictionary<Type, string> _typeToName = new Dictionary<Type, string>(); private static Dictionary<Type, string> _typeToName = new Dictionary<Type, string>();
private static bool _inited = false; private static bool _init;
public JsonNameSerializationBinder() public JsonNameSerializationBinder()
{ {
if (_inited) if (_init)
return; return;
var customDisplayNameTypes = var customDisplayNameTypes =
@ -280,7 +274,7 @@ namespace Wabbajack.Common
_typeToName = _nameToType.ToDictionary( _typeToName = _nameToType.ToDictionary(
t => t.Value, t => t.Value,
t => t.Key); t => t.Key);
_inited = true; _init = true;
} }