示例#1
0
        public void ProcessRequest(HttpContext context)
        {
            SpellCheckerInput  input       = SpellCheckerInput.Parse(new StreamReader(context.Request.InputStream));
            SpellCheckerResult suggestions = null;

            switch (input.Method)
            {
            case "checkWords":
                suggestions = CheckWords(input.Language, input.Words.ToArray());
                break;

            case "getSuggestions":
                suggestions = GetSuggestions(input.Language, input.Words[0]);
                break;

            default:
                suggestions = new SpellCheckerResult();
                break;
            }

            suggestions.id = input.Id;
            JavaScriptSerializer ser = new JavaScriptSerializer();
            var res = ser.Serialize(suggestions);

            context.Response.Write(res);
        }
 /// <summary>
 /// Parses the specified stream into the object
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <returns></returns>
 public static SpellCheckerInput Parse(StreamReader inputStream)
 {
     if (inputStream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (inputStream.EndOfStream)
     {
         throw new ArgumentException("Stream end reached before we started!");
     }
     var jsonString = inputStream.ReadLine();
     var deserialized = (Dictionary<string, object>)new JavaScriptSerializer().DeserializeObject(jsonString);
     
     var input = new SpellCheckerInput();
     input.Id = (string)deserialized["id"];
     input.Method = (string)deserialized["method"];
     input.Language = (string)((object[])deserialized["params"])[0];
     if (((object[])deserialized["params"])[1] is string)
     {
         input.Words.Add((string)((object[])deserialized["params"])[1]);
     }
     else
     {
         var words = ((object[])((object[])deserialized["params"])[1]);
         foreach (var word in words)
         {
             input.Words.Add((string)word);
         } 
     }
     return input;
 }
示例#3
0
        /// <summary>
        /// Parses the specified stream into the object
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public static SpellCheckerInput Parse(StreamReader inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (inputStream.EndOfStream)
            {
                throw new ArgumentException("Stream end reached before we started!");
            }
            var jsonString   = inputStream.ReadLine();
            var deserialized = (Dictionary <string, object>) new JavaScriptSerializer().DeserializeObject(jsonString);

            var input = new SpellCheckerInput();

            input.Id       = (string)deserialized["id"];
            input.Method   = (string)deserialized["method"];
            input.Language = (string)((object[])deserialized["params"])[0];
            if (((object[])deserialized["params"])[1] is string)
            {
                input.Words.Add((string)((object[])deserialized["params"])[1]);
            }
            else
            {
                var words = ((object[])((object[])deserialized["params"])[1]);
                foreach (var word in words)
                {
                    input.Words.Add((string)word);
                }
            }
            return(input);
        }