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

can't solve challenge 1 of 4

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. Bummer! Does your DeserializeWeather method take a List<WeatherForecast> parameter named weatherForecasts? Restart Preview Get Help Recheck 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 public static void SerializeWeatherForecasts(List<WeatherForecast> serializeWeatherForecasts,string fileName) 14 { 15 } 16

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

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

59 return weatherForecasts; 60 } 61 } 62 }

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  void SerializeWeatherForecasts(List<WeatherForecast> serializeWeatherForecasts,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;
        }
    }
}
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
    }
}

4 Answers

James Churchill
STAFF
James Churchill
Treehouse Teacher

Shawn,

The code challenge's error message is referring to the incorrect method name. The error message "Does your DeserializeWeather method take a List<WeatherForecast> parameter named weatherForecasts?" should read "Does your SerializeWeatherForecasts method take a List<WeatherForecast> parameter named weatherForecasts?".

I've updated the code challenge to fix this problem. Thanks so much for bringing this to our attention!

Thanks ~James

Here's how I got through the challenge set.

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)
        {
            JsonSerializer serializer = new JsonSerializer();
            using (StreamWriter writer = new StreamWriter(fileName))
            using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
            {
                serializer.Serialize(jsonWriter, weatherForecasts);
            }
        }

       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;
        }
    }
}
D Elis
D Elis
11,527 Points

working code: 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;

    }
}

}

Michael Mukwekezeke
Michael Mukwekezeke
15,143 Points

The code above could not be compiled. What am I missing