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