Json.net反序列化包含DataTable值的字典中包含字典。

3

我对编程还比较新(除了课堂环境之外)。

我正在开发一个具有插件的应用程序。每个插件都会将其状态(作为字典)打包/解包并发送给包含所有打包状态的字典。我使用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的文档,但我几乎不理解如何在我的代码中使用它。我想我需要在插件获取其打包状态并解包后进行转换或解析。当它被打包时,插件的第一个字典条目被保存为DataTable。然后,在Unpack中,实际上是带有:分隔符的值中的实际dataTable。
我可以提供屏幕截图,但我还没有弄清楚如何做到这一点。你有什么想法吗?谢谢!

问题在于DataTable以一种特殊的方式进行序列化,它有自己内置的转换器将其转换为数组(这意味着它不包含类型信息)。我不确定如何解决这个问题。 - svick
1个回答

0

当一个项目被保存时,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);

希望这能帮助到任何正在寻找答案的人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接