question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

C# client throwing exception trying to Deserialize a json response into a string

See original GitHub issue

I am using an externally generated swagger.json definition that returns json data as a string. The client is failing with an exception calling DeserializeObject in the following code

 if (status_ == "200")
                        {
                            var responseData_ = await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
                            var result_ = default(string);
                            try
                            {
                                result_ = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(responseData_, _settings.Value);
                                return result_;
                            }
                            catch (System.Exception exception_)
                            {
                                throw new SwaggerException("Could not deserialize the response body.", status_, responseData_, headers_, exception_);
                            }
                        }

The call to DeserializeObject fails (and is not required) when the result is also a string. The result should just be set to the reponseData value.

Whats the best way to accomplish this? Do I need to update templates?

Thanks.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
RicoSutercommented, Dec 19, 2017

A string like foobar is not valid JSON, only "foobar" is

2reactions
berkerdongcommented, Nov 22, 2021
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(options =>
    {
        // requires using Microsoft.AspNetCore.Mvc.Formatters;
        options.OutputFormatters.RemoveType<StringOutputFormatter>();
        options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
    });
}

image

https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-6.0

This can be fixed on the server side of the API

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize JSON to c# object raising an exception
The score property is sometimes holding float value but in my c# class, there is datatype int which causes the exception.
Read more >
How to serialize and deserialize JSON using C# - .NET
Learn how to use the System.Text.Json namespace to serialize to and deserialize from JSON in .NET. Includes sample code.
Read more >
SerializationException: There was an error deserializing ...
When I debug and place a checkpoint on the error (line 105) I get the 2 ... DeserializeObject() method to convert the json...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >
Sending and Receiving JSON using HttpClient with System ...
This will cause a HttpRequestException to be thrown when the response is not in the 200-299 status code range. The library code will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found