Monday, November 1, 2010

App Setting Json serialization


public class Inclusion
{
public string ComponentTypeFullName { get; set; }
public string EventName { get; set; }
public string[] SenderPropertyNames { get; set; }
public string[] EventArgsPropertyNames { get; set; }
}

static List li = new List()
{
new Inclusion(){
ComponentTypeFullName="Infragistics.Win.Misc.UltraButton",
EventName="Click",
SenderPropertyNames=new string[]{null},
EventArgsPropertyNames=new string[]{null}},
new Inclusion() {
ComponentTypeFullName="Infragistics.Win.UltraWinEditors.UltraComboEditor",
EventName="SelectionChanged",
SenderPropertyNames=new string[]{"Text"},
EventArgsPropertyNames=new string[]{null}},
new Inclusion() {
ComponentTypeFullName="Infragistics.Win.UltraWinToolbars.UltraToolbarsManager",
EventName="ToolClick",
SenderPropertyNames=new string[]{null},
EventArgsPropertyNames=new string[]{"Tool.Key"}},

};

//Read
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List));
string cd = Settings.Default.Inclusion;
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(cd));

List li2 =(List) ser.ReadObject(ms);
ms.Close();

//write to see the format
Type t = li.GetType();
DataContractJsonSerializer ser2 = new DataContractJsonSerializer(t);
FileStream fs = new FileStream(@"c:\3.txt", FileMode.CreateNew);
ser.WriteObject(fs, li);
fs.Flush();
fs.Close();

No comments: