Click here to Skip to main content
Click here to Skip to main content
Go to top

Work-Around for DataContractJsonSerializer ArgumentNullExceptions

, 24 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Stop trailling nulls from preventing deserialization.
If you've tried using a DataContractJsonSerializer or a DataContractSerializer with Push Notification for the Windows Phone, you may have experienced an ArgumentNullException during deserialization. This can happen because the MemoryStream is buffered with null characters '\0' that prevent deserialization. A solution is to create a new array and copy all bytes except for the trailing nulls, as shown in the following excerpt:
 
using (BinaryReader reader = new BinaryReader(bodyStream))
{
	byte[] bodyBytes = reader.ReadBytes((int)bodyStream.Length);
 
	int lengthWithoutNulls = bodyBytes.Length;
	for (int i = bodyBytes.Length - 1; i >= 0 && bodyBytes[i] == '\0'; 
			i--, lengthWithoutNulls--)
	{
		/* Intentionally left blank. */
	}
 
	byte[] cleanedBytes = new byte[lengthWithoutNulls];
	Array.Copy(bodyBytes, cleanedBytes, lengthWithoutNulls);
 
	DataContractJsonSerializer serializer
		= new DataContractJsonSerializer(typeof(StockQuote));
 
	using (MemoryStream stream = new MemoryStream(cleanedBytes))
	{
		StockQuote = (StockQuote)serializer.ReadObject(stream);
	}
}
 
This excerpt is from the downloadable sample code in my upcoming book Windows Phone 7 Unleashed.
 

Have a great day!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniel Vaughan
President Outcoder
Switzerland Switzerland
Daniel Vaughan is a Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company dedicated to creating best-of-breed user experiences and leading-edge back-end solutions, using the Microsoft stack of technologies--in particular WPF, WinRT, Windows Phone, and also Xamarin.Forms.
 
Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.
 
Daniel is the developer behind several acclaimed Windows Phone apps including Surfy, Intellicam, and Splashbox; and is the creator of a number of popular open-source projects including Calcium SDK, and Clog.
 
Would you like Daniel to bring value to your organisation? Please contact

Daniel's Blog | MVP profile | Follow on Twitter
 
Windows Phone Experts
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralHello, i have studied your project regarding wp7 whic... PinmemberMohsin Malek16-Apr-11 0:20 
Hello,
i have studied your project regarding wp7 which is "Netflix Browser for Windows Phone 7 - Part 2"... and its really great. i have been studing and working on this for last few days... this is the best tutorial for me ever. i m develping same project for my practise with same layout to display some main service in panaroma control and then on click it will display sub services in pivot control as u have done. but the problem is when i m clicked on my main service list it only give me pivot control but not the sub services.
after long studied i thought its bcoz of database. so, if u dont mind can u help to be a good developer. plz.
i just want to know about the database fields which have been used and the wcf service which u have used.
plz help me.........
GeneralRe: Hi Mohsin, Yes, the Netflix Browser for Windows Phone 7 - P... PinmemberDaniel Vaughan16-Apr-11 8:37 
GeneralOk sure. Thanks for the comment. If you post your code, I'll... PinmemberDaniel Vaughan28-Oct-10 9:12 
GeneralOk sure. Thanks for the comment. If you post your code, I'll... PinmemberKatka Vaughan28-Oct-10 9:09 
GeneralReason for my vote of 2 Well this solves the problem but you... Pinmemberh3228-Oct-10 2:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 24 Oct 2010
Article Copyright 2010 by Daniel Vaughan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid