SharpLibUtils/TypeConverterJson.cs
author StephaneLenclud
Tue, 30 Aug 2016 21:14:18 +0200
changeset 261 e2729a990e8b
parent 222 0e8c6c2f4777
permissions -rw-r--r--
Published v1.2.1.0
Support for Event Trigger through SharpLibDisplay.
     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         /// Internalize
    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                 //try
    48                 //{
    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()
    54                         {
    55                             UseSimpleDictionaryFormat = true
    56                         });
    57                     T settings = (T)ser.ReadObject(stream);
    58                     return settings;
    59                 //}
    60                 //catch (Exception ex)
    61                 //{
    62                     //That's not helping with partial loading
    63                 //    Trace.WriteLine("WARNING: Internalize exception -" + ex.ToString() );
    64                 //    return null;
    65                 //}
    66             }
    67             else
    68             {
    69                 return base.ConvertFrom(context, culture, value);
    70             }
    71                 
    72         }
    73 
    74         /// <summary>
    75         /// Externalize
    76         /// </summary>
    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)
    83         {
    84             if (destinationType == typeof(string))
    85             {
    86                 //Save settings into JSON string
    87                 MemoryStream stream = new MemoryStream();
    88                 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
    89                 {
    90                     UseSimpleDictionaryFormat = true
    91                 });
    92                 ser.WriteObject(stream, value);
    93                 // convert stream to string
    94                 stream.Position = 0;
    95                 StreamReader reader = new StreamReader(stream);
    96                 string text = reader.ReadToEnd();
    97                 return text;
    98             }
    99             else
   100             {
   101                 return base.ConvertTo(context,culture,value,destinationType);
   102             }
   103         }
   104     }
   105 }