Last active
December 28, 2016 22:18
-
-
Save AndriiFed/cbe599febdb0f266708bccdcc9c78d4a to your computer and use it in GitHub Desktop.
Issue XML deserialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cashmachine.atmstorage; | |
import cashmachine.money.MoneyPack; | |
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.annotation.PropertyAccessor; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
public class AtmSafeFileJackson implements AtmSafe { | |
private String filename = "safe.json"; | |
public AtmSafeFileJackson() { | |
} | |
public AtmSafeFileJackson(String filename) { | |
this.filename = filename; | |
} | |
public void saveSafe(HashMap<String, ArrayList<MoneyPack>> safe) throws IOException { | |
ObjectMapper objectMapper = new ObjectMapper(); | |
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); | |
String safeStr = objectMapper.writeValueAsString(safe); | |
try (PrintWriter out = new PrintWriter(filename)) { | |
out.println(safeStr); | |
} catch (IOException exception) { | |
System.out.println("Error open file: " + filename + " " + exception); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public HashMap<String, ArrayList<MoneyPack>> loadSafe() throws IOException { | |
HashMap<String, ArrayList<MoneyPack>> moneyStorage = new HashMap<>(); | |
String safeStr; | |
ObjectMapper objectMapper = new ObjectMapper(); | |
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); | |
File file = new File(filename); | |
if (!file.exists()) { | |
saveSafe(moneyStorage); | |
return moneyStorage; | |
} | |
try (BufferedReader in = new BufferedReader(new FileReader(filename))) { | |
safeStr = in.readLine(); | |
if (!safeStr.isEmpty()) { | |
moneyStorage = objectMapper.readValue(safeStr, new TypeReference<HashMap<String, ArrayList<MoneyPack>>>() {}); | |
} | |
} catch (IOException exception) { | |
System.out.println("Error open file: " + filename + " " + exception); | |
} | |
return moneyStorage; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cashmachine.atmstorage; | |
import cashmachine.money.MoneyPack; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
public class AtmSafeFileJacksonXML implements AtmSafe { | |
private String filename = "safe.xml"; | |
public AtmSafeFileJacksonXML() { | |
} | |
public AtmSafeFileJacksonXML(String filename) { | |
this.filename = filename; | |
} | |
public void saveSafe(HashMap<String, ArrayList<MoneyPack>> safe) throws IOException { | |
ObjectMapper xmlMapper = new XmlMapper(); | |
try { | |
xmlMapper.writeValue(new File(filename), safe); | |
} catch (IOException exception) { | |
System.out.println("Error write to file: " + filename + " " + exception); | |
} | |
} | |
public HashMap<String, ArrayList<MoneyPack>> loadSafe() throws IOException { | |
HashMap<String, ArrayList<MoneyPack>> moneyStorage = new HashMap<>(); | |
String safeStr; | |
XmlMapper xmlMapper = new XmlMapper(); | |
File file = new File(filename); | |
if (!file.exists()) { | |
saveSafe(moneyStorage); | |
return moneyStorage; | |
} | |
try (BufferedReader in = new BufferedReader(new FileReader(filename))) { | |
safeStr = in.readLine(); | |
if (!safeStr.isEmpty()) { | |
moneyStorage = xmlMapper.readValue(safeStr, new TypeReference<HashMap<String, ArrayList<MoneyPack>>>() {}); | |
} | |
} catch (IOException exception) { | |
System.out.println("Error read from file: " + filename + " " + exception); | |
} | |
return moneyStorage; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Error read from file: safe-test.xml com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of cashmachine.money.MoneyPack: no String-argument constructor/factory method to deserialize from String value ('EUR') | |
at [Source: java.io.StringReader@646007f4; line: 1, column: 28] (through reference chain: java.util.HashMap["EUR"]->java.util.ArrayList[0]) | |
Error read from file: safe-test.xml com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of cashmachine.money.MoneyPack: no String-argument constructor/factory method to deserialize from String value ('EUR') | |
at [Source: java.io.StringReader@6b0c2d26; line: 1, column: 28] (through reference chain: java.util.HashMap["EUR"]->java.util.ArrayList[0]) | |
Error read from file: safe-test.xml com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of cashmachine.money.MoneyPack: no String-argument constructor/factory method to deserialize from String value ('EUR') | |
at [Source: java.io.StringReader@27c6e487; line: 1, column: 28] (through reference chain: java.util.HashMap["EUR"]->java.util.ArrayList[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cashmachine.money; | |
import java.io.Serializable; | |
public class MoneyPack implements Serializable { | |
private String currency; | |
private int value; | |
private int amount; | |
public MoneyPack() { | |
this.currency = "AAA"; | |
this.value = 0; | |
this.amount = 0; | |
} | |
public MoneyPack(String currency, int value, int amount) { | |
this.currency = currency; | |
this.value = value; | |
this.amount = amount; | |
} | |
public void setCurrency(String currency) { | |
this.currency = currency; | |
} | |
public void setValue(int value) { | |
this.value = value; | |
} | |
public String getCurrency() { | |
return currency; | |
} | |
public int getValue() { | |
return value; | |
} | |
public int getAmount() { | |
return amount; | |
} | |
public void setAmount(int amount) { | |
this.amount = amount; | |
} | |
@Override | |
public String toString() { | |
return (String) currency + " " + value + " " + amount; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"EUR":[{"currency":"EUR","value":500,"amount":34},{"currency":"EUR","value":100,"amount":32},{"currency":"EUR","value":50,"amount":30},{"currency":"EUR","value":10,"amount":28}],"USD":[{"currency":"USD","value":500,"amount":14},{"currency":"USD","value":100,"amount":12},{"currency":"USD","value":50,"amount":10},{"currency":"USD","value":10,"amount":8}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<HashMap><EUR><currency>EUR</currency><value>500</value><amount>34</amount></EUR><EUR><currency>EUR</currency><value>100</value><amount>32</amount></EUR><EUR><currency>EUR</currency><value>50</value><amount>30</amount></EUR><EUR><currency>EUR</currency><value>10</value><amount>28</amount></EUR><USD><currency>USD</currency><value>500</value><amount>14</amount></USD><USD><currency>USD</currency><value>100</value><amount>12</amount></USD><USD><currency>USD</currency><value>50</value><amount>10</amount></USD><USD><currency>USD</currency><value>10</value><amount>8</amount></USD></HashMap> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment