带有List <object> </object>的JSON.NET的System.OutOfMemoryException

时间:2014-12-05 12:04:20

标签: c# asp.net-mvc json json.net

我正在进行一个流程,我的服务器生成一个包含25000条记录和85MB大小的JSON.Net文件,然后该文件由客户端导入。

客户端下载文件并使用JSON.Net进行反序列化。

我的问题是时间和不同的机器,反序列化命令给出:

System.OutOfMemoryException

注意:我试图手动使用GC不起作用。

代码生成错误:

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

代码生成JSON文件:

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

我的班级地址

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; }
}

1 个答案:

答案 0 :(得分:3)

我正在玩上面的答案:Deserialize json array stream one item at a time。以下稍作修改可能会满足您的需求。它逐项读取和反序列化JSON,而不是将整个列表加载到内存中:

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

    public static IEnumerable<T> DeserializeEnumerableString<T>(string json)
    {
        using (var sr = new StringReader(json))
            foreach (var item in DeserializeEnumerable<T>(sr))
                yield return item;
    }

    public static IEnumerable<T> DeserializeEnumerable<T>(TextReader textReader)
    {
        var serializer = JsonSerializer.CreateDefault();

        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
                    yield return serializer.Deserialize<T>(reader);
                }
            }
        }
    }
}