Use System.Json.Text in UWP,
JsonSerializer.ToString(new { test = "abc" });
Failure occurred while loading a type.
This operation cannot be carried out because metadata for the following object was removed for performance reasons: <unavailable>
No further information is available. Rebuild in debug mode for better information.
cc @MichalStrehovsky
You can get more information in the exception message if you switch your build type to Debug and turn on .NET Native in the Debug configuration: switch the project configuration to Debug, and then in the project properties on the Build tab check the checkbox next to "Compile with .NET Native tool chain". The exception message will have more details after you do that.
Reflection-based serializers typically require RD.XML to make them work right out of the box. System.Text.Json wasn't tested on UWP and the experience will be rough.
For UWP, it's recommended to use existing serializers that the .NET Native compiler understands such as DataContractJsonSerializer or Netwonsoft.Json.
(I should probably point out that the reason why the experience it's going to be more rough for System.Text.Json is because this is a framework assembly and a lot of the things in the .NET Native compiler that try to make reflection experience better don't kick in for framework assemblies - framework assemblies are expected to carry their own RD.XML that describes their dynamic behavior so that the compiler can do aggressive treeshaking on them to reduce their size. We didn't make an investment into RD.XML for System.Text.Json.)
@MichalStrehovsky
I've got detailed exception message:
'System.Text.Json.Serialization.JsonPropertyInfoNotNullable<<>f__AnonymousType2<System.String>,System.String,System.String>' is missing metadata. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=392859
test code:
JsonSerializer.ToString(new { test = "abc" });
Oh, okay, I actually looked and right now it's impossible to make System.Text.Json work on UWP. The JsonPropertyInfoNotNullable type is marked [ReflectionBlocked] in the implementation assembly so .NET Native will completely block it from reflection. No amount of RD.XML can fix that.
When .NET Native support was deleted in CoreFX in dotnet/corefx#38170, along with it we deleted the logic that injects this attribute into framework assemblies. Next System.Text.Json release probably won't have that and we can try again.
@MichalStrehovsky
it's impossible to make System.Text.Json work on UWP.
Only if using .NET Native, right? I think the latest version of UWP should run with the regular .NET Core 2.2 runtime otherwise, where STJ seems to be working OK for me.
I think the latest version of UWP should run with the regular .NET Core 2.2 runtime otherwise, where STJ seems to be working OK for me.
.NET Native is still a requirement to ship UWP apps to the Store.
@ViktorHofer can you clarify the resolution on all these issues that are getting closed?
@sharwell you asked that question already on another issue. Please see my response there.
This one actually was a mistake and needs to be re-opened.
Just to bump this with something I discovered today, calls to JasonSerializer.Serialize() were working fine using UWP .NET Native against .NET Core 3.0. After updating to .NET Core 3.1 preview 3, calls to Serialize now fail with metadata exceptions.
https://github.com/dotnet/runtime/issues/978#issuecomment-567613428
I downgrade to 4.6.0 and it works fine without any rd.xml!
FYI @MattWhilden
Relinking to our Windows Community Toolkit Issue here as well.
Here's the issue I've been using to track this getting resolved. The current design question is around PreserveDependencyAttribute and whether it will be renamed/reused etc. In the mean time we'll need to have rd.xml workarounds.
I added this to rd.xml, seems to work.
<Namespace Name="System.Text.Json.Serialization.Converters" Browse="Required All"/>
Most helpful comment
I added this to
rd.xml, seems to work.