SharpLibUtils/TypeConverterJson.cs
author StephaneLenclud
Sun, 24 Jul 2016 20:36:07 +0200
changeset 214 4961ede27e0a
child 222 0e8c6c2f4777
permissions -rw-r--r--
Adding a bunch of CEC actions.
Stephane@212
     1
using System;
Stephane@212
     2
using System.Collections.Generic;
Stephane@212
     3
using System.ComponentModel;
Stephane@212
     4
using System.IO;
Stephane@212
     5
using System.Linq;
Stephane@212
     6
using System.Text;
Stephane@212
     7
using System.Threading.Tasks;
Stephane@212
     8
using System.Runtime.Serialization;
Stephane@212
     9
using System.Runtime.Serialization.Json;
Stephane@212
    10
using System.Globalization;
Stephane@212
    11
Stephane@212
    12
namespace SharpLib.Utils
Stephane@212
    13
{
Stephane@212
    14
    /// <summary>
Stephane@212
    15
    /// Allow serialization into JSON.
Stephane@212
    16
    /// Most useful to be able to save complex settings for instance.
Stephane@212
    17
    /// Usage:
Stephane@212
    18
    /// ...
Stephane@212
    19
    /// [TypeConverter(typeof(TypeConverterJson<PersistantObject>))]
Stephane@212
    20
    /// [DataContract]
Stephane@212
    21
    /// public class PersistantObject
Stephane@212
    22
    /// ...
Stephane@212
    23
    /// </summary>
Stephane@212
    24
    /// <typeparam name="T"></typeparam>
Stephane@212
    25
    public class TypeConverterJson<T> : TypeConverter where T : class
Stephane@212
    26
    {
Stephane@212
    27
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
Stephane@212
    28
        {
Stephane@212
    29
            if (sourceType == typeof(string))
Stephane@212
    30
                return true;
Stephane@212
    31
            else
Stephane@212
    32
                return base.CanConvertFrom(context, sourceType);
Stephane@212
    33
        }
Stephane@212
    34
Stephane@212
    35
        /// <summary>
Stephane@212
    36
        /// 
Stephane@212
    37
        /// </summary>
Stephane@212
    38
        /// <param name="context"></param>
Stephane@212
    39
        /// <param name="culture"></param>
Stephane@212
    40
        /// <param name="value"></param>
Stephane@212
    41
        /// <returns></returns>
Stephane@212
    42
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
Stephane@212
    43
        {
Stephane@212
    44
            string stringValue = value as string;
Stephane@212
    45
            if (stringValue != null)
Stephane@212
    46
            {
Stephane@212
    47
                //Load object form JSON string
Stephane@212
    48
                byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
Stephane@212
    49
                MemoryStream stream = new MemoryStream(byteArray);
Stephane@212
    50
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
Stephane@212
    51
                {
Stephane@212
    52
                    UseSimpleDictionaryFormat = true
Stephane@212
    53
                });
Stephane@212
    54
                T settings = (T)ser.ReadObject(stream);
Stephane@212
    55
                return settings;
Stephane@212
    56
            }
Stephane@212
    57
            else
Stephane@212
    58
                return base.ConvertFrom(context, culture, value);
Stephane@212
    59
        }
Stephane@212
    60
Stephane@212
    61
        /// <summary>
Stephane@212
    62
        /// 
Stephane@212
    63
        /// </summary>
Stephane@212
    64
        /// <param name="context"></param>
Stephane@212
    65
        /// <param name="culture"></param>
Stephane@212
    66
        /// <param name="value"></param>
Stephane@212
    67
        /// <param name="destinationType"></param>
Stephane@212
    68
        /// <returns></returns>
Stephane@212
    69
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
Stephane@212
    70
        {
Stephane@212
    71
            if (destinationType == typeof(string))
Stephane@212
    72
            {
Stephane@212
    73
                //Save settings into JSON string
Stephane@212
    74
                MemoryStream stream = new MemoryStream();
Stephane@212
    75
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
Stephane@212
    76
                {
Stephane@212
    77
                    UseSimpleDictionaryFormat = true
Stephane@212
    78
                });
Stephane@212
    79
                ser.WriteObject(stream, value);
Stephane@212
    80
                // convert stream to string
Stephane@212
    81
                stream.Position = 0;
Stephane@212
    82
                StreamReader reader = new StreamReader(stream);
Stephane@212
    83
                string text = reader.ReadToEnd();
Stephane@212
    84
                return text;
Stephane@212
    85
            }
Stephane@212
    86
            else
Stephane@212
    87
            {
Stephane@212
    88
                return base.ConvertTo(context,culture,value,destinationType);
Stephane@212
    89
            }
Stephane@212
    90
        }
Stephane@212
    91
    }
Stephane@212
    92
}