Skip to content

Commit

Permalink
Merge pull request #17 from AsrOneSdk/sriramvu-dev
Browse files Browse the repository at this point in the history
DataContractUtils in common client
  • Loading branch information
sriramvu committed Jan 22, 2015
2 parents 0e0cb60 + f557750 commit 7a3fe28
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,101 @@ private SiteRecoveryManagementClient GetSiteRecoveryClient()
return siteRecoveryClient;
}
}

/// <summary>
/// Helper around serialization/deserialization of objects. This one is a thin wrapper around
/// DataContractUtils template class which is the one doing the heavy lifting.
/// </summary>
[SuppressMessage(
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
"SA1402:FileMayOnlyContainASingleClass",
Justification = "Keeping all contracts together.")]
public static class DataContractUtils
{
/// <summary>
/// Serializes the supplied object to the string.
/// </summary>
/// <typeparam name="T">The object type.</typeparam>
/// <param name="obj">Object to serialize</param>
/// <returns>Serialized string.</returns>
public static string Serialize<T>(T obj)
{
return DataContractUtils<T>.Serialize(obj);
}

/// <summary>
/// Deserialize the string to the expected object type.
/// </summary>
/// <typeparam name="T">The object type</typeparam>
/// <param name="xmlString">Serialized string</param>
/// <param name="result">Deserialized object</param>
public static void Deserialize<T>(string xmlString, out T result)
{
result = DataContractUtils<T>.Deserialize(xmlString);
}
}

/// <summary>
/// Template class for DataContractUtils.
/// </summary>
/// <typeparam name="T">The object type</typeparam>
[SuppressMessage(
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
"SA1402:FileMayOnlyContainASingleClass",
Justification = "Keeping all contracts together.")]
public static class DataContractUtils<T>
{
/// <summary>
/// Serializes the propertyBagContainer to the string.
/// </summary>
/// <param name="propertyBagContainer">Property bag</param>
/// <returns>Serialized string </returns>
public static string Serialize(T propertyBagContainer)
{
var serializer = new DataContractSerializer(typeof(T));
string xmlString;
StringWriter sw = null;
try
{
sw = new StringWriter();
using (var writer = new XmlTextWriter(sw))
{
// Indent the XML so it's human readable.
writer.Formatting = Formatting.Indented;
serializer.WriteObject(writer, propertyBagContainer);
writer.Flush();
xmlString = sw.ToString();
}
}
finally
{
if (sw != null)
{
sw.Close();
}
}

return xmlString;
}

/// <summary>
/// Deserialize the string to the propertyBagContainer.
/// </summary>
/// <param name="xmlString">Serialized string</param>
/// <returns>Deserialized object</returns>
public static T Deserialize(string xmlString)
{
T propertyBagContainer;
using (Stream stream = new MemoryStream())
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlString);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
propertyBagContainer = (T)deserializer.ReadObject(stream);
}

return propertyBagContainer;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,103 +42,6 @@ public enum NetworkTargetType
Azure,
}

/// <summary>
/// Helper around serialization/deserialization of objects. This one is a thin wrapper around
/// DataContractUtils template class which is the one doing the heavy lifting.
/// </summary>
[SuppressMessage(
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
"SA1402:FileMayOnlyContainASingleClass",
Justification = "Keeping all contracts together.")]
public static class DataContractUtils
{
/// <summary>
/// Serializes the supplied object to the string.
/// </summary>
/// <typeparam name="T">The object type.</typeparam>
/// <param name="obj">Object to serialize</param>
/// <returns>Serialized string.</returns>
public static string Serialize<T>(T obj)
{
return DataContractUtils<T>.Serialize(obj);
}

/// <summary>
/// Deserialize the string to the expected object type.
/// </summary>
/// <typeparam name="T">The object type</typeparam>
/// <param name="xmlString">Serialized string</param>
/// <param name="result">Deserialized object</param>
public static void Deserialize<T>(string xmlString, out T result)
{
result = DataContractUtils<T>.Deserialize(xmlString);
}
}

/// <summary>
/// Template class for DataContractUtils.
/// </summary>
/// <typeparam name="T">The object type</typeparam>
[SuppressMessage(
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
"SA1402:FileMayOnlyContainASingleClass",
Justification = "Keeping all contracts together.")]
public static class DataContractUtils<T>
{
/// <summary>
/// Serializes the propertyBagContainer to the string.
/// </summary>
/// <param name="propertyBagContainer">Property bag</param>
/// <returns>Serialized string </returns>
public static string Serialize(T propertyBagContainer)
{
var serializer = new DataContractSerializer(typeof(T));
string xmlString;
StringWriter sw = null;
try
{
sw = new StringWriter();
using (var writer = new XmlTextWriter(sw))
{
// Indent the XML so it's human readable.
writer.Formatting = Formatting.Indented;
serializer.WriteObject(writer, propertyBagContainer);
writer.Flush();
xmlString = sw.ToString();
}
}
finally
{
if (sw != null)
{
sw.Close();
}
}

return xmlString;
}

/// <summary>
/// Deserialize the string to the propertyBagContainer.
/// </summary>
/// <param name="xmlString">Serialized string</param>
/// <returns>Deserialized object</returns>
public static T Deserialize(string xmlString)
{
T propertyBagContainer;
using (Stream stream = new MemoryStream())
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlString);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
propertyBagContainer = (T)deserializer.ReadObject(stream);
}

return propertyBagContainer;
}
}

/// <summary>
/// Create network mapping input.
/// </summary>
Expand Down

0 comments on commit 7a3fe28

Please sign in to comment.