'PublishTrimmed error with System.Text.Json
I am trying to publish my c# code with the argument -p:PublishTrimmed=true. It uses System.Text.Json, and gives me a warning. /home/USER/tymaker-config-builder/Program.cs(69,33): warning IL2026: Using member 'System.Text.Json.JsonSerializer.Serialize<SerializeExtra.LetterInfo>(SerializeExtra.LetterInfo, System.Text.Json.JsonSerializerOptions?)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [/home/USER/tymaker-config-builder/tymaker-config-builder.csproj].
It also gives me another very similar warning: /home/tymaker-config-builder/Program.cs(69,13): Trim analysis warning IL2026: SerializeExtra.Program.Main(): Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [/home/USER/tymaker-config-builder/tymaker-config-builder.csproj]
Is there a way to resolve this issue while keeping my output trimmed? I do not want to have to publish ~70MB executables on my GitHub.
Here is my code:
using System.Text.Json;
using System.Text.Json.Serialization;
using System.IO;
namespace SerializeExtra
{
public class LetterInfo
{
public string? gift { get; set; }
public string? party { get; set; }
public string? sender { get; set; }
public string? address { get; set; }
}
public class Program
{
public static void Main()
{
Console.Write("Thank-you note bot JSON generator");
Console.Write('\n');
Console.Write("Version 1.0.0");
Console.Write("\n\n");
var letterInfo = new LetterInfo { };
Console.Write("What gift are you getting?\nThis gift will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
letterInfo.gift = Console.ReadLine();
if (letterInfo.gift == "skip")
{
letterInfo.gift = null;
}
Console.Write("\n\n");
Console.Write("What party are the thank-you notes for?\nThis party will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
letterInfo.party = Console.ReadLine();
if (letterInfo.party == "skip")
{
letterInfo.party = null;
}
Console.Write("\n\n");
Console.Write("What is your name?\nThis sender will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
letterInfo.sender = Console.ReadLine();
if (letterInfo.sender == "skip")
{
letterInfo.sender = null;
}
Console.Write("\n\n");
if (letterInfo.sender == null)
{
Console.Write("What do you want your closing word(s) to be? (e.g: {answer}, Joe)\nThis closing word will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
} else
{
Console.Write("What do you want your closing word(s) to be? (e.g: {answer}, " + letterInfo.sender + ")\nThis closing word will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
}
letterInfo.address = Console.ReadLine();
if (letterInfo.address == "skip")
{
letterInfo.address = null;
}
Console.Write("\n\n");
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = JsonSerializer.Serialize(letterInfo, options);
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
File.WriteAllText(Path.Combine(savePath, "tymaker.json"), jsonString);
Console.Write("Configuration file (tymaker.json) is saved at " + savePath + "\n\n");
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
