diff --git a/src/Mono.Android/Java.Lang/Object.cs b/src/Mono.Android/Java.Lang/Object.cs index 68db056acb6..8d01578b39b 100644 --- a/src/Mono.Android/Java.Lang/Object.cs +++ b/src/Mono.Android/Java.Lang/Object.cs @@ -12,21 +12,21 @@ namespace Java.Lang { - [DataContract] + [Serializable] public partial class Object : IDisposable, IJavaObject, IJavaObjectEx #if JAVA_INTEROP , IJavaPeerable #endif // JAVA_INTEROP { - IntPtr key_handle; + [NonSerialized] IntPtr key_handle; #pragma warning disable CS0649, CS0169, CS0414 // Suppress fields are never used warnings, these fields are used directly by monodroid-glue.cc - IntPtr weak_handle; - int refs_added; + [NonSerialized] IntPtr weak_handle; + [NonSerialized] int refs_added; #pragma warning restore CS0649, CS0169, CS0414 - JObjectRefType handle_type; - IntPtr handle; - bool needsActivation; - bool isProxy; + [NonSerialized] JObjectRefType handle_type; + [NonSerialized] IntPtr handle; + [NonSerialized] bool needsActivation; + [NonSerialized] bool isProxy; IntPtr IJavaObjectEx.KeyHandle { get {return key_handle;} diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs index d8106acf7c9..5afcf9b908f 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs @@ -89,7 +89,6 @@ public void CheckIncludedAssemblies () "System.Console.dll", "System.Linq.Expressions.dll", "System.ObjectModel.dll", - "System.Runtime.Serialization.Primitives.dll", "System.Private.CoreLib.dll", "System.Collections.Concurrent.dll", "System.Collections.dll", @@ -103,7 +102,6 @@ public void CheckIncludedAssemblies () "System.Core.dll", "System.Data.dll", "System.dll", - "System.Runtime.Serialization.dll", "UnnamedProject.dll", "Mono.Data.Sqlite.dll", "Mono.Data.Sqlite.dll.config", diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/MainActivity.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/MainActivity.cs index 04a91ffc8e6..4b2ff76580c 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/MainActivity.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/MainActivity.cs @@ -32,6 +32,7 @@ protected override void OnCreate (Bundle bundle) //${AFTER_ONCREATE} } } + //${AFTER_MAINACTIVITY} } diff --git a/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs b/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs index ecbe02bb1f1..12e42fd37ac 100644 --- a/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs +++ b/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs @@ -419,5 +419,75 @@ public class LinkModeFullClass { } } + [Test] + public void JsonDeserializationCreatesJavaHandle ([Values (false, true)] bool isRelease) + { + AssertHasDevices (); + + proj = new XamarinAndroidApplicationProject () { + IsRelease = isRelease, + }; + + if (isRelease || !CommercialBuildAvailable) { + proj.SetAndroidSupportedAbis ("armeabi-v7a", "arm64-v8a", "x86"); + } + + proj.References.Add (new BuildItem.Reference ("System.Runtime.Serialization")); + + if (Builder.UseDotNet) + proj.References.Add (new BuildItem.Reference ("System.Runtime.Serialization.Json")); + + proj.MainActivity = proj.DefaultMainActivity.Replace ("//${AFTER_ONCREATE}", + @"TestJsonDeserializationCreatesJavaHandle(); + } + + void TestJsonDeserializationCreatesJavaHandle () + { + Person p = new Person () { + Name = ""John Smith"", + Age = 900, + }; + var stream = new MemoryStream (); + var serializer = new DataContractJsonSerializer (typeof (Person)); + + serializer.WriteObject (stream, p); + + stream.Position = 0; + StreamReader sr = new StreamReader (stream); + + Console.WriteLine ($""JSON Person representation: {sr.ReadToEnd ()}""); + + stream.Position = 0; + Person p2 = (Person) serializer.ReadObject (stream); + + Console.WriteLine ($""JSON Person parsed: Name '{p2.Name}' Age '{p2.Age}' Handle '0x{p2.Handle:X}'""); + + if (p2.Name != ""John Smith"") + throw new InvalidOperationException (""JSON deserialization of Name""); + if (p2.Age != 900) + throw new InvalidOperationException (""JSON deserialization of Age""); + if (p2.Handle == IntPtr.Zero) + throw new InvalidOperationException (""Failed to instantiate new Java instance for Person!""); + + Console.WriteLine ($""JSON Person deserialized OK"");").Replace ("//${AFTER_MAINACTIVITY}", @" + [DataContract] + class Person : Java.Lang.Object { + [DataMember] + public string Name; + + [DataMember] + public int Age; + }").Replace ("using System;", @"using System; +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Json;"); + builder = CreateApkBuilder (); + Assert.IsTrue (builder.Install (proj), "Install should have succeeded."); + ClearAdbLogcat (); + AdbStartActivity ($"{proj.PackageName}/{proj.JavaPackageName}.MainActivity"); + Assert.IsFalse (MonitorAdbLogcat ((line) => { + return line.Contains ("InvalidOperationException"); + }, Path.Combine (Root, builder.ProjectDirectory, "startup-logcat.log"), 45), $"Output did contain InvalidOperationException!"); + } } } diff --git a/tests/api-compatibility/acceptable-breakages-vReference.txt b/tests/api-compatibility/acceptable-breakages-vReference.txt index cbece75318a..46c870ca29d 100644 --- a/tests/api-compatibility/acceptable-breakages-vReference.txt +++ b/tests/api-compatibility/acceptable-breakages-vReference.txt @@ -22,3 +22,4 @@ CannotRemoveAttribute : Attribute 'System.ComponentModel.CategoryAttribute' exis CannotRemoveAttribute : Attribute 'System.ComponentModel.CategoryAttribute' exists on 'Android.Content.ContentProviderAttribute.Icon' in the contract but not the implementation. CannotRemoveAttribute : Attribute 'System.ComponentModel.CategoryAttribute' exists on 'Android.Content.ContentProviderAttribute.Label' in the contract but not the implementation. CannotRemoveAttribute : Attribute 'System.ComponentModel.CategoryAttribute' exists on 'Android.Content.ContentProviderAttribute.RoundIcon' in the contract but not the implementation. +CannotRemoveAttribute : Attribute 'System.Runtime.Serialization.DataContractAttribute' exists on 'Java.Lang.Object' in the contract but not the implementation. \ No newline at end of file