Skip to content

Instantly share code, notes, and snippets.

@jarryDk
Last active December 20, 2018 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarryDk/0a66ba7367adcb2643f41ebd20686043 to your computer and use it in GitHub Desktop.
Save jarryDk/0a66ba7367adcb2643f41ebd20686043 to your computer and use it in GitHub Desktop.
package dk.jarry.util;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.UUID;
import javax.json.Json;
import javax.json.JsonObject;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class Yaml2Json {
private Path sourcePath;
private Path targetPath;
private Boolean replace = Boolean.FALSE;
public Yaml2Json() {
}
public Yaml2Json(Path path) {
this.sourcePath = path;
}
public Yaml2Json(Path sourcePath, Path targetPath) {
this.sourcePath = sourcePath;
this.targetPath = targetPath;
}
public Yaml2Json(Path sourcePath, Path targetPath, Boolean replace) {
this.sourcePath = sourcePath;
this.targetPath = targetPath;
this.replace = replace;
}
public JsonObject getJson() throws JsonParseException, JsonMappingException, IOException {
JsonObject jsonObject = null;
if (sourcePath == null) {
throw new IllegalArgumentException("sourcePath is null");
}
if (!Files.exists(sourcePath)) {
throw new IOException(sourcePath.toString() + " is not on disk");
}
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Object object = mapper.readValue(sourcePath.toFile(), Object.class);
ObjectMapper jsonWriter = new ObjectMapper();
String jsonString = jsonWriter.writeValueAsString(object);
jsonObject = Json.createReader(new StringReader(jsonString)).readObject();
return jsonObject;
}
public JsonObject getJson(String yamlString) throws JsonParseException, JsonMappingException, IOException {
JsonObject jsonObject = null;
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Object object = mapper.readValue(yamlString, Object.class);
ObjectMapper jsonWriter = new ObjectMapper();
String jsonString = jsonWriter.writeValueAsString(object);
jsonObject = Json.createReader(new StringReader(jsonString)).readObject();
return jsonObject;
}
public String getJsonString() throws JsonParseException, JsonMappingException, IOException, ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine scriptEngine = manager.getEngineByName("nashorn");
scriptEngine.put("jsonString", getJson().toString());
scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 3)");
String prettyPrintedJson = (String) scriptEngine.get("result");
return prettyPrintedJson;
}
public void save() throws JsonParseException, JsonMappingException, IOException, ScriptException {
if (targetPath == null) {
targetPath = Paths.get(sourcePath.toString().replace(".yaml", ".json"));
}
if (Files.exists(targetPath)) {
if (replace) {
Files.delete(targetPath);
} else {
targetPath = Paths.get(targetPath.toString(), "_" + UUID.randomUUID().toString());
}
}
if (Files.exists(targetPath)) {
targetPath = Paths.get(targetPath.toString(), "_" + UUID.randomUUID().toString());
}
Files.write(targetPath, getJsonString().getBytes(), StandardOpenOption.CREATE);
}
}
/**
* Add to pom.xml
*
*<dependency>
* <groupId>com.fasterxml.jackson.dataformat</groupId>
* <artifactId>jackson-dataformat-yaml</artifactId>
* <version>2.9.5</version>
*</dependency>
*<dependency>
* <groupId>com.fasterxml.jackson.core</groupId>
* <artifactId>jackson-databind</artifactId>
* <version>2.9.5</version>
*</dependency>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment