Bug 6658 - Cannot de-serialize an object using DataContractJsonSerializer
Summary: Cannot de-serialize an object using DataContractJsonSerializer
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Runtime.Serialization ()
Version: unspecified
Hardware: PC Windows
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-08-23 07:17 UTC by Chris
Modified: 2018-03-13 10:49 UTC (History)
5 users (show)

Tags: XATriaged
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 Chris 2012-08-23 07:17:24 UTC
I have a fairly complex object which contains within it a number of List<string>, List<MethodStatement> and List<RiskAssessment> which I serialize using the code below:-

private static string Serialize(object objectToSerialize)
{
	{
		using (MemoryStream ms = new MemoryStream())
		{
			DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
			serializer.WriteObject(ms, objectToSerialize);
			ms.Position = 0;

			using (StreamReader reader = new StreamReader(ms))
			{
				return reader.ReadToEnd();
			}
		}
	}
	catch (Exception ex)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(Application.Context);
		builder.SetMessage("Error in IsolatedStorage Serialize: Message is " + ex.Message);
		builder.SetTitle("SharedPreferences Exception");
		builder.SetPositiveButton("Ok", delegate { return; });
		builder.Show();
	}

	return "";
}


...and de-serialize with 

private static T Deserialize<T>(string jsonString)
{
	try
	{
	using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
	{
		DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
		return (T)serializer.ReadObject(ms);
	}
	}
	catch (Exception ex)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(Application.Context);
		builder.SetMessage("Error in IsolatedStorage Deserialize: Message is " + ex.Message);
		builder.SetTitle("SharedPreferences Exception");
		builder.SetPositiveButton("Ok", delegate { return; });
		builder.Show();
	}

	return default(T);
}

This has been working previously and forms the basis of much of my application!  I guess I have upgraded Mono for Android at some point and now this no longer works - I get an exception thrown by the de-serializer 

System.Runtime.Serialization.SerializationException: Deserialization has failed ---> System.ArgumentException: failed to convert parameters
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Json.TypeMapProperty.SetMemberValue (System.Object owner, System.Object value) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Json.TypeMap.Deserialize (System.Runtime.Serialization.Json.JsonSerializationReader jsr) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Json.JsonSerializationReader.ReadRoot () [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlReader reader, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlReader reader, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlDictionaryReader reader, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.IO.Stream stream) [0x00000] in <filename unknown>:0 
  at TTL.Mbrss.SharedPreferencesHelper.Deserialize[Whereabouts] (System.String jsonString) [0x00024] in E:\CMD Technology\Development Sources\MBRSS\mbbrs\SharedPreferencesHelper.cs:111 }

I assume that one of the arrays is failing to de-serialize but the code has not changed for a while now.

This is very urget however as my application needs to be updated and I cannot even get it to run now.
Comment 1 Alexey Sednev 2013-02-13 14:14:34 UTC
I'm experiencing similar issue but only when I try to deserialize ObservableCollection<T>. It does work fine in Silverlight(Windows Phone) and WinRT. Changing ObservableCollection<T> to List<T> solves the problem.

Also, it's worth mentioning that T is an abstract class and instead of it I have 2 types that derive from T. KnownTypeAttribute is used to specify the types used.
Comment 2 Marek Safar 2018-03-13 10:49:22 UTC
Thank you for your report!

I cannot reproduce the issue with Mono master. If the issue still persists please include the version information and change the bug status to NEW.