<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hans Rasmussen &#187; serialization</title>
	<atom:link href="http://www.hansrasmussen.com/tag/serialization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hansrasmussen.com</link>
	<description>info@hansrasmussen.com, +46 (0)723 207008</description>
	<lastBuildDate>Wed, 01 Feb 2012 11:59:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Consuming a JSON service with C#</title>
		<link>http://www.hansrasmussen.com/2012/02/consuming-a-json-service-with-csharp/</link>
		<comments>http://www.hansrasmussen.com/2012/02/consuming-a-json-service-with-csharp/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 11:58:16 +0000</pubDate>
		<dc:creator>Hans Rasmussen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[Webservice]]></category>

		<guid isPermaLink="false">http://www.hansrasmussen.com/?p=803</guid>
		<description><![CDATA[If you like to consume a JSON web service in C# this example might be handy. You need to know the url (web address) of the web service You need to understand what the web service will return You need to know what kind of security the web service accepts (My example uses Basic Authentication) [...]]]></description>
			<content:encoded><![CDATA[<p>If you like to consume a JSON web service in C# this example might be handy.</p>
<p>You need to know the url (web address) of the web service<br />
You need to understand what the web service will return<br />
You need to know what kind of security the web service accepts (My example uses Basic Authentication)<br />
You need to understand under which context you are consuming the service (My example uses a Proxy server to communicate)</p>
<p>I found a site that automatically generates a class based on the result of the JSON web service at <a href="http://json2csharp.com/">http://json2csharp.com/</a>. This site can generate an class either by you specifying the url to the web service or providing the JSON result from the service you wish to consume.</p>
<p>Lets just assume in the example that my service will return {&#8220;variable1&#8243;:1,&#8221;variable2&#8243;:&#8221;a return string&#8221;}</p>
<p>By definition this means one variable with an int and one with a string.</p>
<p>JSON2CSHARP will return a usable class for me to use in my code;</p>
<p>public class RootObject<br />
{<br />
    public int variable1 { get; set; }<br />
    public string variable2 { get; set; }<br />
}</p>
<p>Lets go ahead and add some more code to consume this now when we have a nice helper class to contain my result.</p>
<p>string url = &#8220;<a href="http://yourserver/service?someparams=somevalue">http://yourserver/service?someparams=somevalue</a>&#8220;;</p>
<p>HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);<br />
string username = &#8220;username&#8221;;<br />
string password = &#8220;********&#8221;;</p>
<p>// Use the CredentialCache so we can attach the authentication to the request<br />
CredentialCache mycache = new CredentialCache();</p>
<p>// We want to use Basic Authentication<br />
mycache.Add(new Uri(url), &#8220;Basic&#8221;, new NetworkCredential(username, password));<br />
wr.Credentials = mycache;<br />
wr.Headers.Add(&#8220;Authorization&#8221;, &#8220;Basic &#8221; + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + &#8220;:&#8221; + password)));</p>
<p>// Proxy (if you do not need it &#8211; ommit it)<br />
wr.Proxy = new WebProxy(<a href="http://proxyserver:8080">http://proxyserver:8080</a>);</p>
<p>// Get the response from the web service<br />
HttpWebResponse response = (HttpWebResponse)wr.GetResponse();<br />
Stream r_stream = response.GetResponseStream();</p>
<p>//convert it<br />
StreamReader response_stream = new StreamReader(r_stream, System.Text.Encoding.GetEncoding(&#8220;utf-8&#8243;));</p>
<p>string jSon = response_stream.ReadToEnd();</p>
<p>//clean up your stream<br />
response_stream.Close();</p>
<p>System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();<br />
 RootObject result = jsSerializer.Deserialize&lt;RootObject&gt;(jSon);</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hansrasmussen.com/2012/02/consuming-a-json-service-with-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DataContractSerializer &#8211; Alphabetical mystique</title>
		<link>http://www.hansrasmussen.com/2010/02/datacontractserializer-alphabetical-mystique/</link>
		<comments>http://www.hansrasmussen.com/2010/02/datacontractserializer-alphabetical-mystique/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 23:46:47 +0000</pubDate>
		<dc:creator>Hans Rasmussen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.hansrasmussen.com/2010/02/datacontractserializer-alphabetical-mystique/</guid>
		<description><![CDATA[http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx I noticed that DataContractSerializer is using serialization in alphabetical order. If you do not follow the below rules you will end up missing data after deserialization of your XML.     [Serializable, DataContract(Namespace = "your-namespace")]     public class YourObject : DataModelDeserializable&#60;YourObject&#62;     {         [DataMember]         public string PostalCode { get; set; }         [DataMember] [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx">http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx</a></p>
<p>I noticed that DataContractSerializer is using serialization in alphabetical order. If you do not follow the below rules you will end up missing data after deserialization of your XML.</p>
<p>    [Serializable, DataContract(Namespace = "your-namespace")]</p>
<p>    public class YourObject : DataModelDeserializable&lt;YourObject&gt;</p>
<p>    {</p>
<p>        [DataMember]</p>
<p>        public string PostalCode { get; set; }</p>
<p>        [DataMember]</p>
<p>        public string City { get; set; }</p>
<p>        [DataMember]</p>
<p>        public DateTime Street { get; set; }</p>
<p>    }</p>
<p>If you would serialize the above the XML would be in <strong>alphabetical</strong> order;</p>
<p>&lt;City&gt;New York&lt;/City&gt;</p>
<p>&lt;PostalCode&gt;23001&lt;/PostalCode&gt;</p>
<p>&lt;Street&gt;1st Avenue&lt;/Street&gt;</p>
<p>If you would now create your XML from another component, intending to use it with the deserialize process you might mistake the alphabetical order;</p>
<p>&lt;Street&gt;1st Avenue&lt;/Street&gt;</p>
<p>&lt;PostalCode&gt;23001&lt;/PostalCode&gt;</p>
<p>&lt;City&gt;New York&lt;/City&gt;</p>
<p>You will not notice that the properties are not filled with any values from the elements in the XML. The reason for this is the alphabetical processing. You might even discover that only some properties are correct because you start with having some in the right alphabetical order but fail with the rest of the XML and the equivalent properties.</p>
<p>You can solve this in 2 ways;</p>
<p>1. Make sure that the XML file elements are in alphabetical order.</p>
<p>2. The first might fail because you are limited to control the element order so the you can instruct DataContractSerializer that it should order its serialization differently.</p>
<p>    [Serializable, DataContract(Namespace = "your-namespace")]</p>
<p>    public class YourObject : DataModelDeserializable&lt;YourObject&gt;</p>
<p>    {</p>
<p>        [DataMember(Order=2)]</p>
<p>        public string PostalCode { get; set; }</p>
<p>        [DataMember(Order=3)]</p>
<p>        public string City { get; set; }</p>
<p>        [DataMember(Order=1)]</p>
<p>        public DateTime Street { get; set; }</p>
<p>    }</p>
<p>/* now the serialization process is done in order Street, PostalCode, City */</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hansrasmussen.com/2010/02/datacontractserializer-alphabetical-mystique/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

