diff --git a/Helpers/HelperMarkdown.cs b/Helpers/HelperMarkdown.cs index a9dc724..90081a9 100644 --- a/Helpers/HelperMarkdown.cs +++ b/Helpers/HelperMarkdown.cs @@ -609,9 +609,14 @@ internal static partial class HelperMarkdown string directory = Path.Combine(Environment.CurrentDirectory, ".vscode"); if (!Directory.Exists(directory)) { - JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true }; - string json = JsonSerializer.Serialize(collection.Select(l => l.MarkdownFile), jsonSerializerOptions); - File.WriteAllText($"{DateTime.Now.Ticks}.json", json); + string json; + MarkdownFile markdownFile = collection.First().MarkdownFile; + json = JsonSerializer.Serialize(markdownFile, MarkdownFileSourceGenerationContext.Default.MarkdownFile); + if (json != "{}") + { + json = JsonSerializer.Serialize(collection.Select(l => l.MarkdownFile).ToArray(), MarkdownFileCollectionSourceGenerationContext.Default.MarkdownFileArray); + File.WriteAllText($"{DateTime.Now.Ticks}.json", json); + } } } diff --git a/Helpers/HelperPackageFilesByDate.cs b/Helpers/HelperPackageFilesByDate.cs index 657665f..d0af3b9 100644 --- a/Helpers/HelperPackageFilesByDate.cs +++ b/Helpers/HelperPackageFilesByDate.cs @@ -1,19 +1,11 @@ using Microsoft.Extensions.Logging; using System.Text.Json; -using System.Text.Json.Serialization; namespace File_Folder_Helper.Helpers; internal static class HelperPackageFilesByDate { - public record PackageJson( - [property: JsonPropertyName("name")] string Name, - [property: JsonPropertyName("time")] Dictionary Times, - [property: JsonPropertyName("_rev")] string Rev, - [property: JsonPropertyName("_id")] string Id - ); - internal static void SetDateFromJsonEntry(ILogger log, string sourceDirectory, SearchOption searchOption = SearchOption.AllDirectories) { string json; @@ -40,7 +32,7 @@ internal static class HelperPackageFilesByDate if (string.IsNullOrEmpty(packageJsonDirectory)) continue; json = File.ReadAllText(packageJsonFile); - packageJson = JsonSerializer.Deserialize(json); + packageJson = JsonSerializer.Deserialize(json, PackageJsonSourceGenerationContext.Default.PackageJson); if (packageJson is null || !packageJson.Times.Any()) continue; packageJsonDirectoryName = Path.GetFileName(packageJsonDirectory); diff --git a/Helpers/LineNumber.cs b/Helpers/LineNumber.cs index c730031..dd3d01a 100644 --- a/Helpers/LineNumber.cs +++ b/Helpers/LineNumber.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace File_Folder_Helper.Helpers; public record LineNumber(int? Created, @@ -5,4 +7,10 @@ public record LineNumber(int? Created, int? MetaEnd, int? Status, int? Type, - int? Updated); \ No newline at end of file + int? Updated); + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(LineNumber))] +internal partial class LineNumberSourceGenerationContext : JsonSerializerContext +{ +} \ No newline at end of file diff --git a/Helpers/MarkdownFile.cs b/Helpers/MarkdownFile.cs index 1a43b1e..0eeccc9 100644 --- a/Helpers/MarkdownFile.cs +++ b/Helpers/MarkdownFile.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace File_Folder_Helper.Helpers; public record MarkdownFile(string File, @@ -9,4 +11,16 @@ public record MarkdownFile(string File, DateTime LastWriteDateTime, LineNumber LineNumber, string Type, - string H1); \ No newline at end of file + string H1); + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(MarkdownFile))] +internal partial class MarkdownFileSourceGenerationContext : JsonSerializerContext +{ +} + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(MarkdownFile[]))] +internal partial class MarkdownFileCollectionSourceGenerationContext : JsonSerializerContext +{ +} \ No newline at end of file diff --git a/Helpers/PackageJson.cs b/Helpers/PackageJson.cs new file mode 100644 index 0000000..eead9fe --- /dev/null +++ b/Helpers/PackageJson.cs @@ -0,0 +1,20 @@ +using System.Text.Json.Serialization; + +namespace File_Folder_Helper.Helpers; + +public record PackageJson([property: JsonPropertyName("name")] string Name, + [property: JsonPropertyName("time")] Dictionary Times, + [property: JsonPropertyName("_rev")] string Rev, + [property: JsonPropertyName("_id")] string Id); + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(PackageJson))] +internal partial class PackageJsonSourceGenerationContext : JsonSerializerContext +{ +} + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(PackageJson[]))] +internal partial class PackageJsonCollectionSourceGenerationContext : JsonSerializerContext +{ +} diff --git a/Models/AppSettings.cs b/Models/AppSettings.cs index 29b20d0..864933d 100644 --- a/Models/AppSettings.cs +++ b/Models/AppSettings.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using System.Text.Json.Serialization; namespace File_Folder_Helper.Models; @@ -10,8 +11,14 @@ public record AppSettings(string Company, public override string ToString() { - string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + string result = JsonSerializer.Serialize(this, AppSettingsSourceGenerationContext.Default.AppSettings); return result; } -} \ No newline at end of file +} + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(AppSettings))] +internal partial class AppSettingsSourceGenerationContext : JsonSerializerContext +{ +} diff --git a/Models/Binder/AppSettings.cs b/Models/Binder/AppSettings.cs index 462cb7b..60eabc7 100644 --- a/Models/Binder/AppSettings.cs +++ b/Models/Binder/AppSettings.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Configuration; using System.Text.Json; +using System.Text.Json.Serialization; namespace File_Folder_Helper.Models.Binder; @@ -13,7 +14,7 @@ public class AppSettings public override string ToString() { - string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + string result = JsonSerializer.Serialize(this, BinderAppSettingsSourceGenerationContext.Default.AppSettings); return result; } @@ -40,9 +41,17 @@ public class AppSettings public static Models.AppSettings Get(IConfigurationRoot configurationRoot) { Models.AppSettings result; +#pragma warning disable IL3050, IL2026 AppSettings? appSettings = configurationRoot.Get(); +#pragma warning restore IL3050, IL2026 result = Get(appSettings); return result; } -} \ No newline at end of file +} + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(AppSettings))] +internal partial class BinderAppSettingsSourceGenerationContext : JsonSerializerContext +{ +} diff --git a/Program.cs b/Program.cs index 0fab6ca..e742fb9 100644 --- a/Program.cs +++ b/Program.cs @@ -11,7 +11,9 @@ internal class Program public static void Main(string[] args) { +#pragma warning disable IL3050 HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); +#pragma warning restore IL3050 _ = builder.Configuration.AddEnvironmentVariables(); _ = builder.Configuration.AddUserSecrets(); _ = builder.Services.AddSingleton(args.ToList());