C# C# Streams and Data Processing Serialization Serializing to a File

help me plz

Challenge Task 1 of 4

Create a new public static method named SerializeWeatherForecasts that returns void and takes a List<WeatherForecast> parameter named weatherForecasts and a string named fileName. Important: In each task of this code challenge, the code you write should be added to the code from the previous task. Restart Preview Get Help Check Work Program.cs WeatherForecast.cs

1 using System; 2 using System.IO; 3 using System.Collections.Generic; 4 using Newtonsoft.Json; 5 ā€‹ 6 namespace Treehouse.CodeChallenges 7 { 8 public class Program 9 { 10 public static void Main(string[] arg) 11 { 12 } 13

14 public static WeatherForecast ParseWeatherForecast(string[] values) 15 { 16 var weatherForecast = new WeatherForecast(); 17 weatherForecast.WeatherStationId = values[0]; 18 DateTime timeOfDay; 19 if (DateTime.TryParse(values[1], out timeOfDay)) 20 { 21 weatherForecast.TimeOfDay = timeOfDay; 22 } 23 Condition condition; 24 if (Enum.TryParse(values[2], out condition)) 25 { 26 weatherForecast.Condition = condition; 27 } 28 int temperature; 29 if (int.TryParse(values[3], out temperature)) 30 { 31 weatherForecast.Temperature = temperature; 32 } 33 double precipitation; 34 if (double.TryParse(values[4], out precipitation)) 35 { 36 weatherForecast.PrecipitationChance = precipitation; 37 } 38 if (double.TryParse(values[5], out precipitation)) 39 { 40 weatherForecast.PrecipitationAmount = precipitation; 41 } 42 return weatherForecast; 43 } 44 ā€‹ 45 public static List<WeatherForecast> DeserializeWeather(string fileName) 46 { 47 var weatherForecasts = new List<WeatherForecast>(); 48

49 using (var reader = new StreamReader(fileName)) 50 using (var jsonReader = new JsonTextReader(reader)) 51 { 52 var serializer = new JsonSerializer(); 53 weatherForecasts = serializer.Deserialize<List<WeatherForecast>>(jsonReader); 54 } 55

56 return weatherForecasts; 57 } 58 } 59 }

Program.cs
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Treehouse.CodeChallenges
{
    public class Program
    {
        public static void Main(string[] arg)
        {
        }

       public static WeatherForecast ParseWeatherForecast(string[] values)
       {
            var weatherForecast = new WeatherForecast();
            weatherForecast.WeatherStationId = values[0];
            DateTime timeOfDay;
            if (DateTime.TryParse(values[1], out timeOfDay))
            {
                weatherForecast.TimeOfDay = timeOfDay;
            }
            Condition condition;
            if (Enum.TryParse(values[2], out condition))
            {
                weatherForecast.Condition = condition;
            }
            int temperature;
            if (int.TryParse(values[3], out temperature))
            {
                weatherForecast.Temperature = temperature;
            }
            double precipitation;
            if (double.TryParse(values[4], out precipitation))
            {
                weatherForecast.PrecipitationChance = precipitation;
            }
            if (double.TryParse(values[5], out precipitation))
            {
                weatherForecast.PrecipitationAmount = precipitation;
            }
            return weatherForecast;
        }

        public static List<WeatherForecast> DeserializeWeather(string fileName)
        {
            var weatherForecasts = new List<WeatherForecast>();

            using (var reader = new StreamReader(fileName))
            using (var jsonReader = new JsonTextReader(reader))
            {
                var serializer = new JsonSerializer();
                weatherForecasts = serializer.Deserialize<List<WeatherForecast>>(jsonReader);
            }

            return weatherForecasts;
        }
    }
}
WeatherForecast.cs
using System;
using Newtonsoft.Json;

namespace Treehouse.CodeChallenges
{
    public class WeatherForecast
    {
        [JsonProperty(PropertyName = "weather_station_id")]
        public string WeatherStationId { get; set; }
        [JsonProperty(PropertyName = "time_of_day")]
        public DateTime TimeOfDay { get; set; }
        public Condition Condition { get; set; }
        public int Temperature { get; set; }
        [JsonProperty(PropertyName = "precipitation_chance")]
        public double PrecipitationChance { get; set; }
        [JsonProperty(PropertyName = "precipitation_amount")]
        public double PrecipitationAmount { get; set; }
    }

    public enum Condition
    {
        Rain,
        Cloudy,
        PartlyCloudy,
        PartlySunny,
        Sunny,
        Clear
    }
}

2 Answers

Peter Venkman
Peter Venkman
6,275 Points

What are you having trouble with? From what I can tell, you didn't even attempt to solve it yourself.

Go back and watch the video.

using System; using System.IO; using System.Collections.Generic; using Newtonsoft.Json;

namespace Treehouse.CodeChallenges { public class Program { public static void Main(string[] arg) { } public static void SerializeWeatherForecasts( List<WeatherForecast> weatherForecasts, string fileName) { } public static WeatherForecast ParseWeatherForecast(string[] values) { var weatherForecast = new WeatherForecast(); weatherForecast.WeatherStationId = values[0]; DateTime timeOfDay; if (DateTime.TryParse(values[1], out timeOfDay)) { weatherForecast.TimeOfDay = timeOfDay; } Condition condition; if (Enum.TryParse(values[2], out condition)) { weatherForecast.Condition = condition; } int temperature; if (int.TryParse(values[3], out temperature)) { weatherForecast.Temperature = temperature; } double precipitation; if (double.TryParse(values[4], out precipitation)) { weatherForecast.PrecipitationChance = precipitation; } if (double.TryParse(values[5], out precipitation)) { weatherForecast.PrecipitationAmount = precipitation; } return weatherForecast; }

    public static List<WeatherForecast> DeserializeWeather(string fileName)
    {
        var weatherForecasts = new List<WeatherForecast>();

        using (var reader = new StreamReader(fileName))
        using (var jsonReader = new JsonTextReader(reader))
        {
            var serializer = new JsonSerializer();
            weatherForecasts = serializer.Deserialize<List<WeatherForecast>>(jsonReader);
        }

        return weatherForecasts;
    }


}

}

make sure that you place the new method just after the main method to me thats how i solved the problem