Search in sources :

Example 1 with YamlOutputStreamPrinter

use of com.bluenimble.platform.json.printers.YamlOutputStreamPrinter in project serverless by bluenimble.

the class SpecUtils method write.

public static void write(File apiFolder, JsonObject spec) throws CommandExecutionException {
    File fApi = new File(apiFolder, "api." + BlueNimble.SpecLangs.Yaml);
    if (fApi.exists()) {
        // yaml
        OutputStream out = null;
        try {
            out = new FileOutputStream(fApi);
            YamlPrinter yaml = new YamlOutputStreamPrinter(out);
            yaml.print(spec);
        } catch (Exception ex) {
            throw new CommandExecutionException(ex.getMessage(), ex);
        } finally {
            IOUtils.closeQuietly(out);
        }
    } else {
        fApi = new File(apiFolder, "api." + BlueNimble.SpecLangs.Json);
        if (fApi.exists()) {
            try {
                Json.store(spec, fApi);
            } catch (Exception ex) {
                throw new CommandExecutionException(ex.getMessage(), ex);
            }
        }
    }
}
Also used : YamlOutputStreamPrinter(com.bluenimble.platform.json.printers.YamlOutputStreamPrinter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) File(java.io.File) YamlPrinter(com.bluenimble.platform.json.printers.YamlPrinter) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 2 with YamlOutputStreamPrinter

use of com.bluenimble.platform.json.printers.YamlOutputStreamPrinter in project serverless by bluenimble.

the class SpecUtils method j2y.

@SuppressWarnings("unchecked")
public static void j2y(File fileOrFolder, boolean deleteSource) throws Exception {
    if (!fileOrFolder.exists()) {
        return;
    }
    if (fileOrFolder.isDirectory()) {
        File[] files = fileOrFolder.listFiles(new FileFilter() {

            @Override
            public boolean accept(File file) {
                return file.isDirectory() || file.getName().endsWith(".json");
            }
        });
        if (files == null || files.length == 0) {
            return;
        }
        for (File f : files) {
            j2y(f, deleteSource);
        }
    } else {
        File ymlFile = new File(fileOrFolder.getParentFile(), fileOrFolder.getName().substring(0, fileOrFolder.getName().lastIndexOf(Lang.DOT)) + ".yaml");
        OutputStream out = null;
        try {
            out = new FileOutputStream(ymlFile);
            new YamlOutputStreamPrinter(out).print(Json.load(fileOrFolder));
        } finally {
            IOUtils.closeQuietly(out);
        }
        // validate
        Yaml yaml = new Yaml();
        InputStream is = null;
        try {
            is = new FileInputStream(ymlFile);
            new JsonObject(yaml.loadAs(is, Map.class), true);
        } finally {
            IOUtils.closeQuietly(is);
        }
        if (deleteSource) {
            FileUtils.delete(fileOrFolder);
        }
    }
}
Also used : YamlOutputStreamPrinter(com.bluenimble.platform.json.printers.YamlOutputStreamPrinter) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) JsonObject(com.bluenimble.platform.json.JsonObject) FileFilter(java.io.FileFilter) File(java.io.File) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream)

Example 3 with YamlOutputStreamPrinter

use of com.bluenimble.platform.json.printers.YamlOutputStreamPrinter in project serverless by bluenimble.

the class YamlWriter method write.

@Override
public void write(ApiOutput output, ApiResponse response) throws IOException {
    response.flushHeaders();
    if (output == null) {
        response.write(Lang.BLANK);
        return;
    }
    JsonObject json = output.data();
    if (json == null) {
        response.write(Lang.BLANK);
        return;
    }
    new YamlOutputStreamPrinter(response.toOutput()).print(json);
}
Also used : YamlOutputStreamPrinter(com.bluenimble.platform.json.printers.YamlOutputStreamPrinter) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 4 with YamlOutputStreamPrinter

use of com.bluenimble.platform.json.printers.YamlOutputStreamPrinter in project serverless by bluenimble.

the class SpecUtils method toYaml.

public static void toYaml(String jsonText, OutputStream out) throws Exception {
    JsonObject json = new JsonObject(jsonText);
    YamlPrinter printer = new YamlOutputStreamPrinter(out);
    printer.print(json);
}
Also used : YamlOutputStreamPrinter(com.bluenimble.platform.json.printers.YamlOutputStreamPrinter) JsonObject(com.bluenimble.platform.json.JsonObject) YamlPrinter(com.bluenimble.platform.json.printers.YamlPrinter)

Aggregations

YamlOutputStreamPrinter (com.bluenimble.platform.json.printers.YamlOutputStreamPrinter)4 JsonObject (com.bluenimble.platform.json.JsonObject)3 YamlPrinter (com.bluenimble.platform.json.printers.YamlPrinter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)1 FileFilter (java.io.FileFilter)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 Yaml (org.yaml.snakeyaml.Yaml)1