Bug 29660 - Polymorphic Json Deserialization error
Summary: Polymorphic Json Deserialization error
Status: RESOLVED FEATURE
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies ()
Version: 3.12.0
Hardware: Macintosh Mac OS
: Highest blocker
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-05-01 12:25 UTC by Jon Davis
Modified: 2015-07-01 12:49 UTC (History)
3 users (show)

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


Attachments
Polymorphic Json Sample (10.02 KB, application/zip)
2015-05-01 12:25 UTC, Jon Davis
Details


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 FEATURE

Description Jon Davis 2015-05-01 12:25:03 UTC
Created attachment 11026 [details]
Polymorphic Json Sample

May be a duplicate of Bug 26998. This happened after the follow bug was resolved fixed: Bug 3811

When running it I get basically the same stack trace as earlier but the paths are different. I’m not sure why that is, but when re-running our app I get the same error path as the test app does now as well.

Earlier:
//Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.2/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:190

Now:
/Developer/MonoTouch/Source/monotouch/_build
//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:190 

Stack trace:
2015-04-30 15:31:06.154 PolymorphismJsonDeserialization_iOS[1270:13823] System.Runtime.Serialization.SerializationException: Cannot load type 'KnownDerivedType:#PolymorphismJsonDeserializationConsole'. Error at  (1,92)
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type, System.Object instance) [0x00455] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:190 
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type) [0x00000] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:72 
  at System.Runtime.Serialization.Json.JsonSerializationReader.DeserializeGenericCollection (System.Type collectionType, System.Type elementType, System.Object collectionInstance) [0x0022d] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:354 
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type, System.Object instance) [0x00419] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:183 
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type) [0x00000] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:72 
  at System.Runtime.Serialization.Json.TypeMapProperty.SetMemberValue (System.Object owner, System.Runtime.Serialization.Json.JsonSerializationReader jsr) [0x00019] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/TypeMap.cs:361 
  at System.Runtime.Serialization.Json.TypeMap.Deserialize (System.Runtime.Serialization.Json.JsonSerializationReader jsr, System.Object o) [0x000f4] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/TypeMap.cs:263 
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type, System.Object instance) [0x00485] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:195 
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type) [0x00000] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:72 
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadRoot () [0x0000d] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs:66 
  at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlReader reader, Boolean verifyObjectName) [0x00045] in /Developer/MonoTouch/Source/monotouch/_build//Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs:202
Comment 1 Martin Baulig 2015-07-01 09:45:42 UTC
The error message in older versions of Xamarin.iOS is suboptimal and needs improvement, but that's already been fixed in the meantime.

Expect for the suboptimal error message, this Exception is not a bug, but correct and expected behavior.

Correct Exception message is

====

Unhandled Exception: System.Runtime.Serialization.SerializationException: Element ':item' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/PolymorphismJsonDeserializationConsole:KnownDerivedType'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'KnownDerivedType' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)

====
Comment 2 Carlo Wahlstedt 2015-07-01 12:33:44 UTC
The class implementation is already using the KnownTypeAttribute.

	[DataContract]
	[KnownType (typeof(KnownDerivedType))]
	[KnownType (typeof(KnownDerivedType_2))]
	public abstract class BaseType

Additionally, for verification, I added the known types to the DataContractJsonSerializer but the same exception still occurs.

	var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer (typeof(T), new [] {
		typeof(KnownDerivedType),
		typeof(KnownDerivedType_2)
	});
Comment 3 Martin Baulig 2015-07-01 12:49:38 UTC
Just had another look at this and the solution is actually quite simple:

It looks like the serializer is also looking at the type's namespace.  It's looking for a type called "PolymorphismJsonDeserializationConsole.KnownDerivedType" and can't find any because your namespace is called "PolymorphismJsonDeserialization_iOS".

I just verified that the Exception goes away by either renaming your namespace into "PolymorphismJsonDeserializationConsole" or adjusting the type name in the JSON.