https://stackoverflow.com/questions/66092495/jsonconvert-deserialize-object-out-of-memory-exception

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Xml;
using System.Net;

using Newtonsoft.Json;

using NUnit.Framework; // Throws NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/66092495/jsonconvert-deserialize-object-out-of-memory-exception

//https://github.com/microsoft/Microsoft.IO.RecyclableMemoryStream

	public class Doc
	{
		public Guid Id { get; set; }
		public byte[] Document { get; set; }
	}

	public class StreamedDocument : IDisposable
	{
		public Guid Id { get; set; }
		public Stream Document { get; init; }

    	public void Dispose() => Document?.Dispose();
	}

	public static class DocumentFactory
	{
		const int BufferSize = 8192;
    	private static readonly Microsoft.IO.RecyclableMemoryStreamManager manager = new ();

		public static Stream CreateTemporaryStream() => 
			// Create some temporary stream to hold the document.  
			// Could be a FileStream created with FileOptions.DeleteOnClose or a Microsoft.IO.RecyclableMemoryStream
			//File.Create(Path.GetTempFileName(), BufferSize, FileOptions.DeleteOnClose);
			manager.GetStream();
		
		public static StreamedDocument CreateStreamedDocument(Stream inputStream) =>
			DocumentFactory.PopulateStreamedDocument(new StreamedDocument{ Document = CreateTemporaryStream() }, inputStream);
		
		public static StreamedDocument PopulateStreamedDocument(StreamedDocument doc, Stream inputStream)
		{
			if (doc == null)
				throw new ArgumentNullException();
			if (doc.Document == null)
				throw new ArgumentException("null doc.Document");
			using (var reader = JsonReaderWriterFactory.CreateJsonReader(inputStream, XmlDictionaryReaderQuotas.Max))
			{
				while (!reader.EOF)
				{
					if (reader.NodeType == XmlNodeType.Element && reader.LocalName == nameof(doc.Id))
					{
						doc.Id = reader.ReadElementContentAsGuid();
						// reader should now be positioned PAST the EndElement.
						Debug.Assert(reader.NodeType != XmlNodeType.EndElement, $"reader.NodeType {reader.NodeType} != XmlNodeType.EndElement");
					}
					else if (reader.NodeType == XmlNodeType.Element && reader.LocalName == nameof(doc.Document))
					{
						byte[] buffer = new byte[BufferSize];
						int readBytes = 0;
						while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0)
							doc.Document.Write(buffer, 0, readBytes);
						// reader should now be positioned ON the EndElement
						Debug.Assert(reader.NodeType == XmlNodeType.EndElement, "reader.NodeType == XmlNodeType.EndElement");
						if (doc.Document.CanSeek)
							doc.Document.Position = 0;
					}
					else
					{
						reader.Read();
					}
				}
			}
			return doc;
		}
	}

    class TestClass
    {
		static StreamedDocument TestRequest(HttpWebRequest request)
		{
			var response = (HttpWebResponse)request.GetResponse();
			if (response.StatusCode == HttpStatusCode.OK)
			{
				return DocumentFactory.CreateStreamedDocument(response.GetResponseStream());
			}   			
			// Handle an error response somehow
			return default(StreamedDocument);
		}
		
        public static void Test()
        {
			var inDoc = new Doc
			{
				Id = Guid.Parse("81a130d2-502f-4cf1-a376-63edeb000e9f"),
				Document = Enumerable.Range(0, short.MaxValue).Select(i => unchecked((byte)i)).ToArray(),
			};

			var inJson = JsonConvert.SerializeObject(inDoc);
			Console.WriteLine(inJson);
			var inBytes = Encoding.UTF8.GetBytes(inJson);
			
			using var responseStream = new MemoryStream(inBytes);

			using var doc = DocumentFactory.CreateStreamedDocument(responseStream);
			
			Console.WriteLine($"doc.Id={doc.Id}, doc.Document.Length={doc.Document.Length}");

			using(var temp = new MemoryStream())
			{
				doc.Document.CopyTo(temp);
				Assert.IsTrue(inDoc.Document.SequenceEqual(temp.ToArray()));
			}			
			Assert.AreEqual(inDoc.Id, doc.Id, "Id");
		}	
	}

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
			Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name,  typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
		
		public static string GetNetCoreVersion()
		{
			//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
			//
			var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
			var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
			int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
			if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
				return assemblyPath[netCoreAppIndex + 1];
			return null;
		}
	}

	public static class Debug
	{
		public static void Assert(bool value, string message = "Assert Failed")
		{
			if (!value)
				throw new AssertionException(message);
		}
	}

#if false
{"Id":"81a130d2-502f-4cf1-a376-63edeb000e9f","Document":"AAECAwQFBgcICQ=="}
#endif

;