Search in sources :

Example 1 with BiDeserializer

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;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) XStream(com.thoughtworks.xstream.XStream) Gson(com.google.gson.Gson) BiDeserializer(net.sergeych.biserializer.BiDeserializer) Yaml(org.yaml.snakeyaml.Yaml) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException) Binder(net.sergeych.tools.Binder) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) Contract(com.icodici.universa.contract.Contract)

Example 2 with BiDeserializer

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()));
}
Also used : Binder(net.sergeych.tools.Binder) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) GsonBuilder(com.google.gson.GsonBuilder) XStream(com.thoughtworks.xstream.XStream) Gson(com.google.gson.Gson) BiDeserializer(net.sergeych.biserializer.BiDeserializer) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 XStream (com.thoughtworks.xstream.XStream)2 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)2 BackingStoreException (java.util.prefs.BackingStoreException)2 OptionException (joptsimple.OptionException)2 BiDeserializer (net.sergeych.biserializer.BiDeserializer)2 Binder (net.sergeych.tools.Binder)2 Yaml (org.yaml.snakeyaml.Yaml)2 Contract (com.icodici.universa.contract.Contract)1