1

I was accustomed to using Newtonsoft's JObject in my Asp.net web api's. I could add properties at will using:

            JObject x = new JObject() { "myInt", 25 };
            //or
            x.Add("myInt", 25);

I ran into an error when trying to return a JObject from a ASP.Net core 3.1 web app.

System.NotSupportedException: The collection type 'Newtonsoft.Json.Linq.JObject' is not supported.

So NewtonSoft.Json is not supported in .Net core 3.1. So I looked at this article https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to but it seems that you can not add a property to a JsonDocument. This seems very odd and I thought I might be missing some information. So is there a way to add a property and value to a JsonDocument?

dbc
  • 104,963
  • 20
  • 228
  • 340
Paul Stanley
  • 1,999
  • 1
  • 22
  • 60
  • You can still add `NewtonSoft.Json` to ASP.NET Core 3 and use it instead of default `System.Text.Json` – Pavel Anikhouski Mar 18 '20 at 11:49
  • Are you really sure that first code block will work with Newtonsoft? I'm pretty sure it will still throw an exception. – DavidG Mar 18 '20 at 11:50
  • ```JObject x = new JObject(); x["myInt"] = "25";``` – cerberus Mar 18 '20 at 11:51
  • @DavidG i It works and makes JObject very flexible. – Paul Stanley Mar 18 '20 at 11:53
  • @cerberus I want to add a property to JsonDocument not JObject? – Paul Stanley Mar 18 '20 at 11:54
  • But even your exception shown here is a Newtonsoft exception, doesn't that prove it doesn't work? – DavidG Mar 18 '20 at 11:55
  • @PavelAnikhouski seems odd no way to add a property tho. – Paul Stanley Mar 18 '20 at 11:55
  • @DavidG Newtonsoft doesn't work with .net core 3 so I need to work with JsonDocument which does - I just wanted to add some properties to a response but apparently not possibe. – Paul Stanley Mar 18 '20 at 11:58
  • I've just tried it in a .NET Framework 4.8 app with Newtonsoft and it throws an exception. – DavidG Mar 18 '20 at 11:59
  • @DavidG Your right I was returning JObject .ToString(). However seems strange you don't have the flexibility to add a property to a with JsonDocument. – Paul Stanley Mar 18 '20 at 12:10
  • Why do you even want to add things to a JsonDocument anyway? Just work in a `Dictionary` or whatever and serialise when you need to. – DavidG Mar 18 '20 at 13:35
  • 2
    *So is there a way to add a property and value to a JsonDocument?* - no, there isn't, see [this answer](https://stackoverflow.com/a/59001666/3744182) and [Writable Json DOM #39922](https://github.com/dotnet/corefx/issues/39922) – dbc Mar 18 '20 at 17:56
  • But sometimes there are alternatives, see e.g. [Modifying a JSON file using System.Text.Json](https://stackoverflow.com/q/58997718/3744182) or [Operation is not valid due to the current state of the object (System.Text.Json)](https://stackoverflow.com/q/59719751/3744182). Or you could construct an anonymous object or dictionary. Can you share some details about what you are actually doing? There's no real need to use `JObject` in the simple case shown. – dbc Mar 18 '20 at 17:56

0 Answers0