SharpLibUtils/TypeConverterJson.cs
author StephaneLenclud
Wed, 31 Aug 2016 16:06:47 +0200
changeset 262 c4749a27966d
parent 222 0e8c6c2f4777
permissions -rw-r--r--
Fix crash when trying to select Harmony command without configuration.
Consolidate Named event Trigger.
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>
StephaneLenclud@222
    36
        /// Internalize
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
            {
StephaneLenclud@222
    47
                //try
StephaneLenclud@222
    48
                //{
StephaneLenclud@222
    49
                    //Load object form JSON string
StephaneLenclud@222
    50
                    byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
StephaneLenclud@222
    51
                    MemoryStream stream = new MemoryStream(byteArray);
StephaneLenclud@222
    52
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T),
StephaneLenclud@222
    53
                        new DataContractJsonSerializerSettings()
StephaneLenclud@222
    54
                        {
StephaneLenclud@222
    55
                            UseSimpleDictionaryFormat = true
StephaneLenclud@222
    56
                        });
StephaneLenclud@222
    57
                    T settings = (T)ser.ReadObject(stream);
StephaneLenclud@222
    58
                    return settings;
StephaneLenclud@222
    59
                //}
StephaneLenclud@222
    60
                //catch (Exception ex)
StephaneLenclud@222
    61
                //{
StephaneLenclud@222
    62
                    //That's not helping with partial loading
StephaneLenclud@253
    63
                //    Trace.WriteLine("WARNING: Internalize exception -" + ex.ToString() );
StephaneLenclud@222
    64
                //    return null;
StephaneLenclud@222
    65
                //}
Stephane@212
    66
            }
Stephane@212
    67
            else
StephaneLenclud@222
    68
            {
Stephane@212
    69
                return base.ConvertFrom(context, culture, value);
StephaneLenclud@222
    70
            }
StephaneLenclud@222
    71
                
Stephane@212
    72
        }
Stephane@212
    73
Stephane@212
    74
        /// <summary>
StephaneLenclud@222
    75
        /// Externalize
Stephane@212
    76
        /// </summary>
Stephane@212
    77
        /// <param name="context"></param>
Stephane@212
    78
        /// <param name="culture"></param>
Stephane@212
    79
        /// <param name="value"></param>
Stephane@212
    80
        /// <param name="destinationType"></param>
Stephane@212
    81
        /// <returns></returns>
Stephane@212
    82
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
Stephane@212
    83
        {
Stephane@212
    84
            if (destinationType == typeof(string))
Stephane@212
    85
            {
Stephane@212
    86
                //Save settings into JSON string
Stephane@212
    87
                MemoryStream stream = new MemoryStream();
Stephane@212
    88
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
Stephane@212
    89
                {
Stephane@212
    90
                    UseSimpleDictionaryFormat = true
Stephane@212
    91
                });
Stephane@212
    92
                ser.WriteObject(stream, value);
Stephane@212
    93
                // convert stream to string
Stephane@212
    94
                stream.Position = 0;
Stephane@212
    95
                StreamReader reader = new StreamReader(stream);
Stephane@212
    96
                string text = reader.ReadToEnd();
Stephane@212
    97
                return text;
Stephane@212
    98
            }
Stephane@212
    99
            else
Stephane@212
   100
            {
Stephane@212
   101
                return base.ConvertTo(context,culture,value,destinationType);
Stephane@212
   102
            }
Stephane@212
   103
        }
Stephane@212
   104
    }
Stephane@212
   105
}