// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Xml.Serialization;
namespace Microsoft.TestCommon
{
///
/// MSTest utility for testing code operating against a stream.
///
public class SerializerAssert
{
private static SerializerAssert singleton = new SerializerAssert();
public static SerializerAssert Singleton { get { return singleton; } }
///
/// Creates a , serializes to it using
/// , rewinds the stream and calls .
///
/// The type to serialize. It cannot be null.
/// The value to serialize.
/// Code to check the contents of the stream.
public void UsingXmlSerializer(Type type, object objectInstance, Action codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
XmlSerializer serializer = new XmlSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
///
/// Creates a , serializes to it using
/// , rewinds the stream and calls .
///
/// The type to serialize.
/// The value to serialize.
/// Code to check the contents of the stream.
public void UsingXmlSerializer(T objectInstance, Action codeThatChecks)
{
UsingXmlSerializer(typeof(T), objectInstance, codeThatChecks);
}
///
/// Creates a , serializes to it using
/// , rewinds the stream and calls .
///
/// The type to serialize. It cannot be null.
/// The value to serialize.
/// Code to check the contents of the stream.
public void UsingDataContractSerializer(Type type, object objectInstance, Action codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
DataContractSerializer serializer = new DataContractSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
///
/// Creates a , serializes to it using
/// , rewinds the stream and calls .
///
/// The type to serialize.
/// The value to serialize.
/// Code to check the contents of the stream.
public void UsingDataContractSerializer(T objectInstance, Action codeThatChecks)
{
UsingDataContractSerializer(typeof(T), objectInstance, codeThatChecks);
}
///
/// Creates a , serializes to it using
/// , rewinds the stream and calls .
///
/// The type to serialize. It cannot be null.
/// The value to serialize.
/// Code to check the contents of the stream.
public static void UsingDataContractJsonSerializer(Type type, object objectInstance, Action codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
///
/// Creates a , serializes to it using
/// , rewinds the stream and calls .
///
/// The type to serialize.
/// The value to serialize.
/// Code to check the contents of the stream.
public void UsingDataContractJsonSerializer(T objectInstance, Action codeThatChecks)
{
UsingDataContractJsonSerializer(typeof(T), objectInstance, codeThatChecks);
}
}
}