Search in sources :

Example 16 with YAMLMapper

use of com.fasterxml.jackson.dataformat.yaml.YAMLMapper in project carbon-apimgt by wso2.

the class CommonUtil method jsonToYaml.

/**
 * Converts JSON to YAML.
 *
 * @param json json representation
 * @return json file as a yaml document
 * @throws IOException If an error occurs while converting JSON to YAML
 */
public static String jsonToYaml(String json) throws IOException {
    ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory().enable(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
    JsonNode jsonNodeTree = yamlReader.readTree(json);
    YAMLMapper yamlMapper = new YAMLMapper().disable(YAMLGenerator.Feature.SPLIT_LINES).enable(YAMLGenerator.Feature.INDENT_ARRAYS).disable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE).disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER).enable(YAMLGenerator.Feature.MINIMIZE_QUOTES).enable(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS);
    return yamlMapper.writeValueAsString(jsonNodeTree);
}
Also used : YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 17 with YAMLMapper

use of com.fasterxml.jackson.dataformat.yaml.YAMLMapper in project logging-log4j2 by apache.

the class StackTraceElementYamlMixInTest method testFromYamlWithSimpleModule.

@Test
public void testFromYamlWithSimpleModule() throws Exception {
    final ObjectMapper mapper = new YAMLMapper();
    final SimpleModule module = new SimpleModule();
    module.addDeserializer(StackTraceElement.class, new Log4jStackTraceElementDeserializer());
    mapper.registerModule(module);
    final StackTraceElement expected = new StackTraceElement("package.SomeClass", "someMethod", "SomeClass.java", 123);
    final StackTraceElement actual = mapper.readValue("---\nclass: package.SomeClass\nmethod: someMethod\nfile: SomeClass.java\nline: 123\n...", StackTraceElement.class);
    Assert.assertEquals(expected, actual);
}
Also used : Log4jStackTraceElementDeserializer(org.apache.logging.log4j.jackson.Log4jStackTraceElementDeserializer) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Test(org.junit.Test)

Example 18 with YAMLMapper

use of com.fasterxml.jackson.dataformat.yaml.YAMLMapper in project fess by codelibs.

the class PluginHelper method loadArtifactsFromRepository.

protected List<Artifact> loadArtifactsFromRepository(final String url) {
    final String content = getRepositoryContent(url);
    final ObjectMapper objectMapper = new YAMLMapper();
    try {
        @SuppressWarnings("unchecked") final List<Map<?, ?>> result = objectMapper.readValue(content, List.class);
        if (result != null) {
            return result.stream().map(o -> new Artifact((String) o.get("name"), (String) o.get("version"), (String) o.get("url"))).collect(Collectors.toList());
        }
        return Collections.emptyList();
    } catch (final Exception e) {
        throw new PluginException("Failed to access " + url, e);
    }
}
Also used : LoadingCache(com.google.common.cache.LoadingCache) PluginException(org.codelibs.fess.exception.PluginException) CurlRequest(org.codelibs.curl.CurlRequest) IORuntimeException(org.lastaflute.di.exception.IORuntimeException) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ResourceUtil(org.codelibs.fess.util.ResourceUtil) StreamUtil.split(org.codelibs.core.stream.StreamUtil.split) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) Proxy(java.net.Proxy) Document(org.w3c.dom.Document) Map(java.util.Map) Node(org.w3c.dom.Node) CopyUtil(org.codelibs.core.io.CopyUtil) XMLConstants(javax.xml.XMLConstants) Path(java.nio.file.Path) NodeList(org.w3c.dom.NodeList) Files(java.nio.file.Files) StringUtil(org.codelibs.core.lang.StringUtil) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) Curl(org.codelibs.curl.Curl) TimeUnit(java.util.concurrent.TimeUnit) CacheLoader(com.google.common.cache.CacheLoader) Constants(org.codelibs.fess.crawler.Constants) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Paths(java.nio.file.Paths) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) Pattern(java.util.regex.Pattern) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) CacheBuilder(com.google.common.cache.CacheBuilder) Comparator(java.util.Comparator) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) CurlResponse(org.codelibs.curl.CurlResponse) InputStream(java.io.InputStream) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) PluginException(org.codelibs.fess.exception.PluginException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PluginException(org.codelibs.fess.exception.PluginException) IORuntimeException(org.lastaflute.di.exception.IORuntimeException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)18 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 IOException (java.io.IOException)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 InputStream (java.io.InputStream)5 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Map (java.util.Map)2 Test (org.junit.Test)2 MutableDependencyGraph (com.blackducksoftware.integration.hub.bdio.graph.MutableDependencyGraph)1 MutableMapDependencyGraph (com.blackducksoftware.integration.hub.bdio.graph.MutableMapDependencyGraph)1 Dependency (com.blackducksoftware.integration.hub.bdio.model.dependency.Dependency)1 NameVersionNode (com.blackducksoftware.integration.hub.detect.nameversion.NameVersionNode)1 NodeMetadata (com.blackducksoftware.integration.hub.detect.nameversion.NodeMetadata)1 SubcomponentNodeBuilder (com.blackducksoftware.integration.hub.detect.nameversion.builder.SubcomponentNodeBuilder)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1