Stephane@212: using System;
Stephane@212: using System.Collections.Generic;
Stephane@212: using System.ComponentModel;
Stephane@212: using System.IO;
Stephane@212: using System.Linq;
Stephane@212: using System.Text;
Stephane@212: using System.Threading.Tasks;
Stephane@212: using System.Runtime.Serialization;
Stephane@212: using System.Runtime.Serialization.Json;
Stephane@212: using System.Globalization;
Stephane@212:
Stephane@212: namespace SharpLib.Utils
Stephane@212: {
Stephane@212: ///
Stephane@212: /// Allow serialization into JSON.
Stephane@212: /// Most useful to be able to save complex settings for instance.
Stephane@212: /// Usage:
Stephane@212: /// ...
Stephane@212: /// [TypeConverter(typeof(TypeConverterJson))]
Stephane@212: /// [DataContract]
Stephane@212: /// public class PersistantObject
Stephane@212: /// ...
Stephane@212: ///
Stephane@212: ///
Stephane@212: public class TypeConverterJson : TypeConverter where T : class
Stephane@212: {
Stephane@212: public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
Stephane@212: {
Stephane@212: if (sourceType == typeof(string))
Stephane@212: return true;
Stephane@212: else
Stephane@212: return base.CanConvertFrom(context, sourceType);
Stephane@212: }
Stephane@212:
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
Stephane@212: {
Stephane@212: string stringValue = value as string;
Stephane@212: if (stringValue != null)
Stephane@212: {
Stephane@212: //Load object form JSON string
Stephane@212: byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
Stephane@212: MemoryStream stream = new MemoryStream(byteArray);
Stephane@212: DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
Stephane@212: {
Stephane@212: UseSimpleDictionaryFormat = true
Stephane@212: });
Stephane@212: T settings = (T)ser.ReadObject(stream);
Stephane@212: return settings;
Stephane@212: }
Stephane@212: else
Stephane@212: return base.ConvertFrom(context, culture, value);
Stephane@212: }
Stephane@212:
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: ///
Stephane@212: public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
Stephane@212: {
Stephane@212: if (destinationType == typeof(string))
Stephane@212: {
Stephane@212: //Save settings into JSON string
Stephane@212: MemoryStream stream = new MemoryStream();
Stephane@212: DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
Stephane@212: {
Stephane@212: UseSimpleDictionaryFormat = true
Stephane@212: });
Stephane@212: ser.WriteObject(stream, value);
Stephane@212: // convert stream to string
Stephane@212: stream.Position = 0;
Stephane@212: StreamReader reader = new StreamReader(stream);
Stephane@212: string text = reader.ReadToEnd();
Stephane@212: return text;
Stephane@212: }
Stephane@212: else
Stephane@212: {
Stephane@212: return base.ConvertTo(context,culture,value,destinationType);
Stephane@212: }
Stephane@212: }
Stephane@212: }
Stephane@212: }