ServiceStack.Text错误地使用空条目反序列化Array

我正在为我创建的iOS游戏构建我自己的后端。 游戏目前使用Game Center,但我想将它移植到其他平台,所以我需要不同的东西。 我使用带有MySQL数据库的ServiceStack和OrmLite来存储游戏数据。 我遇到的问题是游戏数据的其中一个类包含一个数组。 游戏是一个棋盘游戏,所以我使用锯齿阵列来存储所有棋盘格,或者是该棋子的棋子,如果没有棋子,则使用空值。 当我反序列化Board类时,所有的空值都被默认的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属性正确反序列化。

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处理。 我不能为了我的生活找出问题所在或者如何解决问题,而且还没有找到答案的运气。


锯齿或多维数组在ServiceStack中不受支持,您将不得不使用另一个集合,如List<List<int>>

链接地址: http://www.djcxy.com/p/65965.html

上一篇: ServiceStack.Text deserializing an Array with null entries incorrectly

下一篇: Where to create MySql tables ServiceStack & OrmLite