Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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
...
//JSON encode Dictionary and return.
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonresult = Server.HtmlEncode(serializer.Serialize(WordSuggestions));
Response.Write(jsonresult);


Data as seen/received by App

{&quot;missspelled&quot;:[&quot;misspelled&quot;,&quot;miss spelled&quot;,&quot;miss-spelled&quot;,&quot;misspelling&quot;,&quot;misspell&quot;,&quot;dispelled&quot;,&quot;misspecified&quot;],&quot;wrods&quot;:[&quot;words&quot;,&quot;rods&quot;,&quot;woods&quot;,&quot;prods&quot;,&quot;w rods&quot;]}


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
Posted
Updated 23-Oct-10 7:15am
v4
Comments
Sandeep Mewara 22-Oct-10 13:44pm    
Well framed and asked.

These are few pointers. Try this
- Did you check the data whether it is encoded form or not. As are encoding after Serializing, you might need to decode this before DeSerializing.

- Also yo are trying to change the type Dictionary<string, List<string>> to Dictionary<string, string[]>
It might be the cause. Don't change the type. You may change it after DeSerializing using various techniques.

Also, give some data for WordSuggestions, so that we also can check it
 
Share this answer
 
Comments
willworknow1 23-Oct-10 13:26pm    
WordSuggestions, in each Key, holds a misspelled word that was found (in order to reference it in the original document). The List associated to each Key holds a list of strings that represents the suggested/possible correct spelling for that Key string. You can see the JSON encoded packet above.
After re reading the posting, I saw the issue.

Here was the problem:

I was encoding the returned serialized data in the ASP.NET with HtmlEncode but using UrlDecode in the Windows app.

Oops.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900