// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.AspNet.SignalR.Infrastructure;
namespace Microsoft.AspNet.SignalR.Json
{
///
/// Extensions for .
///
public static class JsonSerializerExtensions
{
///
/// Deserializes the JSON to a .NET object.
///
/// The serializer
/// The of object being deserialized.
/// The JSON to deserialize
/// The deserialized object from the JSON string.
public static T Parse(this IJsonSerializer serializer, string json)
{
if (serializer == null)
{
throw new ArgumentNullException("serializer");
}
using (var reader = new StringReader(json))
{
return (T)serializer.Parse(reader, typeof(T));
}
}
///
/// Deserializes the JSON to a .NET object.
///
/// The serializer
/// The of object being deserialized.
/// The JSON buffer to deserialize
/// The encoding to use.
/// The deserialized object from the JSON string.
public static T Parse(this IJsonSerializer serializer, ArraySegment jsonBuffer, Encoding encoding)
{
if (serializer == null)
{
throw new ArgumentNullException("serializer");
}
using (var reader = new ArraySegmentTextReader(jsonBuffer, encoding))
{
return (T)serializer.Parse(reader, typeof(T));
}
}
///
/// Serializes the specified object to a JSON string.
///
/// The serializer
/// The object to serailize.
/// A JSON string representation of the object.
public static string Stringify(this IJsonSerializer serializer, object value)
{
if (serializer == null)
{
throw new ArgumentNullException("serializer");
}
using (var writer = new StringWriter(CultureInfo.InvariantCulture))
{
serializer.Serialize(value, writer);
return writer.ToString();
}
}
}
}