Andr谩s T贸th‘s professional blog
banditoth.net

Hey there 馃憢, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

System.Text.Json dictionary deserialisation issue with Refit

This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

If you find that your Dictionary object, if it has a string key, and it is not deserialized with its first initial capitalized, and you are using System.Text.Json serializer, here is the solution.

This is only applies to some versions of the Refit NuGet package, where NewtonSoftJson is not the default serializer.

Solution using Refit

Define RefitSettings when creating your rest service, like this:

RestService.For<IAnythingApi>(
			Constans.BackendApiUrl,
			new RefitSettings
			{
				ContentSerializer = new SystemTextJsonContentSerializer(
                    new JsonSerializerOptions
					{
						DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
                    }),
			});

This solution will allow you to deserialise with uppercase letter key 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.