C# C# Streams and Data Processing Serialization Cleaning up the Player Class

yujia liu
yujia liu
14,964 Points

add the JsonProperty attribute to the TimeOfDay property

I added JsonProperty attribute to TimeOfDay property, I think it should be correct, is this a bug?

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("weather_station_id")]
        public string WeatherStationId { get; set; }
        [JsonProperty("time_of_day")]
        public DateTime TimeOfDay { get; set; }
        public Condition Condition { get; set; }
        public int Temperature { get; set; }
        public double PrecipitationChance { get; set; }
        public double PrecipitationAmount { get; set; }
    }

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

1 Answer

Steven Parker
Steven Parker
170,317 Points

It does look like you have found a bug. :beetle:

But I'm not sure if it's in task 1 or task 2. In task 1, it let you get away without naming PropertyName in the argument. But for task 2, it's expecting to see this:

        [JsonProperty(PropertyName = "time_of_day")]

Yet I believe that a single string argument is taken as the ProperyName by default, so the bug may be in task 2. But it should be consistent about it either way.

You might want to report this bug to Support, it might get you a "special Exterminator badge".

yujia liu
yujia liu
14,964 Points

oh. I see. Thank you so much. I forgot "PropertyName =" . By the way I saw you helped a lot of people solve problems. Also I saw you took a bunch of classes in treehouse.