Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Christer Monsen
Christer Monsen
3,664 Points

[SOLVED] Code Challenge 4 of 4 Serializer

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

        var serializer = new JsonSerializer();
        using (var writer = new StreamWriter(fileName))
        using (var 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;
    }
}

}

Christer Monsen
Christer Monsen
3,664 Points

Edited: a second parameter for Serialize but it still tells me "Bummer! The second parameter in the 'Serialize' should be 'weatherForecasts'."

2 Answers

Steven Parker
Steven Parker
231,268 Points

:point_right: Your call to serializer.Serialize is missing the second argument.

It looks like you're actually on task 4, not task 2. The instructions for task 4 are: "Inside the using block, call the Serialize method from the serializer object. The Serialize method takes a JsonTextWriter as its first parameter, and a List<WeatherForecast> as its second parameter."

Your call to serializer.Serialize has only one argument. Add the second one and you'll have it.

I'm still lost on no.4 I've got public static void SerializeWeatherForecasts(List<WeatherForecast> weatherForecasts,string fileName){ var serializer = new JsonSerializer (); using (var writer = new StreamWriter(fileName)) using (var JsonTextWriter = new JsonTextWriter(writer)) { serializer.Serialize (JsontWriter, List<WeatherForecast> ); }

                } 

But still not working