use of net.sergeych.biserializer.BiDeserializer in project universa by UniversaBlockchain.
the class CLIMain method importContract.
/**
* Import contract from specified yaml, xml or json file.
*
* @param sourceName
*
* @return loaded and from loaded data created Contract.
*/
private static Contract importContract(String sourceName) throws IOException {
ContractFileTypes fileType = getFileType(sourceName);
Contract contract = null;
File pathFile = new File(sourceName);
if (pathFile.exists()) {
try {
Binder binder;
FileReader reader = new FileReader(sourceName);
if (fileType == ContractFileTypes.YAML) {
Yaml yaml = new Yaml();
binder = Binder.convertAllMapsToBinders(yaml.load(reader));
} else if (fileType == ContractFileTypes.JSON) {
Gson gson = new GsonBuilder().create();
binder = Binder.convertAllMapsToBinders(gson.fromJson(reader, Binder.class));
} else {
XStream xstream = new XStream(new DomDriver());
xstream.registerConverter(new MapEntryConverter());
xstream.alias("contract", Binder.class);
binder = Binder.convertAllMapsToBinders(xstream.fromXML(reader));
}
BiDeserializer bm = DefaultBiMapper.getInstance().newDeserializer();
contract = new Contract();
contract.deserialize(binder, bm);
report(">>> imported contract: " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getCreatedAt()));
report("import from " + fileType.toString().toLowerCase() + " ok");
} catch (Exception e) {
addError(Errors.FAILURE.name(), sourceName, e.getMessage());
}
} else {
addError(Errors.NOT_FOUND.name(), sourceName, "Path " + sourceName + " does not exist");
// usage("Path " + sourceName + " does not exist");
}
return contract;
}
use of net.sergeych.biserializer.BiDeserializer in project universa by UniversaBlockchain.
the class CLIMain method updateFields.
/**
* Update fields for specified contract.
*
* @param contract - contract for update.
* @param fields - map of field names and values.
*/
private static void updateFields(Contract contract, HashMap<String, String> fields) throws IOException {
for (String fieldName : fields.keySet()) {
report("update field: " + fieldName + " -> " + fields.get(fieldName));
Binder binder = new Binder();
Binder data = null;
try {
XStream xstream = new XStream(new DomDriver());
xstream.registerConverter(new MapEntryConverter());
xstream.alias(fieldName, Binder.class);
data = Binder.convertAllMapsToBinders(xstream.fromXML(fields.get(fieldName)));
} catch (Exception xmlEx) {
// xmlEx.printStackTrace();
try {
Gson gson = new GsonBuilder().create();
binder = Binder.convertAllMapsToBinders(gson.fromJson(fields.get(fieldName), Binder.class));
data = (Binder) data.get(fieldName);
} catch (Exception jsonEx) {
// jsonEx.printStackTrace();
try {
Yaml yaml = new Yaml();
data = Binder.convertAllMapsToBinders(yaml.load(fields.get(fieldName)));
data = (Binder) data.get(fieldName);
} catch (Exception yamlEx) {
yamlEx.printStackTrace();
}
}
}
if (data != null) {
BiDeserializer bm = DefaultBiMapper.getInstance().newDeserializer();
binder.put("data", bm.deserialize(data));
contract.set(fieldName, binder);
report("update field " + fieldName + " ok");
} else {
report("update field " + fieldName + " error: no valid data");
}
}
report("contract expires at " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getExpiresAt()));
}
Aggregations