Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Text.Json 5.x preview deserialization is slower than Newtonsoft when using Stream scenarios on Xamarin platforms #41089

@klogeaage

Description

@klogeaage

Description

I have found that if you use Newtonsoft's ability to deserialize from a Stream via its JsonTextReader(), then on some platforms, it is comparable to or even better than System.Text.Json. This is not the stated goal of your efforts.

So a typical case is deserializing the result of a HTTP call, which in Newtonsoft should be done like this:

    HttpResponseMessage response = await _client.GetAsync($"{ControllerName}{additionalParameters}");
    response.EnsureSuccessStatusCode();
    using (var stream = await response.Content.ReadAsStreamAsync())
    using (var reader = new StreamReader(stream))
    using (var json = new JsonTextReader(reader))
	items = _serializer.Deserialize<IEnumerable<T>>(json);

While with System.Text.Json the deserializing part will be:

    using (var stream = await response.Content.ReadAsStreamAsync())
        _items = await System.Text.Json.JsonSerializer.DeserializeAsync<IEnumerable<T>>(stream, JsonOptions.Default1);

I have found the performance to be roughly the same on iOS devices and MUCH slower (25-40%) on Android. But even equal performance is not the goal, I presume.

I don't know of a unit testing framework that would let me verify this, especially on real physical devices, which may have quite different characteristics than emulators.

So I have created a small Xamarin.Forms app to measure this, which I will be happy to share with you if you find it relevant.

I used another app of mine to share screen shots of the results.

Configuration

  • Net Standard 2.1 (Xamarin.Forms 4.8.0.1269)
  • Newtonsoft.Json 12.0.3
  • System.Text.Json 5.0.0-preview.7.20364.11
  • iOS 13.6 and Xamarin 10
  • iPhone7 and Galaxy A50

Data

In my app, I store a JSON payload from a real world domain model of 526 KB. It is deserialized 5 times and the average is returned for the two serializers.

If can measure that the performance of this is 3-7% slower on the iOS device and 25-40% slower on the Android device. On simulators, I get similar results.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions