locked
Issues with JSON Deserialize RRS feed

  • Question

  • Hello,

     

    I have been trying to find a solution to a problem I'm running into and can't seem to figure it out. I have a application I'm writing to convert from one software to a new one. Well because doing the setup and conversion can take some time, I decided to writing in a option to for saving your work and then picking up where you left off. I figured the easiest way to do this would be Serialize the class as a JSON string and save it to a file. While I've been able to do that without any problems. I'm having a issue deserializing the string and putting it back into the class. Here is the error I'm getting.

    An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.ServiceModel.Web.dll

    Additional information: The data contract type 'HyperionConvertion.ConvertSettings' cannot be deserialized because the required data members 'EmpStart, from_to_subpackages, helios_memberships, helios_to_hyperion_locations, helios_to_hyperion_mems, helios_user_defined, hyperion_eft_memberships, hyperion_locations, hyperion_memberships, hyperion_product_cat, hyperion_product_types, hyperion_products, hyperion_subpackages' were not found.

    Here is my Class ConvertSettings
    namespace Convertion
    { [Serializable]
    public class ConvertSettings
    {
    public int EmpStart = 1;
    public Dictionary<string, string> hyperion_memberships = new Dictionary<string, string>();
    public Dictionary<string, string> hyperion_subpackages = new Dictionary<string, string>();
    public List<string> hyperion_eft_memberships = new List<string>();
    public Dictionary<string, string> hyperion_products = new Dictionary<string, string>();
    public Dictionary<string, string> hyperion_product_types = new Dictionary<string, string>();
    public Dictionary<string, string> hyperion_product_cat = new Dictionary<string, string>();
    public Dictionary<string, string> hyperion_locations = new Dictionary<string, string>();

    public Dictionary<int, string> helios_memberships = new Dictionary<int, string>();

    public Dictionary<string, string> helios_to_hyperion_locations = new Dictionary<string, string>();
    public Dictionary<string, string> helios_to_hyperion_mems = new Dictionary<string, string>();
    public Dictionary<string, string> helios_user_defined = new Dictionary<string, string>();
    public Dictionary<string, string> from_to_subpackages = new Dictionary<string, string>();
    }
    }

    Here is my code that I'm using to convert the class to a json string

     

    public ConvertSettings settings = new ConvertSettings();

    //............... the code else where fills in the values

    private void SaveSettings()
    {
    DataContractJsonSerializer ser = new DataContractJsonSerializer(settings.GetType());

    using (MemoryStream ms = new MemoryStream())
    {
    ser.WriteObject(ms, settings);
    json = Encoding.Default.GetString(ms.ToArray());
    }

    StreamWriter sw = File.CreateText(Application.StartupPath + "\\json.txt");
    sw.WriteLine(json);
    sw.Close();
    }

    to get it back I'm doing this

     

    public ConvertSettings settings = new ConvertSettings();

    // ...........

    private void ReadSettings()
    {
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ConvertSettings));
    using (FileStream fs = File.OpenRead(Application.StartupPath + "\\json.txt"))
    {
    settings = ser.ReadObject(fs) as ConvertSettings;
    }
    }

    Whats going wrong? I know i can store the information via a binary file, but I'm trying to avoid doing that because there is also going to be a
    linux based version of this program, and I want the saved files to be interchangeable.

    • Moved by Harry Zhu Monday, March 29, 2010 3:12 AM (From:Visual C# General)
    Wednesday, March 24, 2010 5:45 PM

All replies