public static void ReadAheadFromRoot(int stringSize) { string json = CreateTestStringProperty(stringSize); 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); ClassWithStringProperties obj = JsonSerializer.DeserializeAsync <ClassWithStringProperties>(stream, options).Result; VerifyClassWithStringProperties(obj, 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; VerifyExtensionDataStringProperties(obj, stringSize); string jsonSerialized = JsonSerializer.Serialize(obj, options); Assert.Equal(json, jsonSerialized); } }
private static void VerifyClassWithStringProperties(ClassWithStringProperties obj, int stringSize) { // The 10 properties will cause buffer boundary cases where the converter requires read-ahead. Assert.Equal(new string('0', stringSize), obj.MyString0); Assert.Equal(new string('1', stringSize), obj.MyString1); Assert.Equal(new string('2', stringSize), obj.MyString2); Assert.Equal(new string('3', stringSize), obj.MyString3); Assert.Equal(new string('4', stringSize), obj.MyString4); Assert.Equal(new string('5', stringSize), obj.MyString5); Assert.Equal(new string('6', stringSize), obj.MyString6); Assert.Equal(new string('7', stringSize), obj.MyString7); Assert.Equal(new string('8', stringSize), obj.MyString8); Assert.Equal(new string('9', stringSize), obj.MyString9); }
public static void ReadAheadFromRoot(int stringSize) { string json = CreateTestStringProperty(stringSize); 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); ClassWithStringProperties obj = JsonSerializer.ReadAsync <ClassWithStringProperties>(stream, options).Result; VerifyClassWithStringProperties(obj, stringSize); string jsonSerialized = JsonSerializer.ToString(obj, options); Assert.Equal(json, jsonSerialized); }