gpt4 book ai didi

c# - JSON 对象不序列化大数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:55 24 4
gpt4 key购买 nike

我正在尝试序列化 JSON 中的数据。但我面临以下异常。

OutOfMemoryException was unhandled by user code.
An exception of type 'System.OutOfMemoryException' occurred in Newtonsoft.Json.dll but was not handled in user code

下面我定义了我的代码:

主 Controller :

public class TrackingController : BaseAngularController
{
var lstDetails = _services.GetTrackingDetailsByAWBIds(awbids, awbType);
if (lstDetails != null)
{
return AngularJson(lstDetails);
}
}

基础 Controller :

public abstract class BaseAngularController : Controller
{
public AngularJsonResult<T> AngularJson<T>(T model)
{
return new AngularJsonResult<T>() { Data = model };
}
}

Angular JSON 结果类:

public class AngularJsonResult<T> :AngularJsonResult
{
public new T Data
{
get { return (T)base.Data; }
set { base.Data = value; }
}
}

JSON 结果类:

public class AngularJsonResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
DoUninterestingBaseClassStuff(context);

SerializeData(context.HttpContext.Response);
}

private void DoUninterestingBaseClassStuff(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}

var response = context.HttpContext.Response;
response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType;

if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
}

protected virtual void SerializeData(HttpResponseBase response)
{
if (ErrorMessages.Any())
{
Data = new
{
ErrorMessage = string.Join("\n", ErrorMessages),
ErrorMessages = ErrorMessages.ToArray()
};

response.StatusCode = 400;
}

if (Data == null) return;

response.Write(Data.ToJson());
}
}

将对象序列化为 JSON:

public static class JsonExtensions
{
public static string ToJson<T>(this T obj, bool includeNull = true)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new JsonConverter[] { new StringEnumConverter() },
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,//newly added
//PreserveReferencesHandling =Newtonsoft.Json.PreserveReferencesHandling.Objects,
NullValueHandling = includeNull ? NullValueHandling.Include : NullValueHandling.Ignore
};
return JsonConvert.SerializeObject(obj, settings);
}
}

这里我定义了 AngularJson 方法来传递对象并覆盖 ExecuteResult 方法来将对象转换为 JSON。

所以我的 SerializeData 方法传递 Response 并转换为 JSON 格式的 Objet,例如 Data.ToJson()

请告诉我您的建议。

最佳答案

你的问题是你正在将你的巨大数据序列化为服务器内存中的一个字符串,然后将整个字符串写入HttpResponseBase(它还缓冲所有内容默认),并在进程中的某处耗尽内存,可能超过了 maximum c# string length .

减少内存使用的一种方法是直接序列化为 HttpResponseBase.OutputStream使用 JsonSerializer.Serialize() .这避免了中间字符串表示。

可能还需要设置HttpResponseBase.Buffer = false ,如果是,请遵循 Unbuffered Output Very Slow 中给出的建议并将输出流包装在 BufferedStream 中。

可以使用以下扩展方法:

public static class HttpResponseBaseExtensions
{
public static void WriteJson<T>(this HttpResponseBase response, T obj, bool useResponseBuffering = true, bool includeNull = true)
{
var contentEncoding = response.ContentEncoding ?? Encoding.UTF8;
if (!useResponseBuffering)
{
response.Buffer = false;

// use a BufferedStream as suggested in //https://stackoverflow.com/questions/26010915/unbuffered-output-very-slow
var bufferedStream = new BufferedStream(response.OutputStream, 256 * 1024);
bufferedStream.WriteJson(obj, contentEncoding, includeNull);
bufferedStream.Flush();
}
else
{
response.OutputStream.WriteJson(obj, contentEncoding, includeNull);
}
}

static void WriteJson<T>(this Stream stream, T obj, Encoding contentEncoding, bool includeNull)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new JsonConverter[] { new StringEnumConverter() },
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,//newly added
//PreserveReferencesHandling =Newtonsoft.Json.PreserveReferencesHandling.Objects,
NullValueHandling = includeNull ? NullValueHandling.Include : NullValueHandling.Ignore
};
var serializer = JsonSerializer.CreateDefault(settings);
var textWriter = new StreamWriter(stream, contentEncoding);
serializer.Serialize(textWriter, obj);
textWriter.Flush();
}
}

然后使用扩展方法代替 response.Write(Data.ToJson());

关于c# - JSON 对象不序列化大数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38697822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com