© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
27 replies
PapaMortality

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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Newtonsoft.Json error
C#CC# / help
4y ago
Newtonsoft.JSON ignore a JsonProperty
C#CC# / help
2y ago
Newtonsoft.Json json handling
C#CC# / help
2y ago