简体   繁体   中英

ServiceStack.Text deserializing an Array with null entries incorrectly

I'm working on building my own backend for an iOS game I created. The game currently uses Game Center but I want to port it to other platforms, so I need something different. I'm using ServiceStack and OrmLite with a MySQL database to store the game data. The problem I'm having is that one of the classes for the game data contains an array. The game is a board game, so I use a jagged array to store all the board squares, either the piece in that position, or null if there's no piece. When I deserialize the Board class, all of the null values are being replaced with a default Piece class.

public class Piece
{
    public PieceColor Color { get; set; }
    public PieceType Type { get; set; }

    public Piece (PieceColor newColor, PieceType newType)
    {
        Color = newColor;
        Type = newType;
    }
}

PieceColor and PieceType are both enums, when deserializing and getting a PIece object where nulls are supposed to be, both are initialized with 0 values, giving the first item in the enum.

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 ();
        }
    }
}

The Layout property is the array I'm having issues with. It was originally a multidimensional array, tried changing it to a jagged array to see if it made a difference for deserializing. It hasn't. There's more to the Board class, various methods and a constructor with a couple of parameters. I've tried creating an empty parameterless constructor, a parameterless constructor that initializes some properties, and no parameterless constructor. Regardless what I do, I can't get the Layout property to deserialize properly.

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;
        }
    }
}

The game class that is actually being serialized and stored in a database column. As far as I can tell, everything else in this class (and everything but layout in Board) is deserializing properly, just not the one array.

An example of the serialized Game class in the database:

{"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}

The serialization/deserialization is being handled by OrmLite/ServiceStack.Text when I insert/retrieve entries from the database. I can't for the life of me figure out what the problem is or how to fix it, and haven't had any luck searching for answers.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM