public static void WriteToJsonFile(AnimalCollection animalCollection, string path) { JContainer jsonFile = (JContainer)JObject.FromObject(new object()); foreach (var animalType in animalCollection.GetTypes()) { var animalArray = new JArray(); foreach (var animal in animalCollection.List(animalType)) { var animalToken = JToken.FromObject(new { breed = animal.Breed, age = animal.Age, name = animal.Name, gender = animal.Gender.ToString() }); animalArray.Add(animalToken); } var animalArrayWithLabel = new JProperty(animalType, animalArray); jsonFile.Add(animalArrayWithLabel); } var streamWriter = new StreamWriter(path); streamWriter.WriteLine(jsonFile); streamWriter.Flush(); streamWriter.Close(); }
private IEnumerable <string> CallToList(string[] userCommandArgs, int argsCount) { IEnumerable <Animal> result = new List <Animal>(); if (argsCount == 1) { if (!string.IsNullOrWhiteSpace(userCommandArgs[1])) { result = animalCollection.List(userCommandArgs[1]); } } else if (argsCount == 0) { result = animalCollection.List(); } foreach (var animal in result) { yield return(animal.ToString()); } }