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

vincent batteast
vincent batteast
51,094 Points

Inside the SerializeWeatherForecast Instantiate a new JsonSerializer and name it serializer. Then, add a using statement

Program.cs(62,41): error CS0246: The type or namespace name JsonTextwriter' could not be found. Are you missing an assembly reference? Program.cs(65,28): error CS0308: The non-generic methodNewtonsoft.Json.JsonSerializer.Serialize(System.IO.TextWriter, object)' cannot be used with the type arguments /usr/local/lib/ruby/gems/2.3.0/gems/challenge_proctor-0.0.0/lib/challenge_proctor/runners/includes/mono/Newtonsoft.Json.dll (Location of the symbol related to previous error) Compilation failed: 2 error(s), 0 warnings

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;
        }
        public static void SerializeWeatherForecasts(List<WeatherForecast> weatherForecasts, string fileName)
        {
            using (var writer = new StreamWriter(fileName))
            using (var jsonwriter = new JsonTextwriter(writer))
            {
                var serializer = new JsonSerializer();
                serializer.Serialize<List<WeatherForecast>>(fileName);
            }
        }

    }
}
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
    }
}

3 Answers

Jon Wood
Jon Wood
9,884 Points

I think you just need to capitalize Writer when you use the JsonTextWriter class.

mr Vincent did you get the answer ?

did you get the answer?