I'm building a chess game, and I want to serialize the board and send it to the server after each move. I'm going to use JSON for this task.
The way my game is represented is as a 2 dimensional array of a custom POJO I've created called Piece.
At the moment I'm just creating a JSON-ish String, not actual JSON, which is problematic, because I can't treat it like real JSON now, indexing into it and so on.
Here's the code I'm using to create this JSON-ish construct:
StringBuilder result = new StringBuilder();
for (int i = 0; i < piecesMatrix.length; i++) {
result.append("\"row " + i + "\":\n");
for (int j = 0; j < piecesMatrix[i].length; j ++) {
result.append("\"column " + j + "\": ");
if (piecesMatrix[i][j] == null || piecesMatrix[i][j] instanceof LegalMove) {
result.append(" \"null\",\n");
} else {
result.append(" \"" + piecesMatrix[i][j].getClass().getSimpleName() + "\",\n");
}
}
}
message = result.toString();
The result renders like so:
[ 10-06 20:35:19.971 7509: 7509 D/Data =
]
"row 0":
"column 0": "BlackRook",
"column 1": "BlackKnight",
"column 2": "BlackBishop",
"column 3": "BlackQueen",
"column 4": "BlackKing",
"column 5": "BlackBishop",
"column 6": "BlackKnight",
"column 7": "BlackRook",
"row 1":
"column 0": "BlackPawn",
"column 1": "BlackPawn",
"column 2": "BlackPawn",
"column 3": "BlackPawn",
"column 4": "BlackPawn",
"column 5": "BlackPawn",
"column 6": "BlackPawn",
"column 7": "BlackPawn",
"row 2":
"column 0": "null",
"column 1": "null",
"column 2": "null",
"column 3": "null",
"column 4": "null",
"column 5": "null",
"column 6": "null",
"column 7": "null",
"row 3":
"column 0": "null",
"column 1": "null",
"column 2": "null",
"column 3": "null",
"column 4": "null",
"column 5": "null",
"column 6": "null",
"column 7": "null",
"row 4":
"column 0": "null",
"column 1": "WhitePawn",
"column 2": "null",
"column 3": "null",
"column 4": "null",
"column 5": "null",
"column 6": "null",
"column 7": "null",
"row 5":
"column 0": "null",
"column 1": "null",
"column 2": "null",
"column 3": "null",
"column 4": "null",
"column 5": "null",
"column 6": "null",
"column 7": "null",
"row 6":
"column 0": "WhitePawn",
"column 1": "null",
"column 2": "WhitePawn",
"column 3": "WhitePawn",
"column 4": "WhitePawn",
"column 5": "WhitePawn",
"column 6": "WhitePawn",
"column 7": "WhitePawn",
"row 7":
"column 0": "WhiteRook",
"column 1": "WhiteKnight",
"column 2": "WhiteBishop",
"column 3": "WhiteQueen",
"column 4": "WhiteKing",
"column 5": "WhiteBishop",
"column 6": "WhiteKnight",
"column 7": "WhiteRook",
The question is, how could I turn that into real JSON?
Ideally I'd like the output to look like this:
"row 0":
"column 0": "Rook",
"column 1": "Knight",
"column 2": "Bishop",
...,
"row 1":
"column 0": "Pawn",
"column 1": "null",
"column 2": "Pawn",
...,
"row 2":
"column 0": "null",
"column 1": "Pawn",
"column 2": "null",
...
EDIT:
I tried:
Gson gson = new
GsonBuilder().setPrettyPrinting().serializeNulls().create();
message = gson.toJson(piecesMatrix);
I got:
SecurityException: Can not make a java.lang.reflect.Method constructor accessible
EDIT II
I tried:
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
message = gson.toJson(piecesMatrix, Piece.class);
I got:
[ 10-07 00:28:20.591 10899:10899 D/Data =
]
{}