Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing Empty List result in null instead of empty list #808

Closed
alexanderluettig opened this issue Jan 29, 2024 · 5 comments
Closed

Parsing Empty List result in null instead of empty list #808

alexanderluettig opened this issue Jan 29, 2024 · 5 comments
Labels

Comments

@alexanderluettig
Copy link

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)

@tmenier
Copy link
Owner

tmenier commented Jan 29, 2024

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.

@alexanderluettig
Copy link
Author

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

@tmenier
Copy link
Owner

tmenier commented Feb 1, 2024

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

@tmenier
Copy link
Owner

tmenier commented Feb 1, 2024

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.

@alexanderluettig
Copy link
Author

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants