How Do I Serialize / Deserialize Tidbits...

How do I serialize / deserialize between (XML Schema) XSD:Duration from and to .NET TimeSpan?

Apparently, there is no obvious way to do this like you could when dealing with DateTime object conversion to and from XSD:DateTime.  With DateTime you can do something like so:

   1:  //Serialize DateTime object to xsd:DateTime format
   2:  DateTime date1 = DateTime.Now;
   3:  string serializedDate1 = date1.ToString("yyyy-MM-ddTHH:mm:ssK");
   4:   
   5:  //Deserialize an xsd:DateTime format to a DateTime object
   6:  string serializedDate2 = "2008-04-14T21:00:00+07:00";
   7:  DateTime date2 = DateTime.Parse(serializedDate2);

But you cannot deserialize xsd:Duration to TimeSpan using TimeSpan.Parse and there is no obvious way to serialize the TimeSpan object also.

After digging around a little bit more, I encountered System.Runtime.Remoting.Metadata.W3cXsd2001 namespace.  What is this you might ask?  Well, it turned out that this is exactly the correct namespace to do Soap Serialization / Deserialization.  Just plop that namespace in the using section and you are ready to go.

The following code is the equivalent of the code above:

   1:  //Serialize DateTime object to xsd:DateTime format
   2:  DateTime date1 = DateTime.Now;
   3:  string serializedDate1 = SoapDateTime.ToString(date1);
   4:   
   5:  //Deserialize an xsd:DateTime to a Datetime object
   6:  string serializedDate2 = "2008-04-14T21:00:00+07:00";
   7:  DateTime date2 = SoapDateTime.Parse(serializedDate2);

And this is the code you need to serialize / deserialize TimeSpan object:

   1:  //Serialize TimeSpan object to xsd:Duration format
   2:  TimeSpan timeSpan1 = new TimeSpan(1, 0, 0);
   3:  string serializedTimeSpan1 = SoapDuration.ToString(timeSpan1);
   4:   
   5:  //Deserialize xsd:Duration to TimeSpan object
   6:  string serializedTimeSpan2 = "PT1H";
   7:  TimeSpan timeSpan2 = SoapDuration.Parse(serializedTimeSpan2);

To learn more about XML Schema go to http://www.w3schools.com/Schema/default.asp.

How do I serialize / deserialize object from and to JSON in .NET?

To do this, simply install ASP.NET AJAX extension on your machine.  You shouldn't need to if you have Visual Studio 2008 installed on your machine since it's already installed by default.  You will need to add the System.Web.Script.Serialization namespace to your using section.

Say you have a .NET User class like so:

   1:      public class User
   2:      {
   3:          public string Account { get; set; }
   4:          public string FullName { get; set; }
   5:      }

Serializing / Deserializing User object can be as simple as this:

   1:  string json = serializer.Serialize(new User { Account = "DOMAIN\\johndoe", FullName = "John Doe" });
   2:  User user = serializer.Deserialize<User>(json);

Apparently, .NET Framework 3.5 have some new stuffs up its sleeve like DataContractJsonSerializer that is supposed to replace the now obsolete JavascriptSerializer.

Share this post: | | | |
Published Monday, April 14, 2008 11:17 PM by Jimmy Chandra
Filed under: , ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: