Categories
Programación

System.Text.Json in Xamarin.Forms

The .NET Core 3 team recently released a new way to handle JSON for the .NET Standard / .NET Framework and the .NET Core. The new namespace System.Text.Json so I decided to try it out on a Xamarin.Forms Project.

So first I will port one of my Android Apps to the new System.Text.Json and see the impact on Performance and on the App Bundle Package Size. Since the main goal for this new namespace is performance.

I need to install the NuGet package on my Xamarin.Forms project and remove the NewtonSoft.Json

System.Text.Json NuGet Package in Xamarin.Forms

Then I had to change my Data models, hope Quicktype.IO supports the new API from Microsoft soon, but the change is rather simple.

Is put the namespace System.Text.Json and change JsonProperty to JsonPropertyName

Using Statement in System.Text.Json

And for the static class, I did the following changes. I modify JsonConvert.DeserializedObject to JsonSerializer.Deserialize.

And for the Serialize I change from JsonConvert.SerializeObject to JsonSerializer.Serialize

I remove the settings because I don’t do any conversions, the new System.Text.Json allows conversions. But I don’t handle dates or any other kind of conversions, so no need for it.

Old NewtonSoft.Json to the new System.Text.Json in a Xamarin.Forms Project.

Now let’s test System.Text.Json in the Xamarin.Forms publication on the App Bundle.

APP SIZE IN APP BUNDLE FORMAT

So it was a surprise but I think It was kind of logical since the NuGet Package is lighter than the NewtonSoft one. It is a reduction of 620 kb

App Size using System.Net.Json vs NewtonSoft NuGet

As users tend to value your App Size since download speeds in different parts of the world may vary the winner here is the new System.Text.Json

PERFORMANCE (MEMORY)

So this is the thing I was wanting to try this new Namespace

Payload Size for my APP 112 KB
DescriptionCPU %Memory (MB)
NewtonSoft Json85230
System.Text.Json98189

My first conclusion is that the CPU usage is higher but the memory management and footprint that is the Key area to see is lower. The Winner for memory Management is System.Text.Json

PERFORMANCE (SPEED)

RPS (Request per Seconds) is the main goal for this new Namespace and YESSSS they did nail it. This is my data

Payload Size for my APP 112 KB
DescriptionRPS
NewtonSoft Json3485
System.Text.Json7319

So I get a little bit more than 2.1X speed up performance so the Winner is System.Text.Json

CONCLUSION

I think this data is very interesting and probably my new projects will use this new API from Microsoft, first because is OpenSource and free of charge. And second, because Size always matters.

2 replies on “System.Text.Json in Xamarin.Forms”

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *