I am messing around with a spell checker and I am having a problem De-serializing a dictionary of misspelled words and their suggested spellings. An application sends text to an ASP.NET page. The ASP app performs spell checking and returns a dictionary with the Keys being the misspelled words along with a List of correct suggestions.
Error that I am receiving in VS 2010 when ser.Deserialize is called:
An unhandled exception of type 'System.ArgumentException' occurred in System.Web.Extensions.dll
Additional information: Invalid object passed in, member name expected. (1): {"missspelled":["misspelled","miss spelled","miss-spelled","misspelling","misspell","dispelled","misspecified"],"wrods":["words","rods","woods","prods","w rods"]}
For the life of me I cannot figure out why the runtime is complaining. Any help would be appreciated.
ASP.NET code
Dictionary<string, List<string>> WordSuggestions = new Dictionary<string, List<string>>();
...
Populate dictionary with misspelled words and list of suggestions
...
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonresult = Server.HtmlEncode(serializer.Serialize(WordSuggestions));
Response.Write(jsonresult);
Data as seen/received by App
{"missspelled":["misspelled","miss spelled","miss-spelled","misspelling","misspell","dispelled","misspecified"],"wrods":["words","rods","woods","prods","w rods"]}
C# App Code (ReturnedSpelling holds the "Data as seen by App" as seen above.
JavaScriptSerializer ser = new JavaScriptSerializer();
MispelledWords.text = ser.Deserialize<Dictionary<string, string[]>>(ReturnedSpelling);
MispelledWords.text is an appropriate dictionary of type
Dictionary<string, string[]>;
One thing I just noticed is that in the ASP app, my Dictionary is:
Dictionary<string, List<string>>
and my C# app is:
Dictionary<string, string[]>
Although I can't see why this would be a problem after it has been serialized.
...
I have since changed my dictionary
string[] to List<string>
on my application (receiving) side. It still does not work...as I suspected. I am serializing then encoding then sending and when the data is returned, decoding and de-serializing in that order.
This is what I am using on my application side:
Encoding...
HttpUtility.UrlEncode
Decoding...
HttpUtility.UrlDecode