3

what is best way to deseriaze JSON-file one item at a time? I have large JSON-file and I cannot read it to string, because I get error: "An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll"

How I can deserialize this JSON-file one item at a time, without using Json.NET ? I found this, but it uses Json.NET: Deserialize json array stream one item at a time

(I think that I have to use StreamReader when I read file, but how I can deserialize it?)

I have created DataContract:

[DataContract]
public class Header
{
    [DataMember(Name = "time")]
    public string time { get; set; }
    [DataMember(Name = "code")]
    public string code { get; set; }
    [DataMember(Name = "type")]
    public string type { get; set; }
    [DataMember(Name = "value")]
    public string value { get; set; }
}

[DataContract]
public class Body : IDeserializationCallback
{
    [DataMember(Name = "file")]
    public string file { get; set; }
    [DataMember(Name = "title")]
    public string title { get; set; }
    [DataMember(Name = "rows")]
    public List<string> rows { get; set; }

    public Dictionary<int, string> RowHeaders { get; protected set; }

    public void OnDeserialization(object sender)
    {
        RowHeaders = new Dictionary<int, string>();
        String[] headers = this.title.Split(';');
        for (int i = 0; i < headers.Length; i++)
        {
            RowHeaders.Add(i);
        }
    }
}

[DataContract]
public class Dwresult
{
    [DataMember(Name = "header")]
    public Header header { get; set; }
    [DataMember(Name = "body")]
    public List<Body> body { get; set; }
}

[DataContract]
public class RootObject
{
    [DataMember(Name = "dwresult")]
    public Dwresult dwresult { get; set; }
}

Here is example JSON:

{
"dwresult": {
    "header": { 
    "time": "2015.08.02 10:14:51",
    "code": "OK",
    "type": "TEST",
    "count": "2"
     },
        "body": [{
    "file": "test_file_one",
    "title": "EXAMPLE1;EXAMPLE2;EXAMPLE3;EXAMPLE4;EXAMPLE5;EXAMPLE6;EXAMPLE7;EXAMPLE8;EXAMPLE9;EXAMPLE10;EXAMPLE11;EXAMPLE12;EXAMPLE13;EXAMPLE14;EXAMPLE15;EXAMPLE16;EXAMPLE17;EXAMPLE18;EXAMPLE19;EXAMPLE20;",
    "rows": [
    "1;NAME1;CODE1;;;123;DATA1;1;1;1.2;;TOWN;3;1990;1991;2000;;;ACCOUNT;DOWNLOADED;",
    "2;NAME2;CODE2;;;456;DATA2;2;2;2.3;;TOWN2;4;1991;1992;2001;;;ACCOUNT;DOWNLOADED;",
    "3;NAME3;CODE3;;;789;DATA3;3;3;3.4;;TOWN3;5;1992;1993;;PERSON;AGE;AC;DOWNLOADED;",
     ]
    },{
    "file": "test_file_two",
    "title": "ANOT_EXAMPLE1;ANOT_EXAMPLE2;ANOT_EXAMPLE3;ANOT_EXAMPLE4;ANOT_EXAMPLE5;ANOT_EXAMPLE6;ANOT_EXAMPLE7;ANOT_EXAMPLE8;ANOT_EXAMPLE9;ANOT_EXAMPLE10;ANOT_EXAMPLE11;ANOT_EXAMPLE12;ANOT_EXAMPLE13;ANOT_EXAMPLE14;ANOT_EXAMPLE15;ANOT_EXAMPLE16;ANOT_EXAMPLE17;ANOT_EXAMPLE18;ANOT_EXAMPLE19;ANOT_EXAMPLE20;ANOT_EXAMPLE21;ANOT_EXAMPLE22;ANOT_EXAMPLE23;ANOT_EXAMPLE24;ANOT_EXAMPLE25;ANOT_EXAMPLE26;ANOT_EXAMPLE27;ANOT_EXAMPLE28;ANOT_EXAMPLE29;ANOT_EXAMPLE30;ANOT_EXAMPLE31;ANOT_EXAMPLE32;ANOT_EXAMPLE33;ANOT_EXAMPLE34;",
    "rows": [
    "4;NAME4;SCHOOL;;;01;DATA1;1;TOWN;04;;2011;2012;;;1;;;;PERSON;NUMBER;;;;;;;;;56;ACCOUNT;;;DOWNLOADED;",
    "5;NAME5;SCHOOL;;;02;DATA2;2;TOWN2;05;;2012;2013;;;2;;;;PERSON;NUMBER;;;;;;;;;57;ACCOUNT;;;DOWNLOADED;",
    "6;NAME6;SCHOOL;;;03;DATA3;3;TOWN3;06;;2013;2014;;;3;;31;;PERSON;NUMBER;;;;;;;;;58;ACCOUNT;;;DOWNLOADED;",
     ]
    }
    ]
}
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Mark78
  • 31
  • 3
  • How are json objects in a file separated? sample from your json file? – Eser Aug 10 '15 at 12:44
  • 1
    JSON.NET deserialization doesn't keep anything extra in memory; the parser and stream access is fully one-way without backtracking or caching. How are you reading the file to be deserialized? – user5090812 Aug 10 '15 at 12:45
  • Where did the `no Json.NET` requirement originate? – Mark Jansen Aug 10 '15 at 12:48
  • My idea is use this code in SSIS-script component and I'm not sure that can Json.NET cause any problems. So, thats why I asked help without Json.NET. :) – Mark78 Aug 11 '15 at 05:26
  • @Mark78 SSIS doesn't have problems with JSON.NET – Piotr Stapp Aug 11 '15 at 05:50
  • Writing serialization\deserialization manually is not an easy task. I would try to get sources of Json.Net and integrate them into your project if you can't use the Nuget package. – FireAlkazar Aug 11 '15 at 05:51
  • Can anyone provide good example, how I can deserialize my large file using Json.NET? Thank you. – Mark78 Aug 11 '15 at 07:18
  • @Mark78 Have you tried searching SO? - http://stackoverflow.com/questions/8157636/can-json-net-serialize-deserialize-to-from-a-stream – Shaun Wilde Sep 02 '15 at 21:30

0 Answers0