为System.OutOfMemoryException与列表&LT JSON.NET;对象> [英] System.OutOfMemoryException with JSON.NET with List<object>

查看:209
本文介绍了为System.OutOfMemoryException与列表&LT JSON.NET;对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做在我的服务器生成与JSON.Net一个文件25000的记录和85MB大小的过程,那么这个文件是由客户端导入。

客户端下载文件,使反序列化与JSON.Net。

我的问题是时代和不同的机器上,反序列化命令,并提供:

的System.OutOfMemoryException

请注意:我试图手动使用GC没有工作

code生成错误:

  VAR listAddress = JsonConvert.DeserializeObject<名单,LT;地址>>(File.ReadAllText(@C:\\ TEMP \\ test.json);

code生成JSON文件:

 使用(StreamWriter的文件= File.CreateText(C:\\\\ test.json))
{
    JsonSerializer串行=新JsonSerializer();
    serializer.Serialize(文件,listAddress);
}

我的班级地址

 公共类Endereco
{
    公众诠释Codigo {搞定;组; }
    公共字符串CGCCPF {搞定;组; }
    公共字符? TipoPessoa {搞定;组; }
    公共字符串UF {搞定;组; }
    公共字符串SiglaDoRG {搞定;组; }
    公共字符串MaeFantasica {搞定;组; }
    公共字符串DescEndereco {搞定;组; }
    公共字符串补语{搞定;组; }
    公共字符串上城区{搞定;组; }
    公共字符串CEP {搞定;组; }
    公共字符串排{搞定;组; }
    公共字符串CIDADE {搞定;组; }
    公共字符串Telefone {搞定;组; }
    公共字符串传真{搞定;组; }
    公共字符串电子邮件{获得;组; }
    公共字符串IEouRG {搞定;组; }
    公共字符串OrgaoEmissorRG {搞定;组; }
    公众的DateTime? DataNascimento {搞定;组; }
    公共CampoObservacao Observacao {搞定;组; }
    公众的DateTime? DataRegistro {搞定;组; }
    公众的DateTime? DataUltAlteracao {搞定;组; }
    公众诠释? CodigoFuncionario {搞定;组; }
    公共字符串Ramal {搞定;组; }
    公众的DateTime? DataDeEmissaoRG {搞定;组; }
    公共字符串Alfa1 {搞定;组; }
    公共字符串Alfa2 {搞定;组; }
    公共双? NUM1 {搞定;组; }
    公共双? NUM2 {搞定;组; }
    公共字符串PontoReferencia {搞定;组; }
    公共字符串Celular {搞定;组; }
    公共字符串EnderecoWeb {搞定;组; }
    公共字符串CONTA {搞定;组; }
    公共字符串Rasocial {搞定;组; }
    公众诠释? NcCodigo {搞定;组; }
    公众诠释? NcCodigoC {搞定;组; }
    公共字符串CFOP {搞定;组; }
    公共字符串NÚMERO{搞定;组; }
    公众诠释? CodigoMunicipio {搞定;组; }
    公众诠释? CodigoPais {搞定;组; }
    公共字符串Suframa {搞定;组; }
    公共字符串NumeroNit {搞定;组; }
    公共字符串InscricaoMunicipal {搞定;组; }
    公共字符串IE {搞定;组; }    公共BOOL Ativo
    {
        {返回Observacao [状态]等于(I)!。 }
        集合{Observacao [状态] =!价值? 我:的String.Empty; }
    }    公共Endereco()
    {
        Observacao =新CampoObservacao();
    }    公共字符? TipoDeMercado
    {
        {返回!String.IsNullOrEmpty(Observacao [TIPO德梅尔卡多])? (字符?)Observacao [TIPO德梅尔卡多] [0]:空; }
        集合{Observacao [TIPO德梅尔卡多] = value.HasValue? value.ToString()的String.Empty; }
    }    公共字符? EstadoCivil
    {
        {返回!String.IsNullOrEmpty(Observacao [EstadoCivil])? (CHAR?)Observacao [EstadoCivil] [0]:空; }
        集合{Observacao [EstadoCivil] = value.HasValue? value.ToString()的String.Empty; }
    }    公共Cliente Cliente {搞定;组; }
    公共Funcionario Funcionario {搞定;组; }
    公共孝孝{搞定;组; }
}


解决方案

我在玩弄上面链接了答案了一下:<一href=\"http://stackoverflow.com/questions/20374083/deserialize-json-array-stream-one-item-at-a-time/20386292#20386292\">Deserialize JSON阵列流在一个时间的一个项目。以下轻微的修改可能会满足您的需求。它读取和反序列化JSON的项目,由项目而不是加载整个列表到内存中:

 公共静态类JsonConvertExtensions
{
    公共静态的IEnumerable&LT; T&GT; DeserializeEnumerable&LT; T&GT;(字符串文件名)
    {
        使用(VAR流=新的StreamReader(文件名))
            的foreach(; T&gt;在DeserializeEnumerable&LT VAR项目(流))
                产生回报的项目;
    }    公共静态的IEnumerable&LT; T&GT; DeserializeEnumerable&LT; T&GT;(TextReader的的TextReader)
    {
        使用(JsonTextReader读卡器=新JsonTextReader(TextReader的))
        {
            而(reader.Read())
            {
                如果(reader.TokenType == JsonToken.StartObject)
                {
                    //从流加载每个对象,并用它做什么
                    JObject OBJ = JObject.Load(读卡器);
                    如果(OBJ!= NULL)
                    {
                        ŧ项目= obj.ToObject&LT; T&GT;();
                        如果(项目== NULL)
                            的Debug.WriteLine(未知项目类型:+ obj.ToString());
                        其他
                            产生回报的项目;
                    }
                }
            }
        }
    }
}

I'm doing a process where my server generates a file with JSON.Net with 25000 records and size of 85MB, then this file is imported by client.

The client downloads the file and makes the deserialize with JSON.Net.

My problem is that the times and on different machines, deserialize command is giving:

System.OutOfMemoryException

Note: I tried to manually use the GC did not work.

Code generate error:

var listAddress = JsonConvert.DeserializeObject<List<address>>(File.ReadAllText(@"c:\Temp\test.json");

Code generate JSON File:

using (StreamWriter file = File.CreateText("C:\\test.json"))
{
    JsonSerializer serializer = new JsonSerializer();                        
    serializer.Serialize(file, listAddress);                        
}

My Class Address

public class Endereco
{
    public int Codigo { get; set; }
    public string CGCCPF { get; set; }
    public char? TipoPessoa { get; set; }
    public string UF { get; set; }
    public string SiglaDoRG { get; set; }
    public string MaeFantasica { get; set; }
    public string DescEndereco { get; set; }
    public string Complemento { get; set; }
    public string Bairro { get; set; }
    public string CEP { get; set; }
    public string Pai { get; set; }
    public string Cidade { get; set; }
    public string Telefone { get; set; }
    public string Fax { get; set; }
    public string Email { get; set; }
    public string IEouRG { get; set; }
    public string OrgaoEmissorRG { get; set; }
    public DateTime? DataNascimento { get; set; }
    public CampoObservacao Observacao { get; set; }
    public DateTime? DataRegistro { get; set; }
    public DateTime? DataUltAlteracao { get; set; }
    public int? CodigoFuncionario { get; set; }
    public string Ramal { get; set; }
    public DateTime? DataDeEmissaoRG { get; set; }
    public string Alfa1 { get; set; }
    public string Alfa2 { get; set; }
    public double? Num1 { get; set; }
    public double? Num2 { get; set; }
    public string PontoReferencia { get; set; }
    public string Celular { get; set; }
    public string EnderecoWeb { get; set; }
    public string Conta { get; set; }
    public string Rasocial { get; set; }
    public int? NcCodigo { get; set; }
    public int? NcCodigoC { get; set; }
    public string CFOP { get; set; }
    public string Numero { get; set; }
    public int? CodigoMunicipio { get; set; }
    public int? CodigoPais { get; set; }
    public string Suframa { get; set; }
    public string NumeroNit { get; set; }
    public string InscricaoMunicipal { get; set; }
    public string IE { get; set; }

    public bool Ativo
    {
        get { return !Observacao["Status"].Equals("I"); }
        set { Observacao["Status"] = !value ? "I" : String.Empty; }
    }

    public Endereco()
    {
        Observacao = new CampoObservacao();
    }

    public char? TipoDeMercado
    {
        get { return !String.IsNullOrEmpty(Observacao["Tipo de Mercado"]) ? (char?)Observacao["Tipo de Mercado"][0] : null; }
        set { Observacao["Tipo de Mercado"] = value.HasValue ? value.ToString() : String.Empty; }
    }

    public char? EstadoCivil
    {
        get { return !String.IsNullOrEmpty(Observacao["EstadoCivil"]) ? (char?)Observacao["EstadoCivil"][0] : null; }
        set { Observacao["EstadoCivil"] = value.HasValue ? value.ToString() : String.Empty; }
    }

    public Cliente Cliente { get; set; }
    public Funcionario Funcionario { get; set; }
    public Filial Filial { get; set; }
}

解决方案

I was playing around a bit with the answer linked above: Deserialize json array stream one item at a time. The following slight modification might meet your needs. It reads and deserializes the JSON item-by-item rather than loading the entire list into memory:

public static class JsonConvertExtensions
{
    public static IEnumerable<T> DeserializeEnumerable<T>(string filename)
    {
        using (var stream = new StreamReader(filename))
            foreach (var item in DeserializeEnumerable<T>(stream))
                yield return item;
    }

    public static IEnumerable<T> DeserializeEnumerable<T>(TextReader textReader)
    {
        using (JsonTextReader reader = new JsonTextReader(textReader))
        {
            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.StartObject)
                {
                    // Load each object from the stream and do something with it
                    JObject obj = JObject.Load(reader);
                    if (obj != null)
                    {
                        T item = obj.ToObject<T>();
                        if (item == null)
                            Debug.WriteLine("unknown item type: " + obj.ToString());
                        else
                            yield return item;
                    }
                }
            }
        }
    }
}

这篇关于为System.OutOfMemoryException与列表&LT JSON.NET;对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆