Skip to content

Commit

Permalink
Fix json aot issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed Jun 7, 2023
1 parent c2d1d7d commit d153407
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 55 deletions.
16 changes: 16 additions & 0 deletions src/TextMateSharp.Grammars/JsonSerializationContext.cs
Original file line number Diff line number Diff line change
@@ -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<string>))]
internal sealed partial class JsonSerializationContext : JsonSerializerContext
{
}
}
57 changes: 10 additions & 47 deletions src/TextMateSharp.Grammars/LanguageConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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<LanguageConfiguration>(stream, new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip });
#pragma warning restore IL2026
return JsonSerializer.Deserialize(stream, jsonContext.LanguageConfiguration);
}
}
}
Expand Down Expand Up @@ -170,6 +170,7 @@ public class LanguageSnippet
public class LanguageSnippets
{
public IDictionary<string, LanguageSnippet> Snippets { get; set; } = new Dictionary<string, LanguageSnippet>();
private readonly static JsonSerializationContext jsonContext = new(new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip });

public static LanguageSnippets Load(string grammarName, Contributes contributes)
{
Expand All @@ -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<LanguageSnippets>(stream, new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip });
#pragma warning restore IL2026
return JsonSerializer.Deserialize(stream, jsonContext.LanguageSnippets);
}
}
}
Expand Down Expand Up @@ -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<StringPair>(ref reader, StringPairSerializationContext.Default.IListString);
autoPair.NotIn = JsonSerializer.Deserialize(ref reader, JsonSerializationContext.Default.IListString);
}
break;
case JsonTokenType.PropertyName:
Expand All @@ -257,7 +256,7 @@ public override AutoClosingPairs Read(ref Utf8JsonReader reader, Type typeToConv
break;
}
}
autoPairs.Add(autoPair);
autoPairs.Add(autoPair);
break;
}
}
Expand Down Expand Up @@ -368,7 +367,7 @@ public override EnterRules Read(ref Utf8JsonReader reader, Type typeToConvert, J
break;

case JsonTokenType.StartObject:
EnterRule rule = JsonSerializer.Deserialize<EnterRule>(ref reader, EnterRuleSerializationContext.Default.EnterRule);
EnterRule rule = JsonSerializer.Deserialize(ref reader, JsonSerializationContext.Default.EnterRule);
enterRules.Rules.Add(rule);
break;
}
Expand Down Expand Up @@ -476,7 +475,7 @@ public override LanguageSnippets Read(ref Utf8JsonReader reader, Type typeToConv
propName = reader.GetString();
break;
case JsonTokenType.StartObject:
LanguageSnippet snippet = JsonSerializer.Deserialize<LanguageSnippet>(ref reader, LanguageSnippetSerializationContext.Default.LanguageSnippet);
LanguageSnippet snippet = JsonSerializer.Deserialize(ref reader, JsonSerializationContext.Default.LanguageSnippet);
snippets.Snippets.Add(propName, snippet);
break;
}
Expand Down Expand Up @@ -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
{
}
}
9 changes: 1 addition & 8 deletions src/TextMateSharp.Grammars/RegistryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void InitializeAvailableGrammars()
{
GrammarDefinition definition = JsonSerializer.Deserialize(
stream,
GrammarDefinitionSerializationContext.Default.GrammarDefinition);
JsonSerializationContext.Default.GrammarDefinition);

foreach (var language in definition.Contributes.Languages)
{
Expand Down Expand Up @@ -260,11 +260,4 @@ static bool HasGrammar(string id, List<Grammar> grammars)
return false;
}
}

// Enable Source Generator support for GrammarDefinition
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(GrammarDefinition))]
internal sealed partial class GrammarDefinitionSerializationContext : JsonSerializerContext
{
}
}

0 comments on commit d153407

Please sign in to comment.