tmenier/Flurl

Parsing Empty List result in null instead of empty list

alexanderluettig opened this issue · 5 comments

My Code

var response = await <url>
	.WithOAuthBearerToken(apiToken)
	.GetJsonAsync<Response>();

public class Response
{
	public List<dynamic> results;
}

Response JSON that i get:

{
    "results" : []
}

Behaviour

When running my code i exptect that the empty array will be parsed to an empty list.
In the newest version Response.results is null.

In different 3.x.x Versions Response.results is still correctly parsed to an empty list

Things i tried

I tried different types for the List elements (Jobject, JSONObject, dynamic)
Different cases for the list property (results, Results)

dynamics are no longer supported out of the box because STJ doesn't support them. See #699. (Also covered in the upgrade guide.) If you need to use dynamics, switch to the Newtonsoft serializer, which matches 3.x behavior.

I tested it with different types for the array and for every other type i try e.g. int, string, bool. I still get null for the parsed list

Try using GetStringAsync, then use one of those libraries (STJ or Newtonsoft) directly to deserialize the string to the desired type. If you can get some combination to work, it should guide you on how to do it with Flurl. If not, I think at the very least you have a question you can ask on Stack Overflow that is specific to those JSON serializers and not to Flurl, exposing it to far larger audience of experts. The truth is I don't know why that specific scenario isn't working, because Flurl defers virtually all of that work to STJ or Newtonsoft, as you can tell from the dead simple implementations:

https://github.com/tmenier/Flurl/blob/dev/src/Flurl.Http/Configuration/DefaultJsonSerializer.cs
https://github.com/tmenier/Flurl/blob/dev/src/Flurl.Http.Newtonsoft/NewtonsoftJsonSerializer.cs

As a side-note, I would have guessed that using Newtonsoft + List<JObject> would have worked, but I'll take your word for it that you tried that.

Yeah I tried GetStringAsync in the first place.
Which gave me the json object with the empty results array. I'll try to parse the string instead.
Thanks for the help :)