protected virtual void OnJsonFileChunkIsReady(JsonFileChunkIsReadyArgs e) { EventHandler <JsonFileChunkIsReadyArgs> handler = JsonFileChunkIsReady; if (handler != null) { handler(this, e); } }
//static AutoResetEvent autoEvent = new AutoResetEvent(false); /// <summary> /// Read content from A large JSON file by chunks /// </summary> /// <param name="fileName">JSON file name</param> public void readingChunksFromLargeJsonFile(string fileName) { //manualResetEvent = new ManualResetEvent(false); //FastBulkGeocoding fbGeocoding = new FastBulkGeocoding(""); //fbGeocoding.GeocodingIsFinished += FbGeocoding_GeocodingIsFinished; //manualResetEvent.WaitOne(); JsonSerializer serializer = new JsonSerializer(); AddressField o = null; string sJsonAddressesChunk = ""; int curJsonObjects = 0; using (FileStream s = File.Open(fileName, FileMode.Open)) using (StreamReader sr = new StreamReader(s)) using (JsonReader reader = new JsonTextReader(sr)) { //reader.SupportMultipleContent = true; bool blStartAdresses = false; while (reader.Read()) { if (reader.TokenType == JsonToken.StartArray) { blStartAdresses = true; } if (reader.TokenType == JsonToken.StartObject && blStartAdresses) { o = serializer.Deserialize <AddressField>(reader); if (o.Address == null) { continue; } sJsonAddressesChunk += JsonConvert.SerializeObject(o, Formatting.None) + ","; curJsonObjects++; if (curJsonObjects >= jsonObjectsChunkSize) { sJsonAddressesChunk = "{\"rows\":[" + sJsonAddressesChunk.TrimEnd(',') + "]}"; JsonFileChunkIsReadyArgs chunkIsReady = new JsonFileChunkIsReadyArgs(); chunkIsReady.AddressesChunk = sJsonAddressesChunk; sJsonAddressesChunk = ""; curJsonObjects = 0; //manualResetEvent.Set(); OnJsonFileChunkIsReady(chunkIsReady); Thread.Sleep(5000); //manualResetEvent.WaitOne(); } } } if (sJsonAddressesChunk != "") { sJsonAddressesChunk = "{\"rows\":[" + sJsonAddressesChunk.TrimEnd(',') + "]}"; JsonFileChunkIsReadyArgs chunkIsReady = new JsonFileChunkIsReadyArgs(); chunkIsReady.AddressesChunk = sJsonAddressesChunk; sJsonAddressesChunk = ""; OnJsonFileChunkIsReady(chunkIsReady); System.Threading.Thread.Sleep(5000); } JsonFileReadingIsDoneArgs args = new JsonFileReadingIsDoneArgs() { IsDone = true }; OnJsonFileReadingIsDone(args); } }