Link to home
Start Free TrialLog in
Avatar of Pyromanci
PyromanciFlag for United States of America

asked on

Issues with Deserialize JSON in C#

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;

    }

}

Know when the file is written and i go and look at it this is what's in there.
{"EmpStart":740,"helios_to_hyperion_locations":[],"helios_to_hyperion_mems":[],"hyperion_eft_memberships":["1","2","3","4","5","6"],"hyperion_locations":[{"Key":"Global Settings","Value":"-1"},{"Key":"WSC","Value":"1"},{"Key":"OVC","Value":"2"},{"Key":"SWL","Value":"3"},{"Key":"LMP","Value":"4"},{"Key":"LSL","Value":"5"},{"Key":"KPC","Value":"6"},{"Key":"HPC","Value":"7"},{"Key":"CML","Value":"8"},{"Key":"Corp","Value":"9"},{"Key":"CCL","Value":"10"},{"Key":"CCM","Value":"11"},{"Key":"BCM","Value":"12"},{"Key":"NML","Value":"13"},{"Key":"AAL","Value":"14"},{"Key":"GHL","Value":"15"},{"Key":"RPM","Value":"16"},{"Key":"017","Value":"17"},{"Key":"018","Value":"18"}],"hyperion_memberships":[{"Key":"NONE","Value":"-1"},{"Key":"Employee","Value":"-2"},{"Key":"All Access","Value":"1"},{"Key":"Instant Plus","Value":"2"},{"Key":"Fastest","Value":"3"},{"Key":"Faster","Value":"4"},{"Key":"Fast","Value":"5"},{"Key":"Fast 9.99","Value":"6"},{"Key":"EZ1 All Access","Value":"7"},{"Key":"EZ1 Instant Plus","Value":"8"},{"Key":"EZ1 Fastest","Value":"9"},{"Key":"EZ1 Faster","Value":"10"},{"Key":"EZ1 Fast","Value":"11"},{"Key":"1M All Access","Value":"12"},{"Key":"1M Instant Plus","Value":"13"},{"Key":"1M Fastest","Value":"14"},{"Key":"1M Faster","Value":"15"},{"Key":"1M Fast","Value":"16"},{"Key":"1W Instant Plus","Value":"17"},{"Key":"1W Fastest","Value":"18"},{"Key":"1W Faster","Value":"19"},{"Key":"1W Fast","Value":"20"}],"hyperion_product_cat":[{"Key":"Instant Plus","Value":"1"},{"Key":"Instant","Value":"2"},{"Key":"Fastest","Value":"3"},{"Key":"Faster","Value":"4"},{"Key":"Fast","Value":"5"},{"Key":"Misc","Value":"6"},{"Key":"Packette","Value":"7"}],"hyperion_product_types":[{"Key":"Tanning Lotion","Value":"1"},{"Key":"Facial Lotion","Value":"2"},{"Key":"Extender Lotion","Value":"3"},{"Key":"Extender Wash","Value":"4"},{"Key":"Leg Lotion","Value":"5"},{"Key":"Sunless Product","Value":"6"},{"Key":"Lip Product","Value":"7"},{"Key":"Eye Protection","Value":"8"},{"Key":"Outdoor Lotion","Value":"9"},{"Key":"Burn Relief","Value":"10"},{"Key":"Misc","Value":"11"},{"Key":"Tanning Lotion Packette","Value":"16"},{"Key":"Sunless Packette","Value":"17"},{"Key":"Facial Lotion Packette","Value":"18"},{"Key":"Beauty Product","Value":"19"},{"Key":"Tanning Accessory","Value":"20"}],"hyperion_products":[{"Key":"Product 1 (IP - Bottle)","Value":"1"},{"Key":"Product 2 (FT - Bottle)","Value":"3"}],"hyperion_subpackages":[{"Key":"Employee","Value":"-2"},{"Key":"Walk in - NONE","Value":"-1"},{"Key":"CREDITS","Value":"1"},{"Key":"MEM CREDITS","Value":"2"},{"Key":"FAST SESSIONS","Value":"5"}]}

after formatting it to make it readable it is a valid JSON string. I can even make PHP, Javascript and Java read it without any problems.

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.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Hello Pyromanci,

Could you post the json.txt file ?

Regards.
Avatar of Pyromanci

ASKER

It is. It's right there at the bottom

Or here:

{"EmpStart":740,"helios_to_hyperion_locations":[],"helios_to_hyperion_mems":[],"hyperion_eft_memberships":["1","2","3","4","5","6"],"hyperion_locations":[{"Key":"Global Settings","Value":"-1"},{"Key":"WSC","Value":"1"},{"Key":"OVC","Value":"2"},{"Key":"SWL","Value":"3"},{"Key":"LMP","Value":"4"},{"Key":"LSL","Value":"5"},{"Key":"KPC","Value":"6"},{"Key":"HPC","Value":"7"},{"Key":"CML","Value":"8"},{"Key":"Corp","Value":"9"},{"Key":"CCL","Value":"10"},{"Key":"CCM","Value":"11"},{"Key":"BCM","Value":"12"},{"Key":"NML","Value":"13"},{"Key":"AAL","Value":"14"},{"Key":"GHL","Value":"15"},{"Key":"RPM","Value":"16"},{"Key":"017","Value":"17"},{"Key":"018","Value":"18"}],"hyperion_memberships":[{"Key":"NONE","Value":"-1"},{"Key":"Employee","Value":"-2"},{"Key":"All Access","Value":"1"},{"Key":"Instant Plus","Value":"2"},{"Key":"Fastest","Value":"3"},{"Key":"Faster","Value":"4"},{"Key":"Fast","Value":"5"},{"Key":"Fast 9.99","Value":"6"},{"Key":"EZ1 All Access","Value":"7"},{"Key":"EZ1 Instant Plus","Value":"8"},{"Key":"EZ1 Fastest","Value":"9"},{"Key":"EZ1 Faster","Value":"10"},{"Key":"EZ1 Fast","Value":"11"},{"Key":"1M All Access","Value":"12"},{"Key":"1M Instant Plus","Value":"13"},{"Key":"1M Fastest","Value":"14"},{"Key":"1M Faster","Value":"15"},{"Key":"1M Fast","Value":"16"},{"Key":"1W Instant Plus","Value":"17"},{"Key":"1W Fastest","Value":"18"},{"Key":"1W Faster","Value":"19"},{"Key":"1W Fast","Value":"20"}],"hyperion_product_cat":[{"Key":"Instant Plus","Value":"1"},{"Key":"Instant","Value":"2"},{"Key":"Fastest","Value":"3"},{"Key":"Faster","Value":"4"},{"Key":"Fast","Value":"5"},{"Key":"Misc","Value":"6"},{"Key":"Packette","Value":"7"}],"hyperion_product_types":[{"Key":"Tanning Lotion","Value":"1"},{"Key":"Facial Lotion","Value":"2"},{"Key":"Extender Lotion","Value":"3"},{"Key":"Extender Wash","Value":"4"},{"Key":"Leg Lotion","Value":"5"},{"Key":"Sunless  Product","Value":"6"},{"Key":"Lip Product","Value":"7"},{"Key":"Eye Protection","Value":"8"},{"Key":"Outdoor Lotion","Value":"9"},{"Key":"Burn Relief","Value":"10"},{"Key":"Misc","Value":"11"},{"Key":"Tanning Lotion Packette","Value":"16"},{"Key":"Sunless Packette","Value":"17"},{"Key":"Facial Lotion Packette","Value":"18"},{"Key":"Beauty Product","Value":"19"},{"Key":"Tanning Accessory","Value":"20"}],"hyperion_products":[{"Key":"Product 1 (IP - Bottle)","Value":"1"},{"Key":"Product 2 (FT - Bottle)","Value":"3"}],"hyperion_subpackages":[{"Key":"Employee","Value":"-2"},{"Key":"Walk in - NONE","Value":"-1"},{"Key":"CREDITS","Value":"1"},{"Key":"MEM CREDITS","Value":"2"},{"Key":"FAST SESSIONS","Value":"5"}]}
Try :

adding
{"myRoot" :
at the beggining

and
}
at the end
{"myRoot":{"EmpStart":740,"helios_to_hyperion_locations":[],"helios_to_hyperion_mems":[],"hyperion_eft_memberships":["1","2","3","4","5","6"],"hyperion_locations":[{"Key":"Global Settings","Value":"-1"},{"Key":"WSC","Value":"1"},{"Key":"OVC","Value":"2"},{"Key":"SWL","Value":"3"},{"Key":"LMP","Value":"4"},{"Key":"LSL","Value":"5"},{"Key":"KPC","Value":"6"},{"Key":"HPC","Value":"7"},{"Key":"CML","Value":"8"},{"Key":"Corp","Value":"9"},{"Key":"CCL","Value":"10"},{"Key":"CCM","Value":"11"},{"Key":"BCM","Value":"12"},{"Key":"NML","Value":"13"},{"Key":"AAL","Value":"14"},{"Key":"GHL","Value":"15"},{"Key":"RPM","Value":"16"},{"Key":"017","Value":"17"},{"Key":"018","Value":"18"}],"hyperion_memberships":[{"Key":"NONE","Value":"-1"},{"Key":"Employee","Value":"-2"},{"Key":"All Access","Value":"1"},{"Key":"Instant Plus","Value":"2"},{"Key":"Fastest","Value":"3"},{"Key":"Faster","Value":"4"},{"Key":"Fast","Value":"5"},{"Key":"Fast 9.99","Value":"6"},{"Key":"EZ1 All Access","Value":"7"},{"Key":"EZ1 Instant Plus","Value":"8"},{"Key":"EZ1 Fastest","Value":"9"},{"Key":"EZ1 Faster","Value":"10"},{"Key":"EZ1 Fast","Value":"11"},{"Key":"1M All Access","Value":"12"},{"Key":"1M Instant Plus","Value":"13"},{"Key":"1M Fastest","Value":"14"},{"Key":"1M Faster","Value":"15"},{"Key":"1M Fast","Value":"16"},{"Key":"1W Instant Plus","Value":"17"},{"Key":"1W Fastest","Value":"18"},{"Key":"1W Faster","Value":"19"},{"Key":"1W Fast","Value":"20"}],"hyperion_product_cat":[{"Key":"Instant Plus","Value":"1"},{"Key":"Instant","Value":"2"},{"Key":"Fastest","Value":"3"},{"Key":"Faster","Value":"4"},{"Key":"Fast","Value":"5"},{"Key":"Misc","Value":"6"},{"Key":"Packette","Value":"7"}],"hyperion_product_types":[{"Key":"Tanning Lotion","Value":"1"},{"Key":"Facial Lotion","Value":"2"},{"Key":"Extender Lotion","Value":"3"},{"Key":"Extender Wash","Value":"4"},{"Key":"Leg Lotion","Value":"5"},{"Key":"Sunless  Product","Value":"6"},{"Key":"Lip Product","Value":"7"},{"Key":"Eye Protection","Value":"8"},{"Key":"Outdoor Lotion","Value":"9"},{"Key":"Burn Relief","Value":"10"},{"Key":"Misc","Value":"11"},{"Key":"Tanning Lotion Packette","Value":"16"},{"Key":"Sunless Packette","Value":"17"},{"Key":"Facial Lotion Packette","Value":"18"},{"Key":"Beauty Product","Value":"19"},{"Key":"Tanning Accessory","Value":"20"}],"hyperion_products":[{"Key":"Product 1 (IP - Bottle)","Value":"1"},{"Key":"Product 2 (FT - Bottle)","Value":"3"}],"hyperion_subpackages":[{"Key":"Employee","Value":"-2"},{"Key":"Walk in - NONE","Value":"-1"},{"Key":"CREDITS","Value":"1"},{"Key":"MEM CREDITS","Value":"2"},{"Key":"FAST SESSIONS","Value":"5"}]}}

Open in new window

doesn't help/ still get same error
Any one else have any ideas on this?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
These three keys are missing in the json object/string.
Work fine :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Windows.Forms;
using System.IO;

namespace Convertion
{
    [Serializable]

    public class ConvertSettings
    {
        static void Main(string[] args)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ConvertSettings));
            ConvertSettings settings;

            using (FileStream fs = File.OpenRead(Application.StartupPath + "\\json.txt"))
            {
                settings = ser.ReadObject(fs) as ConvertSettings;
            }
            // Do thing here with setting
        }

                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>(); // Not found

                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>(); // Not found

//                public Dictionary<string, string> from_to_subpackages = new Dictionary<string, string>(); // Not found

    }
}

Open in new window

You may do some test with the JSON string in code snippet.
It should work (all three pairs is in the file)
{"EmpStart":740,"helios_to_hyperion_locations":[],"helios_to_hyperion_mems":[],"hyperion_eft_memberships":["1","2","3","4","5","6"],"hyperion_locations":[{"Key":"Global Settings","Value":"-1"},{"Key":"WSC","Value":"1"},{"Key":"OVC","Value":"2"},{"Key":"SWL","Value":"3"},{"Key":"LMP","Value":"4"},{"Key":"LSL","Value":"5"},{"Key":"KPC","Value":"6"},{"Key":"HPC","Value":"7"},{"Key":"CML","Value":"8"},{"Key":"Corp","Value":"9"},{"Key":"CCL","Value":"10"},{"Key":"CCM","Value":"11"},{"Key":"BCM","Value":"12"},{"Key":"NML","Value":"13"},{"Key":"AAL","Value":"14"},{"Key":"GHL","Value":"15"},{"Key":"RPM","Value":"16"},{"Key":"017","Value":"17"},{"Key":"018","Value":"18"}],"hyperion_memberships":[{"Key":"NONE","Value":"-1"},{"Key":"Employee","Value":"-2"},{"Key":"All Access","Value":"1"},{"Key":"Instant Plus","Value":"2"},{"Key":"Fastest","Value":"3"},{"Key":"Faster","Value":"4"},{"Key":"Fast","Value":"5"},{"Key":"Fast 9.99","Value":"6"},{"Key":"EZ1 All Access","Value":"7"},{"Key":"EZ1 Instant Plus","Value":"8"},{"Key":"EZ1 Fastest","Value":"9"},{"Key":"EZ1 Faster","Value":"10"},{"Key":"EZ1 Fast","Value":"11"},{"Key":"1M All Access","Value":"12"},{"Key":"1M Instant Plus","Value":"13"},{"Key":"1M Fastest","Value":"14"},{"Key":"1M Faster","Value":"15"},{"Key":"1M Fast","Value":"16"},{"Key":"1W Instant Plus","Value":"17"},{"Key":"1W Fastest","Value":"18"},{"Key":"1W Faster","Value":"19"},{"Key":"1W Fast","Value":"20"}],"hyperion_product_cat":[{"Key":"Instant Plus","Value":"1"},{"Key":"Instant","Value":"2"},{"Key":"Fastest","Value":"3"},{"Key":"Faster","Value":"4"},{"Key":"Fast","Value":"5"},{"Key":"Misc","Value":"6"},{"Key":"Packette","Value":"7"}],"hyperion_product_types":[{"Key":"Tanning Lotion","Value":"1"},{"Key":"Facial Lotion","Value":"2"},{"Key":"Extender Lotion","Value":"3"},{"Key":"Extender Wash","Value":"4"},{"Key":"Leg Lotion","Value":"5"},{"Key":"Sunless Product","Value":"6"},{"Key":"Lip Product","Value":"7"},{"Key":"Eye Protection","Value":"8"},{"Key":"Outdoor Lotion","Value":"9"},{"Key":"Burn Relief","Value":"10"},{"Key":"Misc","Value":"11"},{"Key":"Tanning Lotion Packette","Value":"16"},{"Key":"Sunless Packette","Value":"17"},{"Key":"Facial Lotion Packette","Value":"18"},{"Key":"Beauty Product","Value":"19"},{"Key":"Tanning Accessory","Value":"20"}],"hyperion_products":[{"Key":"Product 1 (IP - Bottle)","Value":"1"},{"Key":"Product 2 (FT - Bottle)","Value":"3"}],"hyperion_subpackages":[{"Key":"Employee","Value":"-2"},{"Key":"Walk in - NONE","Value":"-1"},{"Key":"CREDITS","Value":"1"},{"Key":"MEM CREDITS","Value":"2"},{"Key":"FAST SESSIONS","Value":"5"}],"helios_memberships":"","helios_user_defined":"","from_to_subpackages":""}

Open in new window

Could you post the code that fill the value ? Because else we let the class empty, all three pairs currently missing CAN be found :


{"EmpStart":1,"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":[]}

Open in new window

I think the dictionary is set to null in your code when there's no value (the "fill" part/section)
Check if you've not "bad" characters too inside this three dictionaries.