Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dotnet] Enable the reflection-based json serializer by default. Fixes
Browse files Browse the repository at this point in the history
rolfbjarne committed Aug 11, 2023
1 parent c1f56a0 commit 09d494f
Showing 3 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.props
Original file line number Diff line number Diff line change
@@ -65,6 +65,9 @@
-->
<!-- AutoreleasePoolSupport needs to be set earlier than other switches, so that illink doesn't override it - https://github.com/dotnet/runtime/pull/86753 - so it's set here, instead of in Xamarin.Shared.Sdk.targets -->
<AutoreleasePoolSupport Condition="'$(AutoreleasePoolSupport)' == ''">true</AutoreleasePoolSupport>

<!-- Enable the reflection-based json serializer by default. Ref: https://github.com/xamarin/xamarin-macios/issues/18057 -->
<JsonSerializerIsReflectionEnabledByDefault Condition="'$(JsonSerializerIsReflectionEnabledByDefault)' == ''">true</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

<!-- Set the default RuntimeIdentifier if not already specified. -->
25 changes: 25 additions & 0 deletions tests/linker/CommonLinkAnyTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if NET
using System.Text.Json;
#endif

using Foundation;
using ObjCRuntime;
@@ -66,5 +69,27 @@ public void BackingFieldInGenericType ()
GC.KeepAlive (view.HeightAnchor);
}
#endif // !__WATCHOS__

#if NET
[Test]
public void JsonSerializer_Serialize ()
{
var a = JsonSerializer.Serialize (42);
Assert.AreEqual ("42", a, "serialized 42");

var b = JsonSerializer.Serialize (new int [] { 42, 3, 14, 15 });
Assert.AreEqual ("[42,3,14,15]", b, "serialized array");
}

[Test]
public void JsonSerializer_Deserialize ()
{
var a = JsonSerializer.Deserialize<int> ("42");
Assert.AreEqual (42, a, "deserialized 42");

var b = JsonSerializer.Deserialize<int[]> ("[42,3,14,15]");
CollectionAssert.AreEqual (new int [] { 42, 3, 14, 15 }, b, "deserialized array");
}
#endif
}
}
20 changes: 20 additions & 0 deletions tests/monotouch-test/mono/BclTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Text.Json;

using NUnit.Framework;

#nullable enable

namespace MonoTouchFixtures.System.Text.Json {
[TestFixture]
[Preserve (AllMembers = true)]
public partial class SerializationTest {
[Test]
public void Serialize ()
{
var txt = JsonSerializer.Serialize (42);
Console.WriteLine (txt);
Assert.AreEqual ("?", txt, "serialized blob");
}
}
}

0 comments on commit 09d494f

Please sign in to comment.