none
The non-generic method 'object.ToString()' cannot be used with type arguments - JSON w/C# RRS feed

  • Question

  • I am following the DevBlog on Miscrosoft's site for .Net Core 3's System.Text.JSON and having issues doing exactly what it specifies to do. The code they provide doesn't work even copying and pasting there code. So something is broken or really messed up.

    I really mean it if you copy and paste what they put it does not work the way they have it with no changes made to it at all. I thought I must have made a mistake and couldn't find one so I said hell I'll just copy and paste everything they put in til I get to where I am and see if it doesn't work for real or if I messed up. Well I found out I couldn't have messed up.

    When I get to:

    return JsonSerializer.ToString<WeatherForecast>(value, options);

    Things go wrong with the .ToString<WeatherForecast> section of code. it gives me the error:

                        The non-generic method 'object.ToString()' cannot be used with type arguments

    https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/

    Monday, October 21, 2019 8:09 PM

All replies

  • Hello,

    You are better off writing a comment in the comment section of the page rather than ask here. So far there are 59 comments and at first glance see nobody having issues with the code, you may be the first.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 21, 2019 10:28 PM
    Moderator
  • Have you tried this: ‘return JsonSerializer.Serialize(value, options)’?

    See also:

    Tuesday, October 22, 2019 6:04 AM
  • Hi KXAura, 

    Thank you for posting here.

    The reference you provided is someone’s blog, ‘JsonSerializer.ToString<T>()’ method doesn't exist in ‘System.Text.Json’ and ‘System.Text.Json.Serialization’ namespace.

    Here’s an example to serialize and deserialize json string using System.Text.Json, and you can refer to it.

        class Program
        {
            static void Main(string[] args)
            {
                var student = new Student
                {
                    Name = "A",
                    Age = 20
                };
                var options = new JsonSerializerOptions
                {
                    WriteIndented = true
                };
                var json = JsonSerializer.Serialize<Student>(student, options);
                Console.WriteLine(json);
                Console.WriteLine();
                var stu = JsonSerializer.Deserialize<Student>(json);
                Console.WriteLine($"Name: {stu.Name}\nAge: {stu.Age}");
                Console.ReadLine();
            }      
        }
    
        class Student
        {
            [JsonPropertyName("studentName")]
            public string Name { get; set; }
            [JsonPropertyName("studentAge")]
            public int Age { get; set; }
        }

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 22, 2019 7:09 AM
    Moderator