移除Newtonsoft.Json
This commit is contained in:
parent
facfbbcf73
commit
866be14168
Binary file not shown.
@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86371939112254644a31e78df202d79c
|
||||
@ -1,20 +1,20 @@
|
||||
using Newtonsoft.Json;
|
||||
//using Newtonsoft.Json;
|
||||
|
||||
namespace StoicGoose.Common.Extensions
|
||||
{
|
||||
public static class ObjectExtensionMethods
|
||||
{
|
||||
/* https://dotnetcoretutorials.com/2020/09/09/cloning-objects-in-c-and-net-core/ */
|
||||
public static T Clone<T>(this T source)
|
||||
{
|
||||
if (source is null) return default;
|
||||
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source, new JsonSerializerSettings()
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
||||
}), new JsonSerializerSettings()
|
||||
{
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
//namespace StoicGoose.Common.Extensions
|
||||
//{
|
||||
// public static class ObjectExtensionMethods
|
||||
// {
|
||||
// /* https://dotnetcoretutorials.com/2020/09/09/cloning-objects-in-c-and-net-core/ */
|
||||
// public static T Clone<T>(this T source)
|
||||
// {
|
||||
// if (source is null) return default;
|
||||
// return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source, new JsonSerializerSettings()
|
||||
// {
|
||||
// ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
||||
// }), new JsonSerializerSettings()
|
||||
// {
|
||||
// ObjectCreationHandling = ObjectCreationHandling.Replace
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
@ -1,31 +1,31 @@
|
||||
using System.IO;
|
||||
//using System.IO;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
//using Newtonsoft.Json;
|
||||
|
||||
namespace StoicGoose.Common.Extensions
|
||||
{
|
||||
public static class SerializationExtensionMethods
|
||||
{
|
||||
public static void SerializeToFile(this object obj, string jsonFileName)
|
||||
{
|
||||
SerializeToFile(obj, jsonFileName, new JsonSerializerSettings() { Formatting = Formatting.Indented });
|
||||
}
|
||||
//namespace StoicGoose.Common.Extensions
|
||||
//{
|
||||
// public static class SerializationExtensionMethods
|
||||
// {
|
||||
// public static void SerializeToFile(this object obj, string jsonFileName)
|
||||
// {
|
||||
// SerializeToFile(obj, jsonFileName, new JsonSerializerSettings() { Formatting = Formatting.Indented });
|
||||
// }
|
||||
|
||||
public static void SerializeToFile(this object obj, string jsonFileName, JsonSerializerSettings serializerSettings)
|
||||
{
|
||||
using var writer = new StreamWriter(jsonFileName);
|
||||
writer.Write(JsonConvert.SerializeObject(obj, serializerSettings));
|
||||
}
|
||||
// public static void SerializeToFile(this object obj, string jsonFileName, JsonSerializerSettings serializerSettings)
|
||||
// {
|
||||
// using var writer = new StreamWriter(jsonFileName);
|
||||
// writer.Write(JsonConvert.SerializeObject(obj, serializerSettings));
|
||||
// }
|
||||
|
||||
public static T DeserializeFromFile<T>(this string jsonFileName)
|
||||
{
|
||||
using var reader = new StreamReader(jsonFileName);
|
||||
return (T)JsonConvert.DeserializeObject(reader.ReadToEnd(), typeof(T), new JsonSerializerSettings() { Formatting = Formatting.Indented });
|
||||
}
|
||||
// public static T DeserializeFromFile<T>(this string jsonFileName)
|
||||
// {
|
||||
// using var reader = new StreamReader(jsonFileName);
|
||||
// return (T)JsonConvert.DeserializeObject(reader.ReadToEnd(), typeof(T), new JsonSerializerSettings() { Formatting = Formatting.Indented });
|
||||
// }
|
||||
|
||||
public static T DeserializeObject<T>(this string jsonString)
|
||||
{
|
||||
return (T)JsonConvert.DeserializeObject(jsonString, typeof(T), new JsonSerializerSettings() { Formatting = Formatting.Indented });
|
||||
}
|
||||
}
|
||||
}
|
||||
// public static T DeserializeObject<T>(this string jsonString)
|
||||
// {
|
||||
// return (T)JsonConvert.DeserializeObject(jsonString, typeof(T), new JsonSerializerSettings() { Formatting = Formatting.Indented });
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
@ -1,37 +1,37 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
//using System;
|
||||
//using System.Globalization;
|
||||
//using System.Linq;
|
||||
//using System.Text.RegularExpressions;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
//using Newtonsoft.Json;
|
||||
//using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace StoicGoose.Common.Localization
|
||||
{
|
||||
public static class Localizer
|
||||
{
|
||||
public static string FallbackCulture { get; set; } = "en";
|
||||
//namespace StoicGoose.Common.Localization
|
||||
//{
|
||||
// public static class Localizer
|
||||
// {
|
||||
// public static string FallbackCulture { get; set; } = "en";
|
||||
|
||||
static JObject source = default;
|
||||
// static JObject source = default;
|
||||
|
||||
public static void Initialize(string jsonData) => source = JsonConvert.DeserializeObject(jsonData) as JObject;
|
||||
// public static void Initialize(string jsonData) => source = JsonConvert.DeserializeObject(jsonData) as JObject;
|
||||
|
||||
public static CultureInfo[] GetSupportedLanguages() => source?.Children().Select(x => new CultureInfo((x as JProperty).Name)).ToArray() ?? Array.Empty<CultureInfo>();
|
||||
// public static CultureInfo[] GetSupportedLanguages() => source?.Children().Select(x => new CultureInfo((x as JProperty).Name)).ToArray() ?? Array.Empty<CultureInfo>();
|
||||
|
||||
private static JToken GetToken(string path) => source?.SelectToken($"{CultureInfo.CurrentUICulture.TwoLetterISOLanguageName}.{path}") ?? source?.SelectToken($"{FallbackCulture}.{path}");
|
||||
public static string GetString(string path) => GetToken(path)?.Value<string>() ?? path[(path.LastIndexOf('.') + 1)..];
|
||||
public static string GetString(string path, object parameters)
|
||||
{
|
||||
var result = GetString(path);
|
||||
var properties = parameters.GetType().GetProperties();
|
||||
foreach (Match match in Regex.Matches(result, @"{(?<param>[^}:]*):*(?<format>[^}]*)}").Where(x => x.Success))
|
||||
{
|
||||
var property = properties.First(x => x.Name == match.Groups["param"].Value);
|
||||
var format = match.Groups["format"].Value;
|
||||
var formattedValue = string.IsNullOrEmpty(format) ? $"{property.GetValue(parameters)}" : string.Format($"{{0:{format}}}", property.GetValue(parameters));
|
||||
result = result.Replace(match.Value, formattedValue);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
// private static JToken GetToken(string path) => source?.SelectToken($"{CultureInfo.CurrentUICulture.TwoLetterISOLanguageName}.{path}") ?? source?.SelectToken($"{FallbackCulture}.{path}");
|
||||
// public static string GetString(string path) => GetToken(path)?.Value<string>() ?? path[(path.LastIndexOf('.') + 1)..];
|
||||
// public static string GetString(string path, object parameters)
|
||||
// {
|
||||
// var result = GetString(path);
|
||||
// var properties = parameters.GetType().GetProperties();
|
||||
// foreach (Match match in Regex.Matches(result, @"{(?<param>[^}:]*):*(?<format>[^}]*)}").Where(x => x.Success))
|
||||
// {
|
||||
// var property = properties.First(x => x.Name == match.Groups["param"].Value);
|
||||
// var format = match.Groups["format"].Value;
|
||||
// var formattedValue = string.IsNullOrEmpty(format) ? $"{property.GetValue(parameters)}" : string.Format($"{{0:{format}}}", property.GetValue(parameters));
|
||||
// result = result.Replace(match.Value, formattedValue);
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using StoicGoose.Common.Extensions;
|
||||
using StoicGoose.Common.Utilities;
|
||||
using StoicGoose.Common.Utilities;
|
||||
using StoicGoose.Core.Machines;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -571,11 +570,13 @@ static class Program
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(filename));
|
||||
|
||||
Configuration configuration;
|
||||
if (!File.Exists(filename) || (configuration = filename.DeserializeFromFile<Configuration>()) == null)
|
||||
{
|
||||
configuration = new Configuration();
|
||||
configuration.SerializeToFile(filename);
|
||||
}
|
||||
//if (!File.Exists(filename) || (configuration = filename.DeserializeFromFile<Configuration>()) == null)
|
||||
//{
|
||||
// configuration = new Configuration();
|
||||
// configuration.SerializeToFile(filename);
|
||||
//}
|
||||
|
||||
configuration = new Configuration();
|
||||
|
||||
return configuration;
|
||||
}
|
||||
@ -588,7 +589,7 @@ static class Program
|
||||
|
||||
public static void SaveConfiguration()
|
||||
{
|
||||
Configuration?.SerializeToFile(programConfigPath);
|
||||
//Configuration?.SerializeToFile(programConfigPath);
|
||||
}
|
||||
|
||||
private static string GetVersionDetails()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user