-
DescriptionI have built a .NET for Android app with .NET 8 (not MAUI). The app works perfectly when I don't link the libs. But when I tried to link them using:
I have tried to exclude the lib, but nothing worked. I have tried to add this to the <ItemGroup>
<LinkerDescriptor Include="Linker.xml" />
</ItemGroup> And exclude the full lib but didn't work also: <?xml version="1.0" encoding="utf-8" ?>
<linker>
<assembly fullname="System.Text.Json">
<type fullname="System.Text.Json.JsonSerializer" />
<type fullname="System.Text.Json.JsonSerializerOptions" />
<type fullname="System.Text.Json.JsonDocument" />
<type fullname="System.Text.Json.JsonElement" />
<type fullname="System.Text.Json.JsonTokenType" />
<type fullname="System.Text.Json.JsonPropertyName" />
<type fullname="System.Text.Json.Serialization.JsonConverter" />
<type fullname="System.Text.Json.Serialization.JsonConverterFactory" />
</assembly>
</linker> Avoiding the linking increases the size of the app in almost 1 MiB. So, I'd like to know what I should do. Reproduction StepsThe code that is failing is the following: var timetable = JsonSerializer.Deserialize<List<Timetable>>(results);
public class Timetable
{
public int id { get; set; }
public string member { get; set; }
public string role { get; set; }
public string min { get; set; }
public string opt { get; set; }
public string max { get; set; }
public string time { get; set; }
public string lastColor { get; set; }
public bool disqualified { get; set; }
} I also tried to exclude the crashing methods adding this: //The first line is what I added, yes, I'm calling .NET from JS. This is working if I don't link anything.
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(CallJSInterface))]
[JavascriptInterface]
[Export]
public void ExportToExcel(string data) No changes. Also, I tested the suggested solution with the same result:
It didn't work. partial class CallJSInterface
{
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(List<Timetable>))]
internal partial class TimetableContext : JsonSerializerContext
{
}
public class Timetable
{
public int id { get; set; }
public string member { get; set; }
public string role { get; set; }
public string min { get; set; }
public string opt { get; set; }
public string max { get; set; }
public string time { get; set; }
public string lastColor { get; set; }
public bool disqualified { get; set; }
}
}
private static List<Timetable> DeserializeTT(string results)
{
return JsonSerializer.Deserialize(results, TimetableContext.Default.ListTimetable);
} I'm not sure sure if I should change here also: var timetable = timetable.Where(x => JsonSerializer.Deserialize<List<int>>(selected).Contains(x.id)).ToList(); However, I don't know how to use the previous documentation for an int since it's not any special class. Expected behaviorTo don't fails. Actual behaviorFails when it tries to execute the previously provided code. The rest of the app works and also works if it's not linked. Regression?N/A Known WorkaroundsN/A ConfigurationN/A Other informationN/A |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
Beta Was this translation helpful? Give feedback.
-
You can reply to the origin issue for further questions. As the exception message says, reflection-based mechanics are disabled when trimming is on, so any trimming related work are unrelated. It's not because something being absent, instead System.Text.Json doesn't invoke them at all. According to Deserialization examples section in documentation, you need to update the
You do need to change here too. In source generation mode, you must be explicit about types you want to serialize, otherwise you won't be able to serialize a single integer. The non-special types support will be trimmed away if you don't specify them. In other words, the |
Beta Was this translation helpful? Give feedback.
I am not sure what other class you are referring to, but
JsonSerializer.Deserialize<List<int>>(selected)
doesn't use a (source-generated) serialization context. You need to use a serialization context that is aware of the root type you are trying to deserialize into. In this case the root type here isList<int>
. (If you are using a serialization context that isn't aware of the root type for deserialization, the deserializer would throw an exception informing you about this fact, by the way...)You don't necessarily need to create separate serialization context classes (but you can), as you can let a single serialization context class handle multiple root types by simply using multiple
[Js…