using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Json;

namespace AjaxWebServicesDemo
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CustomSerializerService
    {
        [OperationContract]
        public BusinessEntity Save(string entity)
        {
            byte[] stream = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(entity);
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(stream);
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(BusinessEntity));
            BusinessEntity businessEntity = (BusinessEntity)jsonSerializer.ReadObject(memoryStream);
            businessEntity.Id = Guid.NewGuid().ToString();
            return businessEntity;
        }

    }
}
