JObject.Parse in new System.Text.Json

1.2k views Asked by At

I am trying to change Newtonsoft.Json.Linq to new System.Text.Json on my previous solution I use JObject.Parse(jsonstring) to convert a json string to a Json Object and return it to a OpenApi V3 UI With JObject.Pase I am able to transfor this:

JObject.Parse(Regex.Unescape($"{{{System.Environment.NewLine}\t\"Name\": \"{Name}\",{System.Environment.NewLine}\t\"Version\": \"{Version}\",{System.Environment.NewLine}\t\"Platform\": \"{Platform}\"{((IsPuppetAgentServiceControllerRequired == false) ? String.Empty : $",{System.Environment.NewLine}\t\"IsPuppetAgentServiceControllerRequired\": true")}{((AgentUrl == null) ? String.Empty : $",{System.Environment.NewLine}\t\"AgentUrl\": \"{AgentUrl}\"")}{getModules()}{getModuleSources()}{getEnvironments()}}}"));

to this

enter image description here However I can't do the same with JsonDocument.Parse(jsonstring). Do you know how can I achieve this? This issue is affecting the JsonSerializer.Deserialize as it seems that it does not support JObject type

Using as reference the string in JObject.Parse(......) above, the output using JsonDocument.Parse is enter image description here

Just to be clear the string that I am sending in JObject.Parse and JsonDocument.Parse is the same and it is:

{\n\t"Name": "xxxx",\n\t"Version": "V1",\n\t"Platform": "xxxx",\n\t"Modules": {\n\t\t"Module1": {},\n\t\t"Module2": {\n\t\t\t"property1": "value1",\n\t\t\t"property2": "value2",\n\t\t}\n\t},\n\t"ModuleSources": {\n\t\t"modulesource1": {\n\t\t\t"url": "http://www.example.com"\n\t\t},\n\t\t"modulesource2": {\n\t\t\t"url": "http://www.example.com"\n\t\t}\n\t},\n\t"Environments": ["env1","env2"]\n}

It is well formed, otherwise JObject.Parse should not be able to parse it properly and provide me the output shown above

Adding a Code tests:

Working

/// <response code="200">The request has succeeded.</response>
/// <response code="400">The server can not find a current representation for the target resource or is not willing to disclose that one exists.</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public JObject Get()
{
     String str = $"{{{System.Environment.NewLine}\t\"Name\": \"name_xxxx\",{System.Environment.NewLine}\t\"Version\": \"vesion_xxxx\",{System.Environment.NewLine}\t\"Platform\": \"platform_xxxx\",{System.Environment.NewLine}\t\"AgentUrl\": \"https://www.example.com\",{System.Environment.NewLine}\t\"Modules\":{{{System.Environment.NewLine}\t\t\"module1\":{{}},{System.Environment.NewLine}\t\t\"module2\":{{{System.Environment.NewLine}\t\t\t\"property1\": \"value1\",{System.Environment.NewLine}\t\t\t\"property2\": \"value2\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"ModuleSources\":{{{System.Environment.NewLine}\t\t\t\"modulesource1\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}},{System.Environment.NewLine}\t\t\t\"modulesource2\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"Environments\":[\"env1\", \"env2\"]{System.Environment.NewLine}}}";
     return JObject.Parse(str);
}

This with JObject.Parse works but with JsonDocument.Parse doesn't The string when debugging is the same as described above enter image description here

Looking it in the text viewer of visual studio while debugging enter image description here

NotWorking

/// <response code="200">The request has succeeded.</response>
/// <response code="400">The server can not find a current representation for the target resource or is not willing to disclose that one exists.</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public JsonDocument Get()
{
     String str = $"{{{System.Environment.NewLine}\t\"Name\": \"name_xxxx\",{System.Environment.NewLine}\t\"Version\": \"vesion_xxxx\",{System.Environment.NewLine}\t\"Platform\": \"platform_xxxx\",{System.Environment.NewLine}\t\"AgentUrl\": \"https://www.example.com\",{System.Environment.NewLine}\t\"Modules\":{{{System.Environment.NewLine}\t\t\"module1\":{{}},{System.Environment.NewLine}\t\t\"module2\":{{{System.Environment.NewLine}\t\t\t\"property1\": \"value1\",{System.Environment.NewLine}\t\t\t\"property2\": \"value2\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"ModuleSources\":{{{System.Environment.NewLine}\t\t\t\"modulesource1\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}},{System.Environment.NewLine}\t\t\t\"modulesource2\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"Environments\":[\"env1\", \"env2\"]{System.Environment.NewLine}}}";
     return JsonDocument.Parse(str);
}
1

There are 1 answers

0
delucaezequiel On

It took me some time, but I finally find the solution. I just had to remove / comment the old NewtonsoftJson controller on ConfigureService function in Startup.cs. It seems that with the two controller in place, the second one .AddJsoOption..... was not taking effect

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllers();
     //services.AddControllers().AddNewtonsoftJson();
     services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true);
     ...

Once I remove the Json was properly displayed on OpenApi / Swagger.

As a side note, if you don't want to display the rootElement just change the function as follows:

/// <response code="200">The request has succeeded.</response>
/// <response code="400">The server can not find a current representation for the target resource or is not willing to disclose that one exists.</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public JsonElement Get()
{
     String str = $"{{{System.Environment.NewLine}\t\"Name\": \"name_xxxx\",{System.Environment.NewLine}\t\"Version\": \"vesion_xxxx\",{System.Environment.NewLine}\t\"Platform\": \"platform_xxxx\",{System.Environment.NewLine}\t\"AgentUrl\": \"https://www.example.com\",{System.Environment.NewLine}\t\"Modules\":{{{System.Environment.NewLine}\t\t\"module1\":{{}},{System.Environment.NewLine}\t\t\"module2\":{{{System.Environment.NewLine}\t\t\t\"property1\": \"value1\",{System.Environment.NewLine}\t\t\t\"property2\": \"value2\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"ModuleSources\":{{{System.Environment.NewLine}\t\t\t\"modulesource1\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}},{System.Environment.NewLine}\t\t\t\"modulesource2\":{{{System.Environment.NewLine}\t\t\t\t\"url\": \"https://www.example.com\"}}{System.Environment.NewLine}\t}},{System.Environment.NewLine}\t\"Environments\":[\"env1\", \"env2\"]{System.Environment.NewLine}}}";
     return JsonDocument.Parse(str).RootElement;
}