How do you handle JSON serialization efficiently in MAUI?
Short answer
Use System.Text.Json with source-generated serialization (JsonSerializerContext) to avoid reflection, which improves performance and trims better for AOT. Configure options (camelCase, ignore nulls) once and reuse them.
Advertisement
AModel answer
Use System.Text.Json with source-generated serialization (JsonSerializerContext) to avoid reflection, which improves performance and trims better for AOT. Configure options (camelCase, ignore nulls) once and reuse them. Source generation is especially valuable given MAUI's trimming and Native AOT direction.
[JsonSerializable(typeof(List<Product>))]
partial class ApiJsonContext : JsonSerializerContext { }
var products = JsonSerializer.Deserialize(json,
ApiJsonContext.Default.ListProduct);Real-world example
Switching to source-generated JSON removes reflection warnings under trimming and speeds up parsing large payloads.
Likely follow-ups
- Why source generation over reflection? | How does it help trimming?
Related topics