无法使用Jackson Databind

时间:2018-07-04 12:29:22

标签: json serialization jackson jackson-databind

我有一个ObjectPlayerProfile类,该类用作在我正在开发的游戏中存储玩家数据的蓝图。我已经从使用GSON切换到Jackson数据绑定(我听说它的序列化/反序列化速度更快,所以我决定尝试一下)。但是,解析器在序列化特定日期格式时遇到问题。

ObjectPlayerProfile.java:

public class ObjectPlayerProfile {

public String playerUUID;

public long playerKills = 0;
public long playerDeaths = 0;
public long zombieKills = 0;

public String discordAuthCode;
public String discordID;

public int rankID;

public EnumDiscordRank playerRank = EnumDiscordRank.CIVILIAN;

public int xp = 0;

public Date lastMessage = Calendar.getInstance().getTime();
public Date lastMessageSeconds = Calendar.getInstance().getTime();

public Date dateCreated = Calendar.getInstance().getTime();

public boolean isSupporter = false;

public Date dateBecameSupporter = Calendar.getInstance().getTime();

public EnumCapeDesign capeDesign = EnumCapeDesign.CAPE_DEFAULT;

public long xpBattlegrounds = 0;
public long bgWins = 0;
public long bgLosses = 0;
public EnumBattlegroundsRank bgRank = EnumBattlegroundsRank.NOOB;

public ArrayList<String> unlockedEmblems = new ArrayList<String>();

public int getRequiredXP() {
    return this.playerRank.requiredXP;
}

public long getRequiredXPBattlegrounds() {
    return this.bgRank.requiredXP;
}

public Date getDatePlayerBecameSupporter() {
    return this.dateBecameSupporter;
}

public boolean hasUnlockedEmblem(long givenEmblemID) {

    return this.unlockedEmblems.contains("emblem_" + givenEmblemID);

}

我使用以下代码从存储的json文件中获取球员个人资料数据:

    public static ObjectPlayerProfile getProfileFromFile(String givenUUID) throws IOException {

    File profileFile = new File(profilesDir + givenUUID + ".json");

    if (!doesProfileExist(givenUUID)) {
        createPlayerProfile(givenUUID);
    }

    if (doesProfileExist(givenUUID)) {
        ObjectPlayerProfile theProfile = objectMapper.readValue(profileFile,ObjectPlayerProfile.class);

        return theProfile;

    } else {

        return null;

    }

}

这是一个示例播放器配置文件.json文件:

{ "playerUUID": "dbc4ea0e-cb87-4ffc-b431-d9a8eec1428c", "playerKills": 0, "playerDeaths": 4, "zombieKills": 11, "discordAuthCode": "12500535dsfg", "discordID": "", "rankID": 0, "playerRank": "ZOMBIE_KILLER", "xp": 957, "lastMessage": "Jun 24, 2018 3:16:02 PM", "lastMessageSeconds": "Jun 24, 2018 3:44:28 PM", "dateCreated": "May 3, 2018 3:13:04 PM", "isSupporter": true, "dateBecameSupporter": "Mar 25, 2018 8:12:24 PM", "capeDesign": "CAPE_PURPLE", "xpBattlegrounds": 0, "bgWins": 1, "bgLosses": 0, "bgRank": "RECRUIT", "unlockedEmblems": [ "emblem_108", "emblem_109" ] }

但是使用getProfileFromFile方法时出现以下错误:

  

com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串“ Jun 24,2018 3:16:02 PM”中反序列化类型java.util.Date的值:不是有效的表示形式

有人在解析此特定日期格式时是否知道要使用的正确注释?预先感谢。

0 个答案:

没有答案