io.swagger.parser.util.DeserializationUtils Maven / Gradle / Ivy
package io.swagger.parser.util;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
import java.io.IOException;
/**
* Created by russellb337 on 7/14/15.
*/
public class DeserializationUtils {
public static JsonNode deserializeIntoTree(String contents, String fileOrHost) {
JsonNode result;
try {
if (fileOrHost.endsWith(".yaml")) {
result = readYamlTree(contents);
} else {
result = Json.mapper().readTree(contents);
}
} catch (IOException e) {
throw new RuntimeException("An exception was thrown while trying to deserialize the contents of " + fileOrHost + " into a JsonNode tree", e);
}
return result;
}
public static T deserialize(Object contents, String fileOrHost, Class expectedType) {
T result;
boolean isJson = false;
if(contents instanceof String && contents.toString().trim().startsWith("{")) {
isJson = true;
}
try {
if (contents instanceof String) {
if (isJson) {
result = Json.mapper().readValue((String) contents, expectedType);
} else {
result = Yaml.mapper().readValue((String) contents, expectedType);
}
} else {
result = Json.mapper().convertValue(contents, expectedType);
}
} catch (Exception e) {
throw new RuntimeException("An exception was thrown while trying to deserialize the contents of " + fileOrHost + " into type " + expectedType, e);
}
return result;
}
public static JsonNode readYamlTree(String contents) {
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml();
return Json.mapper().convertValue(yaml.load(contents), JsonNode.class);
}
public static T readYamlValue(String contents, Class expectedType) {
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml();
return Json.mapper().convertValue(yaml.load(contents), expectedType);
}
}