Skip to content

Work on getting rid of compiler warnings #25

Description

@Jdbye

I started working on this but didn't complete that work yet.
Here are some of my findings.

I think that switching the library to C# 9.0 will be necessary to get rid of some of the code warnings.
That's because Newtonsoft.Json in ReadJson and WriteJson (JsonConverter) uses object? for parameters.
In StringJsonConverter we use type T instead and this isn't directly compatible with object? as it's not marked as nullable, which causes warnings. But in C# 8.0 we can't use T?, this is only allowed starting in 9.0. Using T? makes these parameters be marked as nullable only where the generic type is nullable (e.g. StringJsonConverter<Uri?>) and cleanly solves these warnings.

Where I got stuck was with some of the flurl requests. It's not obvious to me how best to rewrite for example:

                return request.GetJsonAsync(token =>
                {
                    return token.SelectToken("posts").ToObject<ICollection<Post>>();
                }, true, HttpStatusCode.NotFound);

to not produce compiler warnings. It might be necessary to do something like:

                return request.GetJsonAsync(token =>
                {
                    return token.SelectToken("posts")?.ToObject<ICollection<Post>>() ?? Array.Empty<Post>();
                }, true, HttpStatusCode.NotFound);

which does indeed seem to solve the warnings and may also prevent exceptions from being thrown in certain cases. Whether it's desirable to prevent those exceptions from being thrown though is another matter.

These compiler warnings don't seem to cause any actual issues, but for the future it would be nice to get rid of them to make it easier to maintain the project. The compiler warning spam makes it more difficult to spot actual issues that may be introduced by changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions