Add code to preserve public fields of input types to JsonSerializer (#40508)
authorLayomi Akinrinade <laakinri@microsoft.com>
Fri, 7 Aug 2020 15:44:54 +0000 (08:44 -0700)
committerGitHub <noreply@github.com>
Fri, 7 Aug 2020 15:44:54 +0000 (08:44 -0700)
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs
src/libraries/System.Text.Json/tests/TrimmingTests/Helper.cs

index 76dd25b..8b4909a 100644 (file)
@@ -10,7 +10,8 @@ namespace System.Text.Json
     public static partial class JsonSerializer
     {
         // Members accessed by the serializer when deserializing.
-        private const DynamicallyAccessedMemberTypes MembersAccessedOnRead = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties;
+        private const DynamicallyAccessedMemberTypes MembersAccessedOnRead =
+            DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields;
 
         private static TValue? ReadCore<TValue>(ref Utf8JsonReader reader, Type returnType, JsonSerializerOptions options)
         {
index 96c33a0..d23528f 100644 (file)
@@ -10,7 +10,7 @@ namespace System.Text.Json
     public static partial class JsonSerializer
     {
         // Members accessed by the serializer when serializing.
-        private const DynamicallyAccessedMemberTypes MembersAccessedOnWrite = DynamicallyAccessedMemberTypes.PublicProperties;
+        private const DynamicallyAccessedMemberTypes MembersAccessedOnWrite = DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields;
 
         private static void WriteCore<TValue>(
             Utf8JsonWriter writer,
index a9593b9..b99a5a8 100644 (file)
@@ -90,16 +90,18 @@ namespace SerializerTrimmingTest
         }
     }
 
-    internal class MyClass
+    public class MyClass
     {
         public int X { get; set; }
-        public int Y { get; set; }
+        [JsonInclude]
+        public int Y;
     }
 
     internal struct MyStruct
     {
         public int X { get; }
-        public int Y { get; }
+        [JsonInclude]
+        public int Y;
 
         [JsonConstructor]
         public MyStruct(int x, int y) => (X, Y) = (x, y);
@@ -108,7 +110,8 @@ namespace SerializerTrimmingTest
     internal class MyClassWithParameterizedCtor
     {
         public int X { get; set; }
-        public int Y { get; set; }
+        [JsonInclude]
+        public int Y;
 
         public MyClassWithParameterizedCtor(int x, int y) => (X, Y) = (x, y);
     }
@@ -116,11 +119,14 @@ namespace SerializerTrimmingTest
     internal class MyBigClass
     {
         public string A { get; }
-        public string B { get; }
+        [JsonInclude]
+        public string B;
         public string C { get; }
-        public int One { get; }
+        [JsonInclude]
+        public int One;
         public int Two { get; }
-        public int Three { get; }
+        [JsonInclude]
+        public int Three;
 
         public MyBigClass(string a, string b, string c, int one, int two, int three)
         {