• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2316
  • Last Modified:

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.
0
Pyromanci
Asked:
Pyromanci
  • 9
  • 3
1 Solution
 
leakim971PluritechnicianCommented:
Hello Pyromanci,

Could you post the json.txt file ?

Regards.
0
 
PyromanciAuthor Commented:
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"}]}
0
 
leakim971PluritechnicianCommented:
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

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
PyromanciAuthor Commented:
doesn't help/ still get same error
0
 
PyromanciAuthor Commented:
Any one else have any ideas on this?
0
 
leakim971PluritechnicianCommented:
If you comment from ConvertSettings class :

public Dictionary helios_memberships = new Dictionary();
public Dictionary helios_user_defined = new Dictionary();
public Dictionary from_to_subpackages = new Dictionary();

ReadSettings() should run fine.

0
 
leakim971PluritechnicianCommented:
These three keys are missing in the json object/string.
0
 
leakim971PluritechnicianCommented:
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

0
 
leakim971PluritechnicianCommented:
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

0
 
leakim971PluritechnicianCommented:
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

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now