Pyromanci
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.Serializat ion.Serial izationExc eption' occurred in System.ServiceModel.Web.dl l
Additional information: The data contract type 'HyperionConvertion.Conver tSettings' cannot be deserialized because the required data members 'EmpStart, from_to_subpackages, helios_memberships, helios_to_hyperion_locatio ns, 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_locatio ns = 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.ToArra y());
}
StreamWriter sw = File.CreateText(Applicatio n.StartupP ath + "\\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(Co nvertSetti ngs));
using (FileStream fs = File.OpenRead(Application. StartupPat h + "\\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" :[],"helio s_to_hyper ion_mems": [],"hyperi on_eft_mem berships": ["1","2"," 3","4","5" ,"6"],"hyp erion_loca tions":[{" Key":"Glob al Settings","Value":"-1"},{" Key":"WSC" ,"Value":" 1"},{"Key" :"OVC","Va lue":"2"}, {"Key":"SW L","Value" :"3"},{"Ke y":"LMP"," Value":"4" },{"Key":" LSL","Valu e":"5"},{" Key":"KPC" ,"Value":" 6"},{"Key" :"HPC","Va lue":"7"}, {"Key":"CM L","Value" :"8"},{"Ke y":"Corp", "Value":"9 "},{"Key": "CCL","Val ue":"10"}, {"Key":"CC M","Value" :"11"},{"K ey":"BCM", "Value":"1 2"},{"Key" :"NML","Va lue":"13"} ,{"Key":"A AL","Value ":"14"},{" Key":"GHL" ,"Value":" 15"},{"Key ":"RPM","V alue":"16" },{"Key":" 017","Valu e":"17"},{ "Key":"018 ","Value": "18"}],"hy perion_mem berships": [{"Key":"N ONE","Valu e":"-1"},{ "Key":"Emp loyee","Va lue":"-2"} ,{"Key":"A ll Access","Value":"1"},{"Key ":"Instant Plus","Value":"2"},{"Key": "Fastest", "Value":"3 "},{"Key": "Faster"," Value":"4" },{"Key":" Fast","Val ue":"5"},{ "Key":"Fas t 9.99","Value":"6"},{"Key": "EZ1 All Access","Value":"7"},{"Key ":"EZ1 Instant Plus","Value":"8"},{"Key": "EZ1 Fastest","Value":"9"},{"Ke y":"EZ1 Faster","Value":"10"},{"Ke y":"EZ1 Fast","Value":"11"},{"Key" :"1M All Access","Value":"12"},{"Ke y":"1M Instant Plus","Value":"13"},{"Key" :"1M Fastest","Value":"14"},{"K ey":"1M Faster","Value":"15"},{"Ke y":"1M Fast","Value":"16"},{"Key" :"1W Instant Plus","Value":"17"},{"Key" :"1W Fastest","Value":"18"},{"K ey":"1W Faster","Value":"19"},{"Ke y":"1W Fast","Value":"20"}],"hype rion_produ ct_cat":[{ "Key":"Ins tant Plus","Value":"1"},{"Key": "Instant", "Value":"2 "},{"Key": "Fastest", "Value":"3 "},{"Key": "Faster"," Value":"4" },{"Key":" Fast","Val ue":"5"},{ "Key":"Mis c","Value" :"6"},{"Ke y":"Packet te","Value ":"7"}],"h yperion_pr oduct_type s":[{"Key" :"Tanning Lotion","Value":"1"},{"Key ":"Facial Lotion","Value":"2"},{"Key ":"Extende r Lotion","Value":"3"},{"Key ":"Extende r Wash","Value":"4"},{"Key": "Leg Lotion","Value":"5"},{"Key ":"Sunless Product","Value":"6"},{"Ke y":"Lip Product","Value":"7"},{"Ke y":"Eye Protection","Value":"8"},{ "Key":"Out door Lotion","Value":"9"},{"Key ":"Burn Relief","Value":"10"},{"Ke y":"Misc", "Value":"1 1"},{"Key" :"Tanning Lotion Packette","Value":"16"},{" Key":"Sunl ess Packette","Value":"17"},{" Key":"Faci al Lotion Packette","Value":"18"},{" Key":"Beau ty Product","Value":"19"},{"K ey":"Tanni ng Accessory","Value":"20"}], "hyperion_ products": [{"Key":"P roduct 1 (IP - Bottle)","Value":"1"},{"Ke y":"Produc t 2 (FT - Bottle)","Value":"3"}],"hy perion_sub packages": [{"Key":"E mployee"," Value":"-2 "},{"Key": "Walk in - NONE","Value":"-1"},{"Key" :"CREDITS" ,"Value":" 1"},{"Key" :"MEM CREDITS","Value":"2"},{"Ke y":"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.
An unhandled exception of type 'System.Runtime.Serializat
Additional information: The data contract type 'HyperionConvertion.Conver
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_locatio
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
using (MemoryStream ms = new MemoryStream())
{
ser.WriteObject(ms, settings);
json = Encoding.Default.GetString
}
StreamWriter sw = File.CreateText(Applicatio
sw.WriteLine(json);
sw.Close();
}
to get it back I'm doing this
public ConvertSettings settings = new ConvertSettings();
// ...........
private void ReadSettings()
{
DataContractJsonSerializer
using (FileStream fs = File.OpenRead(Application.
{
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
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.
ASKER
It is. It's right there at the bottom
Or here:
{"EmpStart":740,"helios_to _hyperion_ locations" :[],"helio s_to_hyper ion_mems": [],"hyperi on_eft_mem berships": ["1","2"," 3","4","5" ,"6"],"hyp erion_loca tions":[{" Key":"Glob al Settings","Value":"-1"},{" Key":"WSC" ,"Value":" 1"},{"Key" :"OVC","Va lue":"2"}, {"Key":"SW L","Value" :"3"},{"Ke y":"LMP"," Value":"4" },{"Key":" LSL","Valu e":"5"},{" Key":"KPC" ,"Value":" 6"},{"Key" :"HPC","Va lue":"7"}, {"Key":"CM L","Value" :"8"},{"Ke y":"Corp", "Value":"9 "},{"Key": "CCL","Val ue":"10"}, {"Key":"CC M","Value" :"11"},{"K ey":"BCM", "Value":"1 2"},{"Key" :"NML","Va lue":"13"} ,{"Key":"A AL","Value ":"14"},{" Key":"GHL" ,"Value":" 15"},{"Key ":"RPM","V alue":"16" },{"Key":" 017","Valu e":"17"},{ "Key":"018 ","Value": "18"}],"hy perion_mem berships": [{"Key":"N ONE","Valu e":"-1"},{ "Key":"Emp loyee","Va lue":"-2"} ,{"Key":"A ll Access","Value":"1"},{"Key ":"Instant Plus","Value":"2"},{"Key": "Fastest", "Value":"3 "},{"Key": "Faster"," Value":"4" },{"Key":" Fast","Val ue":"5"},{ "Key":"Fas t 9.99","Value":"6"},{"Key": "EZ1 All Access","Value":"7"},{"Key ":"EZ1 Instant Plus","Value":"8"},{"Key": "EZ1 Fastest","Value":"9"},{"Ke y":"EZ1 Faster","Value":"10"},{"Ke y":"EZ1 Fast","Value":"11"},{"Key" :"1M All Access","Value":"12"},{"Ke y":"1M Instant Plus","Value":"13"},{"Key" :"1M Fastest","Value":"14"},{"K ey":"1M Faster","Value":"15"},{"Ke y":"1M Fast","Value":"16"},{"Key" :"1W Instant Plus","Value":"17"},{"Key" :"1W Fastest","Value":"18"},{"K ey":"1W Faster","Value":"19"},{"Ke y":"1W Fast","Value":"20"}],"hype rion_produ ct_cat":[{ "Key":"Ins tant Plus","Value":"1"},{"Key": "Instant", "Value":"2 "},{"Key": "Fastest", "Value":"3 "},{"Key": "Faster"," Value":"4" },{"Key":" Fast","Val ue":"5"},{ "Key":"Mis c","Value" :"6"},{"Ke y":"Packet te","Value ":"7"}],"h yperion_pr oduct_type s":[{"Key" :"Tanning Lotion","Value":"1"},{"Key ":"Facial Lotion","Value":"2"},{"Key ":"Extende r Lotion","Value":"3"},{"Key ":"Extende r Wash","Value":"4"},{"Key": "Leg Lotion","Value":"5"},{"Key ":"Sunless Product","Value":"6"},{"Ke y":"Lip Product","Value":"7"},{"Ke y":"Eye Protection","Value":"8"},{ "Key":"Out door Lotion","Value":"9"},{"Key ":"Burn Relief","Value":"10"},{"Ke y":"Misc", "Value":"1 1"},{"Key" :"Tanning Lotion Packette","Value":"16"},{" Key":"Sunl ess Packette","Value":"17"},{" Key":"Faci al Lotion Packette","Value":"18"},{" Key":"Beau ty Product","Value":"19"},{"K ey":"Tanni ng Accessory","Value":"20"}], "hyperion_ products": [{"Key":"P roduct 1 (IP - Bottle)","Value":"1"},{"Ke y":"Produc t 2 (FT - Bottle)","Value":"3"}],"hy perion_sub packages": [{"Key":"E mployee"," Value":"-2 "},{"Key": "Walk in - NONE","Value":"-1"},{"Key" :"CREDITS" ,"Value":" 1"},{"Key" :"MEM CREDITS","Value":"2"},{"Ke y":"FAST SESSIONS","Value":"5"}]}
Or here:
{"EmpStart":740,"helios_to
Try :
adding
{"myRoot" :
at the beggining
and
}
at the end
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"}]}}
ASKER
doesn't help/ still get same error
ASKER
Any one else have any ideas on this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
}
}
You may do some test with the JSON string in code snippet.
It should work (all three pairs is in the file)
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":""}
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":[]}
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.
Could you post the json.txt file ?
Regards.