Skip to content

Commit 7a3fe28

Browse files
committed
Merge pull request #17 from AsrOneSdk/sriramvu-dev
DataContractUtils in common client
2 parents 0e0cb60 + f557750 commit 7a3fe28

File tree

2 files changed

+97
-97
lines changed

2 files changed

+97
-97
lines changed

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesClient.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,101 @@ private SiteRecoveryManagementClient GetSiteRecoveryClient()
262262
return siteRecoveryClient;
263263
}
264264
}
265+
266+
/// <summary>
267+
/// Helper around serialization/deserialization of objects. This one is a thin wrapper around
268+
/// DataContractUtils template class which is the one doing the heavy lifting.
269+
/// </summary>
270+
[SuppressMessage(
271+
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
272+
"SA1402:FileMayOnlyContainASingleClass",
273+
Justification = "Keeping all contracts together.")]
274+
public static class DataContractUtils
275+
{
276+
/// <summary>
277+
/// Serializes the supplied object to the string.
278+
/// </summary>
279+
/// <typeparam name="T">The object type.</typeparam>
280+
/// <param name="obj">Object to serialize</param>
281+
/// <returns>Serialized string.</returns>
282+
public static string Serialize<T>(T obj)
283+
{
284+
return DataContractUtils<T>.Serialize(obj);
285+
}
286+
287+
/// <summary>
288+
/// Deserialize the string to the expected object type.
289+
/// </summary>
290+
/// <typeparam name="T">The object type</typeparam>
291+
/// <param name="xmlString">Serialized string</param>
292+
/// <param name="result">Deserialized object</param>
293+
public static void Deserialize<T>(string xmlString, out T result)
294+
{
295+
result = DataContractUtils<T>.Deserialize(xmlString);
296+
}
297+
}
298+
299+
/// <summary>
300+
/// Template class for DataContractUtils.
301+
/// </summary>
302+
/// <typeparam name="T">The object type</typeparam>
303+
[SuppressMessage(
304+
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
305+
"SA1402:FileMayOnlyContainASingleClass",
306+
Justification = "Keeping all contracts together.")]
307+
public static class DataContractUtils<T>
308+
{
309+
/// <summary>
310+
/// Serializes the propertyBagContainer to the string.
311+
/// </summary>
312+
/// <param name="propertyBagContainer">Property bag</param>
313+
/// <returns>Serialized string </returns>
314+
public static string Serialize(T propertyBagContainer)
315+
{
316+
var serializer = new DataContractSerializer(typeof(T));
317+
string xmlString;
318+
StringWriter sw = null;
319+
try
320+
{
321+
sw = new StringWriter();
322+
using (var writer = new XmlTextWriter(sw))
323+
{
324+
// Indent the XML so it's human readable.
325+
writer.Formatting = Formatting.Indented;
326+
serializer.WriteObject(writer, propertyBagContainer);
327+
writer.Flush();
328+
xmlString = sw.ToString();
329+
}
330+
}
331+
finally
332+
{
333+
if (sw != null)
334+
{
335+
sw.Close();
336+
}
337+
}
338+
339+
return xmlString;
340+
}
341+
342+
/// <summary>
343+
/// Deserialize the string to the propertyBagContainer.
344+
/// </summary>
345+
/// <param name="xmlString">Serialized string</param>
346+
/// <returns>Deserialized object</returns>
347+
public static T Deserialize(string xmlString)
348+
{
349+
T propertyBagContainer;
350+
using (Stream stream = new MemoryStream())
351+
{
352+
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlString);
353+
stream.Write(data, 0, data.Length);
354+
stream.Position = 0;
355+
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
356+
propertyBagContainer = (T)deserializer.ReadObject(stream);
357+
}
358+
359+
return propertyBagContainer;
360+
}
361+
}
265362
}

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesNetworkMappingClient.cs

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -42,103 +42,6 @@ public enum NetworkTargetType
4242
Azure,
4343
}
4444

45-
/// <summary>
46-
/// Helper around serialization/deserialization of objects. This one is a thin wrapper around
47-
/// DataContractUtils template class which is the one doing the heavy lifting.
48-
/// </summary>
49-
[SuppressMessage(
50-
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
51-
"SA1402:FileMayOnlyContainASingleClass",
52-
Justification = "Keeping all contracts together.")]
53-
public static class DataContractUtils
54-
{
55-
/// <summary>
56-
/// Serializes the supplied object to the string.
57-
/// </summary>
58-
/// <typeparam name="T">The object type.</typeparam>
59-
/// <param name="obj">Object to serialize</param>
60-
/// <returns>Serialized string.</returns>
61-
public static string Serialize<T>(T obj)
62-
{
63-
return DataContractUtils<T>.Serialize(obj);
64-
}
65-
66-
/// <summary>
67-
/// Deserialize the string to the expected object type.
68-
/// </summary>
69-
/// <typeparam name="T">The object type</typeparam>
70-
/// <param name="xmlString">Serialized string</param>
71-
/// <param name="result">Deserialized object</param>
72-
public static void Deserialize<T>(string xmlString, out T result)
73-
{
74-
result = DataContractUtils<T>.Deserialize(xmlString);
75-
}
76-
}
77-
78-
/// <summary>
79-
/// Template class for DataContractUtils.
80-
/// </summary>
81-
/// <typeparam name="T">The object type</typeparam>
82-
[SuppressMessage(
83-
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
84-
"SA1402:FileMayOnlyContainASingleClass",
85-
Justification = "Keeping all contracts together.")]
86-
public static class DataContractUtils<T>
87-
{
88-
/// <summary>
89-
/// Serializes the propertyBagContainer to the string.
90-
/// </summary>
91-
/// <param name="propertyBagContainer">Property bag</param>
92-
/// <returns>Serialized string </returns>
93-
public static string Serialize(T propertyBagContainer)
94-
{
95-
var serializer = new DataContractSerializer(typeof(T));
96-
string xmlString;
97-
StringWriter sw = null;
98-
try
99-
{
100-
sw = new StringWriter();
101-
using (var writer = new XmlTextWriter(sw))
102-
{
103-
// Indent the XML so it's human readable.
104-
writer.Formatting = Formatting.Indented;
105-
serializer.WriteObject(writer, propertyBagContainer);
106-
writer.Flush();
107-
xmlString = sw.ToString();
108-
}
109-
}
110-
finally
111-
{
112-
if (sw != null)
113-
{
114-
sw.Close();
115-
}
116-
}
117-
118-
return xmlString;
119-
}
120-
121-
/// <summary>
122-
/// Deserialize the string to the propertyBagContainer.
123-
/// </summary>
124-
/// <param name="xmlString">Serialized string</param>
125-
/// <returns>Deserialized object</returns>
126-
public static T Deserialize(string xmlString)
127-
{
128-
T propertyBagContainer;
129-
using (Stream stream = new MemoryStream())
130-
{
131-
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlString);
132-
stream.Write(data, 0, data.Length);
133-
stream.Position = 0;
134-
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
135-
propertyBagContainer = (T)deserializer.ReadObject(stream);
136-
}
137-
138-
return propertyBagContainer;
139-
}
140-
}
141-
14245
/// <summary>
14346
/// Create network mapping input.
14447
/// </summary>

0 commit comments

Comments
 (0)