C# C# Streams and Data Processing Serialization Deserializing with Json.NET

Stefan Hoffmann
Stefan Hoffmann
Full Stack JavaScript Techdegree Student 15,754 Points

Are you sure you wrapped your StreamReader into a using? Well... actually I am.

Hello together!

Challenge Task 2 of 4 doesn't let me pass.

It asks me: Did you wrap your StreamReader into a using statement?

As you can see I did.

I do not know what I did wrong, as far as I know the statement below is correct and would compile. The validation-engines message is no help.

Maybe someone can tell me whats up here? How can I convince the challenge to let me continue?

Best regards, Stefan

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

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) ) 
            {

            }

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

/* Sample JSON 

[
  {
    "weather_station_id": "HGKL8Q",
    "time_of_day": "06/11/2016 0:00",
    "condition": "Rain",
    "temperature": 53,
    "precipitation_chance": 0.3,
    "precipitation_amount": 0.03
  },
  {
    "weather_station_id": "HGKL8Q",
    "time_of_day": "06/11/2016 6:00",
    "condition": "Cloudy",
    "temperature": 56,
    "precipitation_chance": 0.08,
    "precipitation_amount": 0.01
  },
  {
    "weather_station_id": "HGKL8Q",
    "time_of_day": "06/11/2016 12:00",
    "condition": "PartlyCloudy",
    "temperature": 70,
    "precipitation_chance": 0,
    "precipitation_amount": 0
  },
  {
    "weather_station_id": "HGKL8Q",
    "time_of_day": "06/11/2016 18:00",
    "condition": "Sunny",
    "temperature": 76,
    "precipitation_chance": 0,
    "precipitation_amount": 0
  },
  {
    "weather_station_id": "HGKL8Q",
    "time_of_day": "06/11/2016 19:00",
    "condition": "Clear",
    "temperature": 74,
    "precipitation_chance": 0,
    "precipitation_amount": 0
  }
]
*/

namespace Treehouse.CodeChallenges
{
    public class WeatherForecast
    {
        public string WeatherStationId { get; set; }
        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
    }
}

2 Answers

andren
andren
24,470 Points

The issue is that the code validation for this task is being extremely picky. It is validating this task by looking for this exact text:

using (var reader = new StreamReader(fileName)) 

If you don't have that exact text, down to the exact same spacing then it will mark your code as invalid.

In your case the issue is that you have placed a space after the opening parenthesis and before the closing parenthesis. Which messes with the code checker's rigid text matching code.

If you remove that spacing like this:

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

    using (var reader = new StreamReader(fileName)) 
    {

    }

    return weatherForecasts;
}

Then your code will be accepted.