JsonSerializer throws exception in xamarin.ios

Stesvis 1,041 Reputation points
2021-03-07T20:29:53.31+00:00

While in android everything works fine (as most times), in iOS this line
var payload = System.Text.Json.JsonSerializer.Serialize(postData);
throws an exception:

System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception. ---> System.MissingMethodException: Method not found: int System.Text.Encodings.Web.TextEncoder.FindFirstCharacterToEncodeUtf8(System.ReadOnlySpan1<byte>) at System.Text.Json.JsonEncodedText.EncodeHelper (System.ReadOnlySpan1[T] utf8Value, System.Text.Encodings.Web.JavaScriptEncoder encoder) [0x00000] in <7e3a59f5e4004edbb4b17c580799cc52>:0
at System.Text.Json.JsonEncodedText.TranscodeAndEncode (System.ReadOnlySpan1[T] value, System.Text.Encodings.Web.JavaScriptEncoder encoder) [0x00033] in <7e3a59f5e4004edbb4b17c580799cc52>:0 at System.Text.Json.JsonEncodedText.Encode (System.ReadOnlySpan1[T] value, System.Text.Encodings.Web.JavaScriptEncoder encoder) [0x00014] in <7e3a59f5e4004edbb4b17c580799cc52>:0
at System.Text.Json.JsonEncodedText.Encode (System.String value, System.Text.Encodings.Web.JavaScriptEncoder encoder) [0x00014] in <7e3a59f5e4004edbb4b17c580799cc52>:0
at System.Text.Json.JsonSerializer..cctor () [0x00042] in <7e3a59f5e4004edbb4b17c580799cc52>:0

But it works fine with Newtonsoft:

var payload = Newtonsoft.Json.JsonConvert.SerializeObject(postData);

I have the latest packages of everything.

Is it a known issue in iOS?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
{count} votes

Accepted answer
  1. Marco Seraphin 111 Reputation points
    2021-03-12T11:41:21.96+00:00

    To solve the crash when serialising JSON when logging in using System.Text.Json.JsonSerializer instead of NewtonSoft.JSON.

    => Method not found: int System.Text.Encodings.Web.TextEncoder.FindFirstCharacterToEncodeUtf8

    You have to add this to the iOS App project:

    <ItemGroup>
    <PackageReference Include="System.Memory" Version="4.5.4">
    <IncludeAssets>none</IncludeAssets>
    </PackageReference>
    <PackageReference Include="System.Buffers" Version="4.5.1">
    <IncludeAssets>none</IncludeAssets>
    </PackageReference>
    </ItemGroup>

    7 people found this answer helpful.

8 additional answers

Sort by: Most helpful
  1. Stesvis 1,041 Reputation points
    2021-03-11T15:29:39.08+00:00

    Here is a simple example.
    https://github.com/stesvis/SerializationTest

    I am trying both serializations, one with System.Text.Json (where it works with or without ReactiveObject) and one with Newtonsoft.
    In my real app, System.Text.Json throws the exception, but i can't reproduce it here. Maybe it depends on other packages installed?

    Anyway, take this code as a reference:

    Json = System.Text.Json.JsonSerializer.Serialize(data, new JsonSerializerOptions()
                    {
                        MaxDepth = 0,
                        IgnoreNullValues = true,
                        IgnoreReadOnlyProperties = true,
                        PropertyNameCaseInsensitive = true,
                        IgnoreReadOnlyFields = true,
                        IncludeFields = false,
                        //PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                    });
                    Json2 = Newtonsoft.Json.JsonConvert.SerializeObject(data, new JsonSerializerSettings()
                    {
                        NullValueHandling = NullValueHandling.Ignore,
                        MissingMemberHandling = MissingMemberHandling.Ignore,
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    });
    

    Newtonsoft works only if you remove :ReactiveObject from public partial class BaseDTO : ReactiveObject

    1 person found this answer helpful.
    0 comments No comments

  2. Chuck C Giddens 5 Reputation points
    2021-08-07T15:28:01.2+00:00

    I have this same issue with Xamarin Android project. I am using RestSharp and it cannot deserilaize some of my objects...works on most but crashes on a couple and cannot fix this issue.

    1 person found this answer helpful.
    0 comments No comments

  3. Duane Arnold 3,216 Reputation points
    2021-03-07T21:16:58.84+00:00

    Method not found: int System.Text.Encodings.Web.TextEncoder.FindFirstCharacterToEncodeUtf8(System.ReadOnlySpan`

    If the method is not there in the object in the DLL that is being called for, it's not there. Maybe, you need to use a more recent version of the DLL that may have the needed method in an object in the DLL. Or maybe, it's not ever going to be there on the platform you are having issues.

    0 comments No comments

  4. Zijian Huang 1 Reputation point
    2021-04-17T22:46:07.953+00:00

    Adding System.Memory and Buffers is not making difference, even after a clean build and installation. And this seems to be a recent break, more exactly a bug.

    0 comments No comments