using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Runtime.Serialization.Json;

namespace wijmoSample1.Models.FileExplorer
{
	public class DataContractJsonResult<T> : System.Web.Mvc.ActionResult where T : class
	{
		public DataContractJsonResult(T data, IEnumerable<Type> knownTypes)
		{
			Data = data;
			KnownTypes = knownTypes;
		}

		public IEnumerable<Type> KnownTypes { get; set; }

		public T Data { get; set; }

		public override void ExecuteResult(ControllerContext context)
		{
			if (Data == null)
				return;

			var serializer = new DataContractJsonSerializer(typeof (T), KnownTypes);
			var response = context.HttpContext.Response;
			serializer.WriteObject(response.OutputStream, Data);
		}
	}
}