c# - Reversing the save method in the Open method -


fairly new programming. can't wrap head around how work in reverse. have save method , open method. save:

idictionary<string, idictionary<string, object>> pluginstates = new dictionary<string, idictionary<string, object>>(); signaller.raisesaverequest(pluginstates); <--goes out , gets packed plugins  //loop through plugins values , types //holds of plugin arrays dictionary<string, object> dictprojectstate = new dictionary<string, object>();  foreach (keyvaluepair<string,idictionary<string,object>> plugin in pluginstates) {      //holds jsonrepresented values     dictionary<string, object> dictjsonrep = new dictionary<string, object>();      //holds object types     dictionary<string, object> dictobjrep = new dictionary<string, object>();      object[] arraydictholder = new object[2];  //holds of dictionaries     string pluginkey = plugin.key;     idictionary<string, object> pluginvalue = new dictionary<string, object>();      pluginvalue = plugin.value;      foreach (keyvaluepair<string, object> element in pluginvalue)     {         string jsonrepresentation = jsonconvert.serializeobject(element);         object objtype = element.value.gettype().tostring();         dictjsonrep.add(element.key, jsonrepresentation);         dictobjrep.add(element.key, objtype);     }     arraydictholder[0] = dictjsonrep;     arraydictholder[1] = dictobjrep;     dictprojectstate.add(pluginkey, arraydictholder); }  jsonserializer serializer = new jsonserializer(); using (streamwriter sw = new streamwriter(strpathname)) using (jsonwriter writer = new jsontextwriter(sw)) {      serializer.serialize(writer, dictprojectstate); } 

so, when saves, event handler goes out , gets packedstate of each plugin, adds dictionary pluginstates. go through each plugin in pluginstates, adding key, , json string version of value 1 dictionary , key, object type dictionary, add 2 dictionaries array , pack dictionary contains pluginkey , array each plugin. reasoning: when deserializing, i'm hitting problems going jarray type datatable , other types within dictionary gets passed plugin unpack upon opening. i'm trying figure out how reverse this, when user opens project, have dictprojectstate , need bring way through code end 1 dictionary containing plugins. how mirror save in open?? thanks!

i believe when deserialize in open function, should object can cast dictionary<string, object>.

the keys of dictionary names of plugins in final dictioary (pluginkey in yoru save method). values should object[] arraydictholder objects. can cast them object[] , @ 2 values inside (dictjsonrep , dictobjrep in save function).

once values both cast dictionary<string, object>(), can iterate through keyvaluepairs in 1 of them, , use key pairs @ value store in other dictionary.

code similar following:

var pluginstates = new dictionary<string, idictionary<string, object>>();  var dictprojectstate = jsonserializer.deserialize(reader) dictionary<string, object>;  foreach(var projectstatepair in dictprojectstate) {     string pluginkey = projectstatepair.key;     var pluginvalues = new dictionary<string, object>();     object[] arraydictholder = projectstatepair.value object[];     var dictjsonrep = arraydictholder[0] dictionary<string, object>;     var dictobjrep =  arraydictholder[1] dictionary<string, object>;     foreach(var jsonreppair in dictjsonrep)     {         string jsonrepresentation = jsonreppair.value;         // know both dictionaries have same keys,         // objtype jsonrepresentation         object objtype = dictobjrep[jsonreppair.key];         // since you're serializing objtype string, you'll need         // actual type before calling deserializeobject.         type jsontype = system.type.gettype(objtype string);         object value = jsonconvert.deserializeobject( jsonrepresentation, jsontype );         pluginvalues.add( jsonreppair.key );     }     pluginstates.add( pluginkey, pluginvalues ); } 

note: setting objtype element.value.gettype().tostring(); in save function. either need update save() set element.value.gettype(); or convert type strings actual types before trying deserialize.

note: not familiar json libraries. above code focused on creating pluginstates dictionary out of deserialized results. need handle creation of jsonserializer , make sure close streams after you've deserialized them. may need tweak actual deserialization calls desired results.

update: added conversion of objtype actual system.type object.


Comments

Popular posts from this blog

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

c++ - Troubles when compiling phash program -