8

Possible Duplicate:
Incremental JSON Parsing in C#

The following questions are related but don't address (at least directly) the problem I'm trying to solve:

I am trying to deserialize potentially a very large JSON data using Json.NET. Instead of loading the entire file into memory and parse the JSON using JObject.Parse(JsonFullString), I wish to read from the stream token by token and construct the object graph. I would appreciate any suggestion on how to implement deserialization from stream.

Note: My intent is to replace the following code with better implementation


      string jsonData = string.Empty;
      byte[] buffer = new byte[16 * 1024];
      using (MemoryStream ms = new MemoryStream())
      {
          int read;
          while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
          {
              ms.Write(buffer, 0, read);
          }
          jsonData = ASCIIEncoding.ASCII.GetString(ms.ToArray());
       }                    
       JObject jObject = JObject.Parse(jsonData);
       var entities = from e in jObject.Root
                      select e;

Community
  • 1
  • 1
Bedasso
  • 1,652
  • 1
  • 14
  • 17

1 Answers1

-3

Nice Idea!
I think what you can do is the following:

  1. Create the object first
  2. Read the stream in a loop, by chunks, as you are doing now
  3. Fore every chunk you've read check if it contains a token (if a comma "," is found within)
  4. if no comma found - read another chunk (maybe a smaller one in this case?)
  5. Split the pre - comma text (data.split(":");) and with the help of reflection, or hardcoded loop, if you dont need to support every possible object, assign the value to the property
  6. continue looping.
YavgenyP
  • 2,113
  • 14
  • 11
  • its not that i dont mind the voting that much, but the json parsers ive seen work exaclty like that. it can be nice to know where you people think i was wrong – YavgenyP Jun 04 '12 at 17:56
  • 1
    Because live is not that easy. Just a simple example `{ "delimeter1": ",", "delimeter2": "}"}`. Also arrays, arrays of arrays, nested objects, escaping non-ascii chars etc. – L.B Jun 04 '12 at 19:27