public static void ReadAheadFromProperties(int stringSize) { string jsonProperties = CreateTestStringProperty(stringSize); StringBuilder builder = new StringBuilder(); builder.Append("{"); builder.Append(@"""Property1"":"); builder.Append(jsonProperties); builder.Append(@",""Property2"":"); builder.Append(jsonProperties); builder.Append(@",""Property3"":"); builder.Append(jsonProperties); builder.Append("}"); string json = builder.ToString(); var options = new JsonSerializerOptions(); options.Converters.Add(new ClassWithStringPropertyConverter()); // Ensure buffer size is small as possible for read-ahead. options.DefaultBufferSize = 1; byte[] data = Encoding.UTF8.GetBytes(json); { MemoryStream stream = new MemoryStream(data); ClassWithNoConverter obj = JsonSerializer.DeserializeAsync <ClassWithNoConverter>(stream, options).Result; VerifyClassWithStringProperties(obj.Property1, stringSize); VerifyClassWithStringProperties(obj.Property2, stringSize); VerifyClassWithStringProperties(obj.Property3, stringSize); string jsonSerialized = JsonSerializer.Serialize(obj, options); Assert.Equal(json, jsonSerialized); } { // Verify extension data works with read-ahead. Extension data stored on JsonElement which has a custom converter. MemoryStream stream = new MemoryStream(data); ClassWithExtensionData obj = JsonSerializer.DeserializeAsync <ClassWithExtensionData>(stream, options).Result; Assert.NotNull(obj.MyOverflow["Property1"]); Assert.NotNull(obj.MyOverflow["Property2"]); Assert.NotNull(obj.MyOverflow["Property3"]); string jsonSerialized = JsonSerializer.Serialize(obj, options); Assert.Equal(json, jsonSerialized); } }
public static void ReadAheadFromProperties(int stringSize) { string jsonProperties = CreateTestStringProperty(stringSize); StringBuilder builder = new StringBuilder(); builder.Append("{"); builder.Append(@"""Property1"":"); builder.Append(jsonProperties); builder.Append(@",""Property2"":"); builder.Append(jsonProperties); builder.Append(@",""Property3"":"); builder.Append(jsonProperties); builder.Append("}"); string json = builder.ToString(); var options = new JsonSerializerOptions(); options.Converters.Add(new ClassWithStringPropertyConverter()); // Ensure buffer size is small as possible for read-ahead. options.DefaultBufferSize = 1; byte[] data = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(data); ClassWithNoConverter obj = JsonSerializer.ReadAsync <ClassWithNoConverter>(stream, options).Result; VerifyClassWithStringProperties(obj.Property1, stringSize); VerifyClassWithStringProperties(obj.Property2, stringSize); VerifyClassWithStringProperties(obj.Property3, stringSize); string jsonSerialized = JsonSerializer.ToString(obj, options); Assert.Equal(json, jsonSerialized); }