Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json Serializer does not appear to work on Xamarin iOS #31326

Open
jefffhaynes opened this issue Oct 28, 2019 · 14 comments
Open

System.Text.Json Serializer does not appear to work on Xamarin iOS #31326

jefffhaynes opened this issue Oct 28, 2019 · 14 comments
Milestone

Comments

@jefffhaynes
Copy link

@jefffhaynes jefffhaynes commented Oct 28, 2019

Perhaps not surprisingly, System.Text.Json.Serializer does not appear to function on iOS likely owing to the lack of Emit support.

public class ChatMessage
{
    [JsonPropertyName("id")]
    public string Id { get; set; }

    [JsonPropertyName("sent")]
    public DateTime? SentOn { get; set; }
}

JsonSerializer.Deserialize<ChatMessage>(json);

This yields:

System.ExecutionEngineException: Attempting to JIT compile method '(wrapper delegate-invoke) void :invoke_callvirt_void_ChatMessage_Nullable1<DateTime> (Chat.Messaging.ChatMessage,System.Nullable1<System.DateTime>)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.

@ericstj

This comment has been minimized.

Copy link
Member

@ericstj ericstj commented Oct 28, 2019

@steveharter

This comment has been minimized.

Copy link
Member

@steveharter steveharter commented Oct 28, 2019

FWIW there is a fallback today to just use standard reflection which is currently enabled in source-only builds (not the "inbox" builds).

@jefffhaynes

This comment has been minimized.

Copy link
Author

@jefffhaynes jefffhaynes commented Oct 28, 2019

No worries I switched back to newtonsoft for now. I'll switch back once it's ready. Thanks!

@sfedotovs

This comment has been minimized.

Copy link

@sfedotovs sfedotovs commented Oct 31, 2019

have the same issue with Json Serializer on Xamarin.iOS.

@steveharter can you elaborate how your note helps in this situation?
I found the place in JsonSerializerOptions.cs

#if BUILDING_INBOX_LIBRARY
                    _memberAccessorStrategy = new ReflectionEmitMemberAccessor();
#else
                    // todo: should we attempt to detect here, or at least have a #define like #SUPPORTS_IL_EMIT
                    _memberAccessorStrategy = new ReflectionMemberAccessor();
#endif

I use nuget package, it should resolve to ReflectionMemberAccessor but the issue still exists. I believe it is not related to Emit, but some other place where JIT is used (Delegates?).

Is there a hope to get fix for the issue in nearest future (at least in preview)? Or should we consider to choose another JsonSerializer to use on Xamarin iOS?

@jkotas

This comment has been minimized.

Copy link
Member

@jkotas jkotas commented Oct 31, 2019

some other place where JIT is used (Delegates?).

Yes, there are number of other constructs that System.Text.Json uses that are not compatible with full AOT compilation. For example, MakeGenericType is not compatible with full AOT compilation. Some of the delegate constructs that you have mentioned are not friendly to full AOT compilation either.

Is there a hope to get fix for the issue in nearest future (at least in preview)?

Unlikely. Making System.Text.Json compatible with full AOT compilation will require fundamental changes. I believe that the best fix is going to be to pre-generate the serializers at build time:
https://github.com/dotnet/corefx/issues/41398

cc @marek-safar

@klogeaage

This comment has been minimized.

Copy link

@klogeaage klogeaage commented Nov 21, 2019

So shouldn't the dependencies of the NuGet package reflect that it simply should not be attempted on Xamarin iOS? I actually found this surprising and have now wasted several hours moving from Json.Net to System.Text.Json because I liked the idea of having the same serializer in both front-end and the .Net Core 3.0 back-end. And because of the performance improvements that it should give.

Another surprise was that it works on the Xamarin iOS simulator, but not on the real device.

So this was a dead-end and that would have been nice to know beforehand.

@huoyaoyuan

This comment has been minimized.

Copy link

@huoyaoyuan huoyaoyuan commented Nov 25, 2019

Interesting that I can make System.Text.Json working on UWP .Net Native after adding some linker instructions.

@sasivishnu

This comment has been minimized.

Copy link

@sasivishnu sasivishnu commented Nov 27, 2019

So shouldn't the dependencies of the NuGet package reflect that it simply should not be attempted on Xamarin iOS? I actually found this surprising and have now wasted several hours moving from Json.Net to System.Text.Json because I liked the idea of having the same serializer in both front-end and the .Net Core 3.0 back-end. And because of the performance improvements that it should give.

Another surprise was that it works on the Xamarin iOS simulator, but not on the real device.

So this was a dead-end and that would have been nice to know beforehand.

Yes, It will be nice to mention not to use this package where AOT is enabled? We too switched using NewtonSoft Json for our company xamarin mobile app project for now.

@PaulVrugt

This comment has been minimized.

Copy link

@PaulVrugt PaulVrugt commented Dec 23, 2019

what the hell? So this package isn't compatible with iOS at all?

@klogeaage

This comment has been minimized.

Copy link

@klogeaage klogeaage commented Dec 23, 2019

Nope - very surprising, to say the least. And what is even worse: you don't discover this until you spent time on implementing it, then get the weird error message when running on a real iOS device, then search on Google and then find this issue. Not a good developer experience at all...

@PaulVrugt

This comment has been minimized.

Copy link

@PaulVrugt PaulVrugt commented Dec 23, 2019

We even released a version of our app, because we didn't find anything wrong, because "most" cases simply work. We now have to create a new version and revert to newtonsoft.json because of all of the functionality in our app about 2 functions are not working.

@klogeaage

This comment has been minimized.

Copy link

@klogeaage klogeaage commented Dec 23, 2019

Interesting. I was lucky then that my app didn't even start correctly.

@danmosemsft

This comment has been minimized.

Copy link
Member

@danmosemsft danmosemsft commented Dec 31, 2019

Yes, It will be nice to mention not to use this package where AOT is enabled? We too switched using NewtonSoft Json for our company xamarin mobile app project for now.

Note the package includes functionality that does not care about AOT eg the readers/writers.

@eman1986

This comment has been minimized.

Copy link

@eman1986 eman1986 commented Feb 18, 2020

Are there plans to get this working for iOS? My team just upgraded our api website to NETCore 3.1 and we switched out to the System.Text.Json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.