view Source/Mapping/MemberMappers/JSONSerialisationMapper.cs @ 9:1e85f66cf767 default tip

update bltoolkit
author nickolay
date Thu, 05 Apr 2018 20:53:26 +0300
parents f990fcb411a9
children
line wrap: on
line source

using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace BLToolkit.Mapping.MemberMappers
{
	public class JSONSerialisationMapper : MemberMapper
	{
		public override void SetValue(object o, object value)
		{
			if (value != null) this.MemberAccessor.SetValue(o, Deserialize(value.ToString()));
		}

        public override object GetValue(object o)
        {
            return this.serialize(this.MemberAccessor.GetValue(o));
        }

        private string serialize(object obj)
        {
            if (obj == null) return null;

			DataContractJsonSerializer ser = new DataContractJsonSerializer(this.Type);
			MemoryStream ms = new MemoryStream();
			ser.WriteObject(ms, obj);
			string jsonString = Encoding.UTF8.GetString(ms.ToArray());
			ms.Close();
			return jsonString;   
		}

		object Deserialize(string txt)
		{
			object retVal = null;
			if (string.IsNullOrEmpty(txt)) return null;

			try
			{
				DataContractJsonSerializer ser = new DataContractJsonSerializer(this.Type);
				MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(txt));
				retVal = ser.ReadObject(ms);                
			}
			catch (Exception)
			{
			}
			return retVal;
		}
	}
}