public static async Task Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage:"); Console.WriteLine("homework input.xml output.json"); return; } var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; try { var sourceFileName = args[0]; var targetFileName = args[1]; var sourceStorage = new FileStorage(sourceFileName); var inputStream = await sourceStorage.ReadStreamAsync(token); var inputConverter = new XmlFormatInputConverter(); var document = await inputConverter.ReadAsync(inputStream, token); var outputConverter = new JsonFormatOutputConverter(); var outputStream = await outputConverter.WriteAsync(document, token); var destinationStorage = new FileStorage(targetFileName); await destinationStorage.WriteStreamAsync(outputStream, token); } catch (Exception e) { Debug.WriteLine(e.ToString()); Console.WriteLine($"Conversion has failed. {e.InnerException?.Message}"); } }
public void TestWrite() { var converter = new JsonFormatOutputConverter(); var document = new Document { Text = "Text", Title = "Title" }; var stream = converter.WriteAsync(document).Result; var reverseConverter = new JsonFormatInputConverter(); var testDoc = reverseConverter.ReadAsync(stream).Result; Assert.AreEqual(document.Title, testDoc.Title); Assert.AreEqual(document.Text, testDoc.Text); }