0

The following code throws an exception ONLY on iOS - on Android it works as expected:

var content = new StringContent(
                            JsonSerializer.Serialize(new { UserName = userName, Password = password }), Encoding.UTF8, "application/json");

I saw the article at https://learn.microsoft.com/en-us/answers/questions/302548/jsonserializer-throws-exception-in-xamarinios.html but that solution did not solve my issue.

Any ideas? Or do I go back to Newtonsoft?

  • generally Newtonsoft is much preferred over JsonSerializer. – Jason Apr 28 '21 at 23:19
  • If you're on .net core 3 then you could try to explicetly reference the latest System.Text.Json (5.0.2 @ Apr 2021). – tymtam Apr 29 '21 at 02:25
  • Newtonsoft is preferred. What exception you are getting? Is that same exception which was in your reference link? Add try catch if you are not able capture exception exactly. – Ranjit Apr 29 '21 at 03:53

1 Answers1

1

The quick answer is that you have to be using .net framework 2.1 (not 2.0 as the wizard apparently set). Then, System.Text.JsonSerializer works as expected on both iOS and Android.