Converted spell checker deserialization to json.net and changed the deserialized object to internal with correct naming conventions.

This commit is contained in:
Shannon
2013-12-19 11:06:00 +11:00
parent 64dbf0207b
commit f54cf32057
4 changed files with 56 additions and 40 deletions

View File

@@ -425,6 +425,7 @@
<Compile Include="umbraco.presentation\umbraco\members\EditMember.aspx.cs">
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="umbraco.presentation\umbraco\plugins\tinymce3\JsonSpellCheckerResult.cs" />
<Compile Include="umbraco.presentation\umbraco\users\EditUser.aspx.cs">
<SubType>ASPXCodeBehind</SubType>
</Compile>

View File

@@ -6,6 +6,7 @@ using System.Net;
using System.Text;
using System.Xml;
using System.Collections.Generic;
using Newtonsoft.Json;
// NB: This class was moved out of the client tinymce folder to aid with upgrades
// but we'll keep the old namespace to make things easier for now (MB)
@@ -70,16 +71,16 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker
public override SpellCheckerResult CheckWords(string language, string[] words)
{
string data = string.Join(" ", words); //turn them into a space-separated string as that's what google takes
string json = SendRequest(language, data);
var jsonRes = new JavaScriptSerializer().Deserialize<JsonSpellCheckerResult>(json);
string json = SendRequest(language, data);
var jsonRes = JsonConvert.DeserializeObject<JsonSpellCheckerResult>(json);
var res = new SpellCheckerResult();
// Get list of misspelled words
if (jsonRes.result != null && jsonRes.result.spellingCheckResponse != null)
if (jsonRes.Result != null && jsonRes.Result.SpellingCheckResponse != null)
{
foreach (var misspelling in jsonRes.result.spellingCheckResponse.misspellings)
foreach (var misspelling in jsonRes.Result.SpellingCheckResponse.Misspellings)
{
res.result.Add(data.Substring(misspelling.charStart, misspelling.charLength));
res.result.Add(data.Substring(misspelling.CharStart, misspelling.CharLength));
}
}
@@ -95,17 +96,17 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker
public override SpellCheckerResult GetSuggestions(string language, string word)
{
string json = SendRequest(language, word);
var jsonRes = new JavaScriptSerializer().Deserialize<JsonSpellCheckerResult>(json);
var jsonRes = JsonConvert.DeserializeObject<JsonSpellCheckerResult>(json);
var res = new SpellCheckerResult();
// Get list of suggestions
if (jsonRes.result != null && jsonRes.result.spellingCheckResponse != null)
if (jsonRes.Result != null && jsonRes.Result.SpellingCheckResponse != null)
{
foreach (var misspelling in jsonRes.result.spellingCheckResponse.misspellings)
foreach (var misspelling in jsonRes.Result.SpellingCheckResponse.Misspellings)
{
foreach (var suggestion in misspelling.suggestions)
foreach (var suggestion in misspelling.Suggestions)
{
res.result.Add(suggestion.suggestion);
res.result.Add(suggestion.Suggestion);
}
}
}

View File

@@ -0,0 +1,44 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker
{
/// <summary>
/// Object used to deserialise the Google Json response
/// </summary>
internal class JsonSpellCheckerResult
{
[JsonProperty("result")]
public GoogleResponseResult Result { get; set; }
public class GoogleResponseResult
{
[JsonProperty("spellingCheckResponse")]
public GoogleResponseSpellingCheckResponse SpellingCheckResponse { get; set; }
}
public class GoogleResponseSpellingCheckResponse
{
[JsonProperty("misspellings")]
public List<GoogleResponseMisspelling> Misspellings { get; set; }
}
public class GoogleResponseMisspelling
{
[JsonProperty("charStart")]
public int CharStart { get; set; }
[JsonProperty("charLength")]
public int CharLength { get; set; }
[JsonProperty("suggestions")]
public List<GoogleResponseSuggestion> Suggestions { get; set; }
}
public class GoogleResponseSuggestion
{
[JsonProperty("suggestion")]
public string Suggestion { get; set; }
}
}
}

View File

@@ -32,34 +32,4 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker
/// <value>The error.</value>
public string error { get; set; }
}
/// <summary>
/// Object used to deserialise the Google Json response
/// </summary>
public class JsonSpellCheckerResult
{
public GoogleResponseResult result { get; set; }
public class GoogleResponseResult
{
public GoogleResponseSpellingCheckResponse spellingCheckResponse { get; set; }
}
public class GoogleResponseSpellingCheckResponse
{
public List<GoogleResponseMisspelling> misspellings { get; set; }
}
public class GoogleResponseMisspelling
{
public int charStart { get; set; }
public int charLength { get; set; }
public List<GoogleResponseSuggestion> suggestions { get; set; }
}
public class GoogleResponseSuggestion
{
public string suggestion { get; set; }
}
}
}