> ## Content Index
> Fetch the complete content index at: https://codetraveler.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Upgrading to Refit v6.0 + Newtonsoft.Json
- URL: https://codetraveler.io/2021/02/17/upgrading-to-refit-v6-0/
- Published: 2021-02-17T19:39:10.000Z
- Updated: 2021-02-17T19:46:12.000Z
- Description: Refit recently released v6.0, replacing Newtonsoft.Json with System.Text.Json which may cause some breaking changes. Let's discuss how we can continue using Newtonsoft.Json with Refit!
- Author: Brandon Minnick
- Tags: Refit, NewtsonSoft.Json, JSON, System.Text.Json, Json.NET

## Quick Caveat 

I do recommend using [System.Text.Json](https://docs.microsoft.com/dotnet/api/system.text.json?view=net-5.0&WT.mc%5Fid=mobile-bramin-16992&ref=codetraveler.io) because it has better performance than [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/?ref=codetraveler.io), and the .NET team continues to improve its performance in each new release of .NET:

- [Link to Performance Comparison](https://samiprogramming.medium.com/system-text-json-vs-newtonsoft-json-d01935068143?ref=codetraveler.io)
- [Link to Performance Improvements in .NET 5](https://devblogs.microsoft.com/dotnet/whats-next-for-system-text-json/?WT.mc%5Fid=mobile-bramin-16992&ref=codetraveler.io)

That being said, [migrating from Newtonsoft.Json to System.Text.Json is a breaking change](https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-5-0&WT.mc%5Fid=mobile-bramin-16992&ref=codetraveler.io) and it may not be quick and easy depending on your code base.

## Introducing Refit.Newtonsoft.Json

Along with [Refit v6.0](https://www.nuget.org/packages/refit/6.0.1?ref=codetraveler.io), the team released a new NuGet package, [Refit.Newtonsoft.Json](https://www.nuget.org/packages/Refit.Newtonsoft.Json/?ref=codetraveler.io). This allows us to continue using Newtonsoft.Json as our default JSON serializer.

After adding the [Refit.Newtonsoft.Json NuGet Package](https://www.nuget.org/packages/Refit.Newtonsoft.Json/?ref=codetraveler.io), here's how to use it:

**Refit v5.0 (Old)**

```csharp
var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");
```

**Refit v6.0 + Refit.Newtonsoft.Json (New)**

```csharp
var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com", new RefitSettings(new NewtonsoftJsonContentSerializer(new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() })););
```

That's it! We are now back to using Newtonsoft.Json as our default JSON serializer.

## Refit.Newtonsoft.Json Extensions 

Since the [Refit.Newtonsoft.Json](https://www.nuget.org/packages/Refit.Newtonsoft.Json/?ref=codetraveler.io) implementation is pretty verbose, I created the following [helper methods](https://github.com/brminnick/GitTrends/blob/8b8916e1cfec776201e6d10edd0b93cbf6947901/GitTrends.Shared/RefitExtensions.cs?ref=codetraveler.io#L8-L14) to keep my code a bit simpler:

([Here's how I implemented this code in my open-source app GitTrends](https://github.com/brminnick/GitTrends/blob/8b8916e1cfec776201e6d10edd0b93cbf6947901/GitTrends.Shared/RefitExtensions.cs?ref=codetraveler.io#L8-L14))

```csharp
public static class RefitExtensions
{
	public static T For<T>(string hostUrl) => RestService.For<T>(hostUrl, GetNewtonsoftJsonRefitSettings());
	public static T For<T>(HttpClient client) => RestService.For<T>(client, GetNewtonsoftJsonRefitSettings());

	public static RefitSettings GetNewtonsoftJsonRefitSettings() => new RefitSettings(new NewtonsoftJsonContentSerializer(new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }));
}
```

With the above `RefitExtensions`, I can more easily initialize Refit in my client-side applications:

```csharp
readonly static IGitHubApi _gitHubApi = RefitExtensions.For<IGitHubApi>("https://api.github.com");
```

And I can more easily initialize Refit in my on my backend applications with `[Refit.HttpClientFactory](https://www.nuget.org/packages/Refit.HttpClientFactory/?ref=codetraveler.io)`:

```csharp
public void ConfigureServices(IServiceCollection services)
{
	services.AddRefitClient<IGitHubApi>(RefitExtensions.GetNewtonsoftJsonRefitSettings())
                .ConfigureHttpClient(client => client.BaseAddress = new Uri("https://api.github.com"));
}
```

## Conclusion

In the long term, I highly recommend migrating your apps to use System.Text.Json to benefit from its many performance improvements.

But in the short-term, [Refit.Newtonsoft.Json](https://www.nuget.org/packages/Refit.Newtonsoft.Json/?ref=codetraveler.io) is a great stop-gap to keep your current code running!

If you'd like to see how I implemented [Refit.Newtonsoft.Json](https://www.nuget.org/packages/Refit.Newtonsoft.Json/?ref=codetraveler.io), here's how I use it in my open-source app, GitTrends: 

[brminnick/GitTrendsA iOS and Android app to monitor the views and clones of your GitHub repos - brminnick/GitTrends![](https://github.githubassets.com/favicons/favicon.svg)GitHubbrminnick![](https://avatars.githubusercontent.com/u/13558917?s=400&v=4)](https://github.com/brminnick/GitTrends/blob/8b8916e1cfec776201e6d10edd0b93cbf6947901/GitTrends.Shared/RefitExtensions.cs?ref=codetraveler.io#L8-L14)