diff --git a/src/TextMateSharp.Grammars/JsonSerializationContext.cs b/src/TextMateSharp.Grammars/JsonSerializationContext.cs new file mode 100644 index 0000000..44ff696 --- /dev/null +++ b/src/TextMateSharp.Grammars/JsonSerializationContext.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace TextMateSharp.Grammars +{ + [JsonSerializable(typeof(GrammarDefinition))] + [JsonSerializable(typeof(LanguageSnippets))] + [JsonSerializable(typeof(LanguageSnippet))] + [JsonSerializable(typeof(LanguageConfiguration))] + [JsonSerializable(typeof(EnterRule))] + [JsonSerializable(typeof(AutoPair))] + [JsonSerializable(typeof(IList))] + internal sealed partial class JsonSerializationContext : JsonSerializerContext + { + } +} \ No newline at end of file diff --git a/src/TextMateSharp.Grammars/LanguageConfiguration.cs b/src/TextMateSharp.Grammars/LanguageConfiguration.cs index 3c32172..7eb556f 100644 --- a/src/TextMateSharp.Grammars/LanguageConfiguration.cs +++ b/src/TextMateSharp.Grammars/LanguageConfiguration.cs @@ -39,6 +39,8 @@ public class LanguageConfiguration [JsonConverter(typeof(EnterRulesJsonConverter))] public EnterRules EnterRules { get; set; } + private readonly static JsonSerializationContext jsonContext = new(new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip }); + public static LanguageConfiguration Load(string grammarName, string configurationFile) { if (string.IsNullOrEmpty(configurationFile)) @@ -51,9 +53,7 @@ public static LanguageConfiguration Load(string grammarName, string configuratio using (StreamReader reader = new StreamReader(stream)) { -#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code - return JsonSerializer.Deserialize(stream, new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip }); -#pragma warning restore IL2026 + return JsonSerializer.Deserialize(stream, jsonContext.LanguageConfiguration); } } } @@ -170,6 +170,7 @@ public class LanguageSnippet public class LanguageSnippets { public IDictionary Snippets { get; set; } = new Dictionary(); + private readonly static JsonSerializationContext jsonContext = new(new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip }); public static LanguageSnippets Load(string grammarName, Contributes contributes) { @@ -187,9 +188,7 @@ public static LanguageSnippets Load(string grammarName, Contributes contributes) using (StreamReader reader = new StreamReader(stream)) { -#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code - return JsonSerializer.Deserialize(stream, new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip }); -#pragma warning restore IL2026 + return JsonSerializer.Deserialize(stream, jsonContext.LanguageSnippets); } } } @@ -233,12 +232,12 @@ public override AutoClosingPairs Read(ref Utf8JsonReader reader, Type typeToConv string propName = string.Empty; while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) { - switch(reader.TokenType) + switch (reader.TokenType) { case JsonTokenType.StartArray: if (string.Compare(propName, "notIn") == 0) { - autoPair.NotIn = JsonSerializer.Deserialize(ref reader, StringPairSerializationContext.Default.IListString); + autoPair.NotIn = JsonSerializer.Deserialize(ref reader, JsonSerializationContext.Default.IListString); } break; case JsonTokenType.PropertyName: @@ -257,7 +256,7 @@ public override AutoClosingPairs Read(ref Utf8JsonReader reader, Type typeToConv break; } } - autoPairs.Add(autoPair); + autoPairs.Add(autoPair); break; } } @@ -368,7 +367,7 @@ public override EnterRules Read(ref Utf8JsonReader reader, Type typeToConvert, J break; case JsonTokenType.StartObject: - EnterRule rule = JsonSerializer.Deserialize(ref reader, EnterRuleSerializationContext.Default.EnterRule); + EnterRule rule = JsonSerializer.Deserialize(ref reader, JsonSerializationContext.Default.EnterRule); enterRules.Rules.Add(rule); break; } @@ -476,7 +475,7 @@ public override LanguageSnippets Read(ref Utf8JsonReader reader, Type typeToConv propName = reader.GetString(); break; case JsonTokenType.StartObject: - LanguageSnippet snippet = JsonSerializer.Deserialize(ref reader, LanguageSnippetSerializationContext.Default.LanguageSnippet); + LanguageSnippet snippet = JsonSerializer.Deserialize(ref reader, JsonSerializationContext.Default.LanguageSnippet); snippets.Snippets.Add(propName, snippet); break; } @@ -548,40 +547,4 @@ public override void Write(Utf8JsonWriter writer, LanguageSnippet value, JsonSer { } } - - [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(StringPair))] - internal sealed partial class StringPairSerializationContext : JsonSerializerContext - { - } - - [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(AutoPair))] - internal sealed partial class AutoPairSerializationContext : JsonSerializerContext - { - } - - [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(EnterRule))] - internal sealed partial class EnterRuleSerializationContext : JsonSerializerContext - { - } - - [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(LanguageConfiguration))] - internal sealed partial class LanguageConfigurationSerializationContext : JsonSerializerContext - { - } - - [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(LanguageSnippet))] - internal sealed partial class LanguageSnippetSerializationContext : JsonSerializerContext - { - } - - [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(LanguageSnippets))] - internal sealed partial class LanguageSnippetsSerializationContext : JsonSerializerContext - { - } } \ No newline at end of file diff --git a/src/TextMateSharp.Grammars/RegistryOptions.cs b/src/TextMateSharp.Grammars/RegistryOptions.cs index d62e360..d7a58d6 100644 --- a/src/TextMateSharp.Grammars/RegistryOptions.cs +++ b/src/TextMateSharp.Grammars/RegistryOptions.cs @@ -167,7 +167,7 @@ void InitializeAvailableGrammars() { GrammarDefinition definition = JsonSerializer.Deserialize( stream, - GrammarDefinitionSerializationContext.Default.GrammarDefinition); + JsonSerializationContext.Default.GrammarDefinition); foreach (var language in definition.Contributes.Languages) { @@ -260,11 +260,4 @@ static bool HasGrammar(string id, List grammars) return false; } } - - // Enable Source Generator support for GrammarDefinition - [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(GrammarDefinition))] - internal sealed partial class GrammarDefinitionSerializationContext : JsonSerializerContext - { - } }