public async Task StartAsync() { if (started) { return; } (jsonWriter, jsonFilePath) = JsonWriterFacory.CreateJsonWriter(jsonDirectoryPath, propertyName); await semaphoreControl.WaitAsync(); try { if (started) { return; } await jsonWriter.WriteStartObjectAsync(); await jsonWriter.WritePropertyNameAsync(propertyName); await jsonWriter.WriteStartArrayAsync(); } finally { started = true; this.queueReaderTask = Task.Run(() => QueueReader()); semaphoreControl.Release(); } }
public async Task <string> GroupFiles(List <string> filesList, string destinationDirectoryPath, string rootFileName, bool deleteFiles, object objectBase, string rootPropertyName) { if ( (objectBase != null && string.IsNullOrEmpty(rootPropertyName)) || (objectBase == null && !string.IsNullOrEmpty(rootPropertyName)) ) { throw new ArgumentException("ObjectBase and RootPropertyName can not be null "); } JsonIOHelper.ValidateFiles(filesList); var isThereObjectBase = objectBase != null && !string.IsNullOrWhiteSpace(rootPropertyName); var ts = new CancellationTokenSource(); var createWriterResult = JsonWriterFacory.CreateJsonWriter(destinationDirectoryPath, rootFileName, Formatting.None); var objectBaseReader = await jsonConverterObjectToReader.ConvertObjectToJSonReaderAsync(objectBase); using (this.jsonWriter = createWriterResult.writer) { var flushTask = Task.Run(() => FlushWriter(ts.Token)); var lastTokenType = JsonToken.None; var lastPropertyname = string.Empty; while (objectBaseReader?.Read() ?? false) { await WriteTokenAsync(objectBaseReader, false); if (lastTokenType == JsonToken.PropertyName && lastPropertyname == rootPropertyName) { break; } lastTokenType = objectBaseReader.TokenType; lastPropertyname = objectBaseReader.Value?.ToString(); } if (!isThereObjectBase) { await WriteStartObjectAsync(); } foreach (var jsonFilePath in filesList) { using (var jsonReader = new JsonTextReader(File.OpenText(jsonFilePath))) { var countStartEndObject = 0; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.StartObject) { countStartEndObject++; } if (jsonReader.TokenType == JsonToken.EndObject) { countStartEndObject--; } if (jsonReader.TokenType == JsonToken.StartObject && countStartEndObject == 1) { continue; } if (jsonReader.TokenType == JsonToken.EndObject && countStartEndObject == 0) { continue; } WriteToken(jsonReader, jsonReader.TokenType == JsonToken.StartArray); } } if (deleteFiles) { File.Delete(jsonFilePath); } } if (!isThereObjectBase) { await WriteEndObjectAsync(); } while (objectBaseReader?.Read() ?? false) { await WriteTokenAsync(objectBaseReader, false); } ts.Cancel(); flushTask.Wait(); await jsonWriter?.FlushAsync(); return(createWriterResult.filePath); } }