JsonElementExtensions.cs (786B)
1 using System.Diagnostics.CodeAnalysis; 2 using System.Reflection; 3 using System.Text.Json; 4 using System.Text.Json.Serialization; 5 6 namespace MatrixRoomUtils.Core.Extensions; 7 8 public static class JsonElementExtensions 9 { 10 public static void FindExtraJsonFields([DisallowNull] this JsonElement? res, Type t) 11 { 12 var props = t.GetProperties(); 13 var unknownPropertyFound = false; 14 foreach (var field in res.Value.EnumerateObject()) 15 { 16 if (props.Any(x => x.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name == field.Name)) continue; 17 Console.WriteLine($"[!!] Unknown property {field.Name} in {t.Name}!"); 18 unknownPropertyFound = true; 19 } 20 if(unknownPropertyFound) Console.WriteLine(res.Value.ToJson()); 21 } 22 }