2017-12-04 13 views
0

저는 jackson 2.9.2에서 deserialize하지 않으려 고합니다. 게임을 만들기 위해 노력하고 있습니다. uni 프로젝트에서 save 함수로 작업하고 있습니다.jackson deserializing 문제는 모든 변수를 null로 설정합니다.

public void LoadSaveString() throws IOException{ 
    ObjectMapper mapper = new ObjectMapper(); 
    SimpleModule module = new SimpleModule(); 
    //module.addDeserializer(SaveFile.class, new SaveDeserializer()); 
    mapper.registerModule(module); 

    String filePath = "files/SaveFile.json"; 
    BufferedReader reader = new BufferedReader(new FileReader(filePath)); 
    testfile = reader.readLine(); 

    System.out.println("Stringen testfilen bliver printet: " + testfile); 
    Player player1 = (Player) mapper.readValue(testfile, Player.class); 
    System.out.println("Test 2: " + player1.toString()); 

} 

과 : 어디에서 내 플레이어 클래스를 저장하고, 직렬화,하지만 난 그것을 역 직렬화하고, 그것에서 새로운 플레이어를 만들려고 할 때, 그것은 null로 모든 변수를 설정 할 수 있습니다, 이것은 나의 방법이다 난 얻을 출력된다 :

Stringen testfilen bliver printet { "플레이어"{ "HP"100 "공기"97 "재고"[] "hasCalledHelp"거짓 "wonGame" : 거짓, "playe false, "stopThreadHP": false, "name": "Mads"}}

(예 : "Mads", "Mads", "awesomePoints": 0, "totalTimePlayed": 3, "terminateThreads" 테스트 2 : 플레이어 {hasCalledHelp이 = 거짓 마력 = 0, 공기 = 0, 재고 = NULL, wonGame = 거짓, awesomePoints = 0, totalTimePlayed = 0, terminateThreads = 거짓, stopThreadOxygen = 거짓, stopThreadHP = 거짓}

그리고 난 알 수없는 특성을 무시하는 내 플레이어 클래스를 설정 한, 그래서는 얻을 그리고, playerclass

@JsonIgnoreProperties(ignoreUnknown = true) 

와의 속성에 중요한 일을 설정 내 플레이어 클래스 :

public class Player { 
private int hp; 
private int air; 
private ArrayList<Item> inventory; 
private boolean hasCalledHelp = false; 
private boolean wonGame = false; 
public String playerName = "Mads"; // Non-negotiable 
public int awesomePoints = 0; 
public int totalTimePlayed = 0; 

편집 : 파일 SaveFile.json에서

내용 :

{ 
    "player": { 
     "hp": 100, 
     "air": 97, 
     "inventory": [], 
     "hasCalledHelp": false, 
     "wonGame": false, 
     "playerName": "Mads", 
     "awesomePoints": 0, 
     "totalTimePlayed": 3, 
     "terminateThreads": false, 
     "stopThreadOxygen": false, 
     "stopThreadHP": false, 
     "name": "Mads" 
    } 
} 
+0

당신은 SaveFile.json의 내용을 게시 할 수 있을까요? – JanTheGun

+0

완료, 게시물을 편집, –

답변

0

우선 첫 줄을 읽지 않고 전체 파일을 문자열로 읽는 것이 좋습니다. 다른 형식의 JSON 파일이 지원되는지 확인하기 만하면됩니다. 또한 Tobias가 이미 언급했듯이 JSON 파일은 Player 만 포함하는 "Player"가 아닙니다. Tobias의 접근 방식 대신 JSON 파일에서 Player 부분을 선택할 수도 있습니다.

public void LoadSaveString() throws IOException { 
    ObjectMapper mapper = new ObjectMapper(); 
    SimpleModule module = new SimpleModule(); 
    mapper.registerModule(module); 

    String filePath = "files/SaveFile.json"; 

    //Read the whole file and not just one line 
    byte[] encoded = Files.readAllBytes(Paths.get(filePath)); 
    String testfile = new String(encoded, "utf-8"); 

    JSONObject json = new JSONObject(testfile); 
    //Pick only the player part 
    String playerPart = json.getJSONObject("player").toString(); 

    System.out.println("Stringen testfilen bliver printet: " + testfile); 
    Player player1 = (Player) mapper.readValue(playerPart, Player.class); 
    System.out.println("Test 2: " + player1.toString()); 

} 

또한 플레이어의 클래스는 각 속성에 대한 getter와 setter를 필요로 :

@JsonIgnoreProperties(ignoreUnknown = true) 
public class Player { 
    private int hp; 
    private int air; 
    private boolean hasCalledHelp = false; 
    private boolean wonGame = false; 
    public String playerName = "Mads"; // Non-negotiable 
    public int awesomePoints = 0; 
    public int totalTimePlayed = 0; 

    public int getHp() { 
     return hp; 
    } 
    public void setHp(int hp) { 
     this.hp = hp; 
    } 
    public int getAir() { 
     return air; 
    } 
    public void setAir(int air) { 
     this.air = air; 
    } 
    public boolean isHasCalledHelp() { 
     return hasCalledHelp; 
    } 
    public void setHasCalledHelp(boolean hasCalledHelp) { 
     this.hasCalledHelp = hasCalledHelp; 
    } 
    public boolean isWonGame() { 
     return wonGame; 
    } 
    public void setWonGame(boolean wonGame) { 
     this.wonGame = wonGame; 
    } 
    public String getPlayerName() { 
     return playerName; 
    } 
    public void setPlayerName(String playerName) { 
     this.playerName = playerName; 
    } 
    public int getAwesomePoints() { 
     return awesomePoints; 
    } 
    public void setAwesomePoints(int awesomePoints) { 
     this.awesomePoints = awesomePoints; 
    } 
    public int getTotalTimePlayed() { 
     return totalTimePlayed; 
    } 
    public void setTotalTimePlayed(int totalTimePlayed) { 
     this.totalTimePlayed = totalTimePlayed; 
    } 
} 
+0

감사합니다, 지금 작동합니다 –

0

파일의 JSON은 속성 player이있는 Object입니다. 이 같은 Player

{ 
    "player": { "hp":100, ... } 
}   

뭔가 아니다 다음 JSON은

{ 
    "hp":100, 
    "air":97, 
    "inventory":[], 
    "hasCalledHelp":false, 
    "wonGame":false, 
    "playerName":"Mads", 
    "awesomePoints":0, 
    "totalTimePlayed":3, 
    "terminateThreads":false, 
    "stopThreadOxygen":false, 
    "stopThreadHP":false, 
    "name":"Mads" 
} 

해야

public class PlayerWrapper { 
    public player Player; 
} 

또는 변경할 수없는 경우 파일은 PlayerWrapper 사용

mapper.readValue(testfile, PlayerWrapper.class)