我是编程新手(在课堂环境之外)。
我正在开发一个有插件的应用程序。每个插件打包/解包它的状态(作为字典),并将其自身发送到包含所有打包状态的字典中。我使用Json.Net来序列化/反序列化每个插件,并将每个插件传递给它自己的类进行打包/解包,这取决于项目是被保存还是被打开。
我遇到的问题是,当我的第一个插件将其打包状态的字典版本解包时,我开始重新填充每个属性,第一个属性(字典中的第一项)是一个DataTable。我收到一个错误,说:
Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to
type 'System.Data.DataTable'.下面是我的序列化代码。
IDictionary<string, IDictionary<string, object>> pluginStates =
new Dictionary<string, IDictionary<string, object>>();
signaller.RaiseSaveRequest(pluginStates); //gets all of the plugins' packedState
JsonSerializer serializer = new JsonSerializer();
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
serializer.TypeNameHandling = TypeNameHandling.All;
using (StreamWriter sw = new StreamWriter(strPathName))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, pluginStates);
}和反序列化。
IDictionary<string, IDictionary<string, object>> pluginStates =
new Dictionary<string, IDictionary<string, object>>();
JsonSerializer serializer = new JsonSerializer();
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
serializer.TypeNameHandling = TypeNameHandling.All;
StreamReader sr = new StreamReader(fullName);
JsonTextReader reader = new JsonTextReader(sr);
string json = sr.ReadToEnd();
pluginStates = serializer.Deserialize<IDictionary<string, IDictionary<string, Object>>>(reader);
pluginStates = JsonConvert.DeserializeObject<IDictionary<string, IDictionary<string, Object>>>(json);
sr.Close();
reader.Close();
signaller.UnpackProjectState(pluginStates);我试着查看了NewtonSoft.Json上的文档,但我几乎不理解它以及如何在我的代码中使其工作。我在想,一旦插件的打包状态需要解包,我就需要对Converter或解析做一些处理。打包后,插件的第一个字典条目被保存为DataTable。然后,在解包时,它是带有:分隔符的值中的实际dataTable。
我会提供屏幕截图,但我还没有想好怎么做。你知道我错过了什么吗?谢谢!
发布于 2012-06-16 03:30:41
保存项目时,ProjMngr将获取打包的pluginStates字典。我循环遍历pluginStates字典中的每个字典(插件),并创建一个包含键、值( json字符串版本)和键、值(.net类型)的字典。将这些元素添加到一个数组中,并将该数组放入序列化的最终projectState字典中。这是代码。
signaller.RaiseSaveRequest(pluginStates); <----has all plugin's packedState
//loop through plugins to get values and types
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 all of the 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.Value);
object objType = element.Value.GetType();
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);
}然后,当一个项目打开时,我反过来做了同样的事情,让pluginState回到它的原始状态,发送给每个插件。在发送前使用了类型以确保类型正确。我意识到我必须反序列化几次,这是令人惊讶的。我就是离不开Jarray或Jobject。但这是可行的。这就是我的开场白。
IDictionary<string, IDictionary<string, object>> pluginStates = new Dictionary<string, IDictionary<string, object>>();
StreamReader sr = new StreamReader(fullName);
JsonTextReader reader = new JsonTextReader(sr);
string json = sr.ReadToEnd();
var dictProjectState = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
sr.Close();
reader.Close();
foreach (var projectStatePair in dictProjectState)
{
string pluginKey = projectStatePair.Key; //key in pluginStates dict
Dictionary<string, object> dictJsonRep = new Dictionary<string, object>();
Dictionary<string, object> dictObjRep = new Dictionary<string, object>();
Dictionary<string, object> pluginValues = new Dictionary<string, object>();
string stpluginValue = projectStatePair.Value.ToString();
Newtonsoft.Json.Linq.JArray ja = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(stpluginValue);
object[] arrayHolder = ja.ToObject<object[]>();
string strArray0 = arrayHolder[0].ToString();
string strArray1 = arrayHolder[1].ToString();
dictJsonRep = JsonConvert.DeserializeObject<Dictionary<string, object>>(strArray0);
dictObjRep = JsonConvert.DeserializeObject<Dictionary<string, object>>(strArray1);
foreach (var pair in dictJsonRep)
{
string strVariableKey = pair.Key;
object objType = dictObjRep[pair.Key];
string jsonRep = (string)pair.Value;
Type jsonType = Type.GetType(objType as string);
object jsonReprValue = null;
jsonReprValue = JsonConvert.DeserializeObject(jsonRep, jsonType);
pluginValues.Add(strVariableKey, jsonReprValue);
}
pluginStates.Add(pluginKey, pluginValues);
}
signaller.UnpackProjectState(pluginStates);希望这能帮助任何寻找答案的人。
https://stackoverflow.com/questions/10953521
复制相似问题