[英]Difficulty with Windows Azure Mobile Services and serialization
我正在努力使我的模型類型與Windows Azure移動服務一起使用。 它工作正常,除非添加以下成員:
[DataMemberJsonConverter(ConverterType = typeof(DictionaryJsonConverter))]
public IDictionary<Tuple<int, int>, BoardSpaceState> pieceLocations { get; set; }
/**
* All of this serialization could probably be done better,
* but I've spent enough time trying to make it work already.
*/
public class DictionaryJsonConverter : IDataMemberJsonConverter
{
public static Tuple<int, int> tupleOfString(string str)
{
var match = Regex.Match(str, @"\((\d+), (\d+)\)");
// need to grab indexes 1 and 2 because 0 is the entire match
return Tuple.Create(int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value));
}
public object ConvertFromJson(IJsonValue val)
{
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(val.GetString());
var deserialized = new Dictionary<Tuple<int, int>, BoardSpaceState>();
foreach (var pieceLoc in dict)
{
deserialized[tupleOfString(pieceLoc.Key)] = (BoardSpaceState) Enum.Parse(typeof(BoardSpaceState), pieceLoc.Value);
}
return deserialized;
}
public IJsonValue ConvertToJson(object instance)
{
var dict = (IDictionary<Tuple<int, int>, BoardSpaceState>)instance;
IDictionary<Tuple<int, int>, string> toSerialize = new Dictionary<Tuple<int, int>, string>();
foreach (var pieceLoc in dict)
{
/** There may be an easier way to convert the enums to strings
* http://stackoverflow.com/questions/2441290/json-serialization-of-c-sharp-enum-as-string
* By default, Json.NET just converts the enum to its numeric value, which is not helpful.
* There could also be a way to do these dictionary conversions in a more functional way.
*/
toSerialize[pieceLoc.Key] = pieceLoc.Value.ToString();
}
var serialized = JsonConvert.SerializeObject(toSerialize);
return JsonValue.CreateStringValue(serialized);
}
}
BoardSpaceState.cs :
public enum BoardSpaceState
{
FriendlyPieceShort,
FriendlyPieceTall,
OpponentPieceShort,
OpponentPieceTall,
None
}
這對於Azure仍然有效,我可以在管理門戶中看到數據。 但是,當我嘗試使用toListAsync()
加載數據時, toListAsync()
以下異常:
{"Object must implement IConvertible."} System.Exception {System.InvalidCastException}
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)\r\n
at Microsoft.WindowsAzure.MobileServices.TypeExtensions.ChangeType(Object value, Type desiredType)\r\n
at Microsoft.WindowsAzure.MobileServices.MobileServiceTableSerializer.Deserialize(IJsonValue value, Object instance, Boolean ignoreCustomSerialization)\r\n
at Microsoft.WindowsAzure.MobileServices.MobileServiceTableSerializer.Deserialize(IJsonValue value, Object instance)\r\n
at Microsoft.WindowsAzure.MobileServices.MobileServiceTableSerializer.Deserialize[T](IJsonValue value)\r\n
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n
at Microsoft.WindowsAzure.MobileServices.TotalCountList`1..ctor(IEnumerable`1 sequence)\r\n
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<ToListAsync>d__3f.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n
at chivalry.DataManager.<withServerData>d__2.MoveNext() in c:\\Users\\Rosarch\\Documents\\Visual Studio 2012\\Projects\\chivalry\\chivalry\\DataManager.cs:line 35" string
HRESULT是-2147467262。
我究竟做錯了什么?
更新:
我收到錯誤的行是:
private IMobileServiceTable<Game> gameTable = App.MobileService.GetTable<Game>();
// ...
var games = await gameTable.ToListAsync(); // error here
對於它的價值,如果我只是從DictionaryJsonConverter.ConvertFromJson
返回new Dictionary<Tuple<int, int>, BoardSpaceState>()
,我也會遇到相同的錯誤。
這似乎是Azure移動服務客戶端SDK中的錯誤-我將其提交給產品團隊,謝謝您的舉報。
同時,有一種解決方法可以使它起作用: pieceLocations
使用接口類型聲明pieceLocations
變量, pieceLocations
使用字典類型聲明它,它就可以工作-這是我剛剛嘗試的代碼。
public sealed partial class MainPage : Page
{
public static MobileServiceClient MobileService = new MobileServiceClient(
"https://YOUR-APP-NAME-HERE.azure-mobile.net/",
"YOUR-APP-KEY-HERE"
);
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private async void btnStart_Click_1(object sender, RoutedEventArgs e)
{
try
{
Game game = new Game();
game.pieceLocations = new Dictionary<Tuple<int, int>, BoardSpaceState>();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
game.pieceLocations[Tuple.Create(i, j)] = BoardSpaceState.None;
}
}
game.pieceLocations[Tuple.Create(1, 2)] = BoardSpaceState.FriendlyPieceShort;
game.pieceLocations[Tuple.Create(2, 1)] = BoardSpaceState.FriendlyPieceTall;
game.pieceLocations[Tuple.Create(3, 4)] = BoardSpaceState.OpponentPieceShort;
game.pieceLocations[Tuple.Create(4, 3)] = BoardSpaceState.OpponentPieceTall;
var table = MobileService.GetTable<Game>();
await table.InsertAsync(game);
AddToDebug("Inserted game: ", game.Id);
AddToDebug("Now trying to retrieve it...");
var allGames = await table.ToListAsync();
AddToDebug("All games, length = {0}", allGames.Count);
}
catch (Exception ex)
{
AddToDebug("Error: {0}", ex);
}
}
void AddToDebug(string text, params object[] args)
{
if (args != null && args.Length > 0) text = string.Format(text, args);
this.txtDebug.Text = this.txtDebug.Text + text + Environment.NewLine;
}
}
[DataTable(Name = "Test")]
public class Game
{
public int Id { get; set; }
[DataMemberJsonConverter(ConverterType = typeof(DictionaryJsonConverter))]
public Dictionary<Tuple<int, int>, BoardSpaceState> pieceLocations { get; set; }
}
public enum BoardSpaceState
{
FriendlyPieceShort,
FriendlyPieceTall,
OpponentPieceShort,
OpponentPieceTall,
None
}
public class DictionaryJsonConverter : IDataMemberJsonConverter
{
public static Tuple<int, int> tupleOfString(string str)
{
var match = Regex.Match(str, @"\((\d+), (\d+)\)");
// need to grab indexes 1 and 2 because 0 is the entire match
return Tuple.Create(int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value));
}
public object ConvertFromJson(IJsonValue val)
{
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(val.GetString());
var deserialized = new Dictionary<Tuple<int, int>, BoardSpaceState>();
foreach (var pieceLoc in dict)
{
deserialized[tupleOfString(pieceLoc.Key)] = (BoardSpaceState)Enum.Parse(typeof(BoardSpaceState), pieceLoc.Value);
}
return deserialized;
}
public IJsonValue ConvertToJson(object instance)
{
var dict = (IDictionary<Tuple<int, int>, BoardSpaceState>)instance;
IDictionary<Tuple<int, int>, string> toSerialize = new Dictionary<Tuple<int, int>, string>();
foreach (var pieceLoc in dict)
{
/** There may be an easier way to convert the enums to strings
* http://stackoverflow.com/questions/2441290/json-serialization-of-c-sharp-enum-as-string
* By default, Json.NET just converts the enum to its numeric value, which is not helpful.
* There could also be a way to do these dictionary conversions in a more functional way.
*/
toSerialize[pieceLoc.Key] = pieceLoc.Value.ToString();
}
var serialized = JsonConvert.SerializeObject(toSerialize);
return JsonValue.CreateStringValue(serialized);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.