Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Mono.Android/Java.Lang/Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected override void OnCreate (Bundle bundle)
//${AFTER_ONCREATE}
}
}
//${AFTER_MAINACTIVITY}
}


70 changes: 70 additions & 0 deletions tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.