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);
}
}
}
}
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);
}
}
}
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);
}
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);
}
Aggregations