我正在为我创建的iOS游戏构建自己的后端。游戏目前使用Game Center,但我想把它移植到其他平台,所以我需要一些不同的东西。我正在使用ServiceStack和OrmLite与MySQL数据库来存储游戏数据。我遇到的问题是游戏数据的一个类包含一个数组。游戏是一个棋盘游戏,所以我使用锯齿状阵列来存储所有棋盘方块,无论是那个位置的棋子,还是如果没有棋子那么零。当我反序列化Board类时,所有的null值都被替换为默认的Piece类。
public class Piece
{
public PieceColor Color { get; set; }
public PieceType Type { get; set; }
public Piece (PieceColor newColor, PieceType newType)
{
Color = newColor;
Type = newType;
}
}
PieceColor和PieceType都是枚举,当反序列化并获得应该为空的PIece对象时,两者都用0值初始化,给出枚举中的第一项。
public class Board {
public Piece[][] Layout { get; set; }
public bool SingleMove { get; set; }
public bool WeakHnefi { get; set; }
public List<Move> MoveHistory { get; set; }
public Move CurrentMove { get; set; }
public int BoardSize { get; set; }
public Variation Variation { get; set; }
[IgnoreDataMember]
public Move LastMove {
get {
return MoveHistory.Last ();
}
}
}
Layout属性是我遇到问题的数组。它最初是一个多维数组,尝试将其更改为锯齿状数组,以查看它是否对反序列化产生了影响。它没有。 Board类,各种方法以及带有几个参数的构造函数还有更多内容。我试过创建一个空的无参数构造函数,一个初始化一些属性的无参数构造函数,没有无参数构造函数。无论我做什么,我都无法使Layout属性正确反序列化。
public class Game {
public Board Board { get; set; }
public Variation Variation { get; set; }
public PieceColor CurrentTurn { get; set; }
public PieceColor WinningColor { get; set; }
public Position SelectedPiece { get; set; }
public bool GameOver { get; set; }
public int SquareSize { get; set; }
public int BorderSize { get; set; }
public string ImageName { get; set; }
public bool IsTablet { get; set; }
[IgnoreDataMember]
public PieceColor Opponent {
get {
if (CurrentTurn == PieceColor.White)
return PieceColor.Red;
else
return PieceColor.White;
}
}
}
实际被序列化并存储在数据库列中的游戏类。据我所知,这个类中的其他所有内容(以及Board中的布局除外)都正确反序列化,而不是一个数组。
数据库中序列化Game类的一个示例:
{"Board":{"Layout":[[{},{},{},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{},{},{}],[{},{},{},{},{},{"Color":"Red","Type":"Hunn"},{},{},{},{},{}],[{},{},{},{},{},{},{},{},{},{},{}],[{"Color":"Red","Type":"Hunn"},{},{},{},{},{"Color":"White","Type":"Hunn"},{},{},{},{},{"Color":"Red","Type":"Hunn"}],[{"Color":"Red","Type":"Hunn"},{},{},{},{"Color":"White","Type":"Hunn"},{"Color":"White","Type":"Hunn"},{"Color":"White","Type":"Hunn"},{},{},{},{"Color":"Red","Type":"Hunn"}],[{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{},{"Color":"White","Type":"Hunn"},{"Color":"White","Type":"Hunn"},{"Color":"White","Type":"Hnefi"},{"Color":"White","Type":"Hunn"},{"Color":"White","Type":"Hunn"},{},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"}],[{"Color":"Red","Type":"Hunn"},{},{},{},{"Color":"White","Type":"Hunn"},{"Color":"White","Type":"Hunn"},{"Color":"White","Type":"Hunn"},{},{"Color":"Red","Type":"Hunn"},{},{}],[{"Color":"Red","Type":"Hunn"},{},{},{},{},{"Color":"White","Type":"Hunn"},{},{},{},{},{"Color":"Red","Type":"Hunn"}],[{},{},{},{},{},{},{},{},{},{},{}],[{},{},{},{},{},{"Color":"Red","Type":"Hunn"},{},{},{},{},{}],[{},{},{},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{"Color":"Red","Type":"Hunn"},{},{},{}]],"SingleMove":false,"WeakHnefi":false,"MoveHistory":[{"Piece":{"Color":"Red","Type":"Hunn"},"BeforeMove":{"X":6,"Y":10},"AfterMove":{"X":6,"Y":8},"PiecesCaptured":[],"CapturedSquares":[]}],"BoardSize":11,"Variation":"Hnefatafl"},"Variation":"Hnefatafl","CurrentTurn":"White","WinningColor":"White","SelectedPiece":{"X":6,"Y":10},"GameOver":false,"SquareSize":28,"BorderSize":1,"ImageName":"Eleven.png","IsTablet":false}
当我从数据库插入/检索条目时,OrmLite / ServiceStack.Text正在处理序列化/反序列化。我不能为我的生活弄清楚问题是什么或如何解决它,并且没有运气寻找答案。
答案 0 :(得分:0)
ServiceStack不支持锯齿状或多维数组,您必须使用其他集合,例如List<List<int>>
。