PapaMortality
PapaMortality
CC#
Created by PapaMortality on 10/29/2023 in #help
Casting Error, Not sure why. Newtonsoft.Json;
using System;
using Newtonsoft.Json;

namespace Cereal
{
public class Program
{
public static void Main(string[] args)
{
// Person p = new Person{
// First = "John",
// Middle = "Doodle",
// Last = "Doe",
// Age = 23
// };

// var r = p.Serialize("test.json");
// Console.WriteLine($"{r}");

Person np = Serializer.Deserialize<Person>("test.json");
Console.WriteLine($"{np}");

}
}
}

// This is in the Serializer class
public static T Deserialize<T>(string filePath)
{
string content;

using (StreamReader sr = new(filePath))
{
content = sr.ReadToEnd();
}

var settings = GetJsonSettings();
Console.WriteLine($"{content}");
return (T)JsonConvert.DeserializeObject<T>(content, settings);
}
using System;
using Newtonsoft.Json;

namespace Cereal
{
public class Program
{
public static void Main(string[] args)
{
// Person p = new Person{
// First = "John",
// Middle = "Doodle",
// Last = "Doe",
// Age = 23
// };

// var r = p.Serialize("test.json");
// Console.WriteLine($"{r}");

Person np = Serializer.Deserialize<Person>("test.json");
Console.WriteLine($"{np}");

}
}
}

// This is in the Serializer class
public static T Deserialize<T>(string filePath)
{
string content;

using (StreamReader sr = new(filePath))
{
content = sr.ReadToEnd();
}

var settings = GetJsonSettings();
Console.WriteLine($"{content}");
return (T)JsonConvert.DeserializeObject<T>(content, settings);
}
Tried a bunch of things to fix it, but I'm honestly stumped. I'll also paste in the Deserialize method.
28 replies