Adding user given name to EAR events and enabling User Events.
2 using System.Collections.Generic;
3 using System.ComponentModel;
7 using System.Threading.Tasks;
8 using System.Runtime.Serialization;
9 using System.Runtime.Serialization.Json;
10 using System.Globalization;
12 namespace SharpLib.Utils
15 /// Allow serialization into JSON.
16 /// Most useful to be able to save complex settings for instance.
19 /// [TypeConverter(typeof(TypeConverterJson<PersistantObject>))]
21 /// public class PersistantObject
24 /// <typeparam name="T"></typeparam>
25 public class TypeConverterJson<T> : TypeConverter where T : class
27 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
29 if (sourceType == typeof(string))
32 return base.CanConvertFrom(context, sourceType);
38 /// <param name="context"></param>
39 /// <param name="culture"></param>
40 /// <param name="value"></param>
41 /// <returns></returns>
42 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
44 string stringValue = value as string;
45 if (stringValue != null)
49 //Load object form JSON string
50 byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
51 MemoryStream stream = new MemoryStream(byteArray);
52 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T),
53 new DataContractJsonSerializerSettings()
55 UseSimpleDictionaryFormat = true
57 T settings = (T)ser.ReadObject(stream);
60 //catch (Exception ex)
62 //That's not helping with partial loading
63 // Trace.WriteLine("WARNING: Internalize exception -" + ex.ToString() );
69 return base.ConvertFrom(context, culture, value);
77 /// <param name="context"></param>
78 /// <param name="culture"></param>
79 /// <param name="value"></param>
80 /// <param name="destinationType"></param>
81 /// <returns></returns>
82 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
84 if (destinationType == typeof(string))
86 //Save settings into JSON string
87 MemoryStream stream = new MemoryStream();
88 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
90 UseSimpleDictionaryFormat = true
92 ser.WriteObject(stream, value);
93 // convert stream to string
95 StreamReader reader = new StreamReader(stream);
96 string text = reader.ReadToEnd();
101 return base.ConvertTo(context,culture,value,destinationType);