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.
17 Replies
PapaMortality
PapaMortality8mo ago
Appricate any help. I'll try not to be too needy lol
Angius
Angius8mo ago
What's the error, exactly?
PapaMortality
PapaMortality8mo ago
error CS0266: Cannot implicitly convert type 'object' to 'Cereal.Person' ing a cast?) Person np = Serializer.Deserialize<Person>("test.json"); On that line
Angius
Angius8mo ago
(also, why Newtonsoft instead of the built-in Systen.Text.Json...?)
PapaMortality
PapaMortality8mo ago
uhh didnt know it existed Is it better?
Angius
Angius8mo ago
Faster And built-in, so no need for any dependencies
PapaMortality
PapaMortality8mo ago
Does it serialize for me or do I have to set that up?
Angius
Angius8mo ago
It works like Newtonsoft
PapaMortality
PapaMortality8mo ago
alrighty, ig ill do that one instead. Though i dont think this is a Newtonsoft issue
Angius
Angius8mo ago
Yeah, it's most probably not
PapaMortality
PapaMortality8mo ago
I think im doin something wrong with my method or my generics idk
Angius
Angius8mo ago
Your code should work, from what I can see That cast to T is unnecessary But shouldn't be causing any issues
PapaMortality
PapaMortality8mo ago
yeah, i did that to try and fix it lol oh bruh i restarted vscode and it disappeared
Angius
Angius8mo ago
lmao
PapaMortality
PapaMortality8mo ago
alrighty well
Angius
Angius8mo ago
VS Code moment
PapaMortality
PapaMortality8mo ago
thanks for the help loll ig ill also try system json
Want results from more Discord servers?
Add your server
More Posts
how do i display this arrayHi. Im having an issue with displaying an string array. the way it comes out is like this. you can sDocker container works fine in manual invocation but not when started via VS2022's start buttonMy project structure: ``` ├── ComBin.sln ├── docker-compose.dcproj ├── docker-compose.dcproj.user ├─index outside the bounds of array errorso i have this program that is supposed to make the user add the amount of students in a class then ✅ Unsuccessfully trying to parse web pages.I want to parse last.fm for artists but my code works only first iteration, in all iterations where ❔ Career PathCurrently I'm learning and getting better at desktop development but I've seen many posts and forums❔ Benchmarking object size differenceI've got a small PR here to move a few `bool` fields into an existing "status" `int`. My understandi❔ How can I diff 2 strings?How can I find the difference between two strings (added/removed/modified characters/words)❔ In need of a way to process inputs to produce outputs, other than using hardcoded if statementsHi all, I currently have a program that prompts the user to ask a question, then based on the parame❔ € symbol prints to console in Rider, but not Visual StudioHey all, Sorry if this is a stupid question, but I'm using Rider IDE on Ubuntu and working on a col❔ Local Environment throwing exception when calling HttpClient.GetStringAsyncI am running the below code via Visual Studio Code, and a "The requested address is not valid in its