3

I am fairly new to programming (outside of a classroom environment).

I'm working on an application that has plugins. Each plugin packs/unpacks it's state (as a dictionary) and sends itself to be added to a dictionary containing all packed states. I'm using Json.Net to serialize/deserialize and pass off each plugin to it's own class to be packed/unpacked depending on if the project is being saved or opened.

The problem I'm having is when my first plugin gets its dictionary version of it's packed state to unpack, and I start going through the repopulating of each property, the first property (first item in the dictionary) is a DataTable. I'm getting an error saying:

Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to 
type 'System.Data.DataTable'.

Here is my code for serializing.

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);
}

and deserializing.

 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);

I've tried looking at the documentation on NewtonSoft.Json, but I barely understand it and how to make it work in my code. I'm thinking I need to do something with the Converters or Parsing once the plugin gets its packed state to unpack. When it was packed up, the plugin's 1st dictionary entry was saved as DataTable. Then, when in the Unpack, it's the actual dataTable in the value with the : separators.

I would provide screen shots, but I haven't figured out how to do that. Any ideas what I'm missing?? THANKS!

1
  • The problem is that DataTable is serialized in a special way, it has its own built-in converter that converts it to an array (which means it doesn't contain type information). I'm not sure how to fix this.
    – svick
    Jun 9, 2012 at 10:20

1 Answer 1

0

When a project is saved, the ProjMngr gets the packed pluginStates dictionary. I loop through each dictionary (plugin) within the pluginStates dictionary and create a dictionary holding the key, value(the json string version) and key, value(.net type). Add these to an array and put the array in the final projectState dictionary that gets serialized. Here's the code.

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);
}

Then when a project is opened, I did the same thing in reverse to get the pluginState back in it's original state to send off to each plugin. Used the type to make sure the type was right before sending. Realized I had to deserialize several times, which was surprising. I just couldn't get away from the Jarray or Jobject. But this works. Here's how I opened.

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);

Hopes this helps anyone looking for an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.