Bug 23058 - DataContractJsonSerializer - Deserialize type incorrect on iOS
Summary: DataContractJsonSerializer - Deserialize type incorrect on iOS
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies (show other bugs)
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Alexis Christoforides
URL:
Depends on:
Blocks:
 
Reported: 2014-09-16 12:30 UTC by Jon Douglas [MSFT]
Modified: 2015-02-12 11:45 UTC (History)
6 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jon Douglas [MSFT] 2014-09-16 12:30:04 UTC
Customer Description:

I'm facing an issue on iOS with Json deserialisation using the DataContractJsonSerializer. I have the following class structure:



[DataContract]
public class ParentType
{
[DataMemberAttribute]
public string Name
{
get; set;
}

[DataMemberAttribute]
public BaseType BaseType
{
get; set;
}
}



[DataContract]
[KnownType(typeof(DerivedType1))]
[KnownType(typeof(DerivedType2))]
public class BaseType
{
[DataMemberAttribute]
public string BaseTypeName
{
get; set;
}
}



[DataContract]
public class DerivedType1 : BaseType
{
[DataMemberAttribute]
public string DerivedType1Property
{
get; set;
}
}



[DataContract]
public class DerivedType2 : BaseType
{
[DataMember]
public string DerivedType2Property
{
get;
set;;
}
}

I have a WCF REST service declared as the following:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
ParentType GetPolymorphicObject();



The GetPolymorphicObject function is defined as the following:

public ParentType GetPolymorphicObject()
{
var myDerived = new DerivedType1 { DerivedType1Property = "Derived 1" };
var myObect = new ParentType { Name = "Parent", BaseType = myDerived };

return myObect;
}

When testing this REST service call with a WPF application the ParentType object is deserialised with the BaseType property which has the type DerivedType1 which is as expected. However, upon testing the service with a sample app on the iOS simulator or device the BaseType property is of type BaseType which in this instance is incorrect. The Json message recieved is the following:

{"BaseType":{"__type":"DerivedType1:#PortableClassLibrary1.Types","BaseTypeName":null,"DerivedType1Property":"Derived 1"},"Name":"Parent2"}

Xamarin version details below:
Xamarin 4.2.3 (build 60)
Mono runtime 3.2.6 ((no/9b58377)
Xcode 5.1.1 (5085)
Xamarin.iOS Version: 7.2.0.2 (Business Edition)
Comment 2 Alexis Christoforides 2014-10-07 19:31:18 UTC
The bug was fixed on master:

https://github.com/mono/mono/commit/71e152accc727089faffa6b4fd6bf58cb62636cf

Related tests to the change:

https://github.com/mono/mono/commit/fbea92153684c2db093a44830818d226fdb17862

This should also fix the other serialization problems noted in the forums.