Search in sources :

Example 51 with Yaml

use of org.yaml.snakeyaml.Yaml in project pai by Microsoft.

the class YamlUtils method toObject.

public static <T> T toObject(String fileName, Class<T> classRef) throws FileNotFoundException {
    Representer representer = new Representer();
    representer.getPropertyUtils().setSkipMissingProperties(true);
    Yaml yaml = new Yaml(new Constructor(classRef), representer);
    return yaml.loadAs(new FileReader(fileName), classRef);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) Constructor(org.yaml.snakeyaml.constructor.Constructor) FileReader(java.io.FileReader) Yaml(org.yaml.snakeyaml.Yaml)

Example 52 with Yaml

use of org.yaml.snakeyaml.Yaml in project be5 by DevelopmentOnTheEdge.

the class YamlSerializer method print.

/**
 * This method not just prints the tree. It transforms the tree using following rules:<br>
 * - any list of strings with one element is transformed to string;<br>
 * - 'true' and 'false' string values is transformed to boolean values.<br>
 *
 * @param root
 * @return
 */
static String print(Object root) {
    final DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    options.setDefaultScalarStyle(ScalarStyle.PLAIN);
    options.setPrettyFlow(false);
    final Object transformed = new TreeTransformer().transform(root);
    return new Yaml(new Constructor(), new YamlRepresenter(transformed), options).dump(transformed);
}
Also used : Constructor(org.yaml.snakeyaml.constructor.Constructor) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 53 with Yaml

use of org.yaml.snakeyaml.Yaml in project be5 by DevelopmentOnTheEdge.

the class ModuleLoader2 method getProjectName.

@SuppressWarnings("unchecked")
private static String getProjectName(URL url) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
    Map<String, Object> module = (Map<String, Object>) new Yaml().load(reader);
    return module.entrySet().iterator().next().getKey();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml)

Example 54 with Yaml

use of org.yaml.snakeyaml.Yaml in project be5 by DevelopmentOnTheEdge.

the class ReadModelFromXmlTest method testWriteReadConnectionProfile.

/**
 * Unexpected error when serializing 'connectionUrl' of TestProject/Connection profiles/Local/test
 * on windows
 * @throws Exception
 */
@Test
public void testWriteReadConnectionProfile() throws Exception {
    Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
    final Project project = new Project("TestProject");
    BeConnectionProfile profile = new BeConnectionProfile("test", project.getConnectionProfiles().getLocalProfiles());
    profile.setConnectionUrl("jdbc:db2://localhost:50000/housing:retrieveMessagesFromServerOnGetMessage=true;");
    profile.setUsername("test");
    profile.setPassword("password");
    LinkedHashMap<String, Object> serializedProfiles = new LinkedHashMap<>();
    serializedProfiles.put(profile.getName(), YamlSerializer.serializeProfile(profile));
    String serialized = new Yaml().dump(serializedProfiles);
    LoadContext loadContext = new LoadContext();
    BeConnectionProfile createdProfile = YamlDeserializer.deserializeConnectionProfile(loadContext, serialized, project);
    assertNotNull(createdProfile);
    if (!loadContext.getWarnings().isEmpty())
        throw loadContext.getWarnings().get(0);
    assertEquals(profile.getConnectionUrl(), createdProfile.getConnectionUrl());
    assertEquals(profile.getUsername(), createdProfile.getUsername());
}
Also used : Project(com.developmentontheedge.be5.metadata.model.Project) BeConnectionProfile(com.developmentontheedge.be5.metadata.model.BeConnectionProfile) LoadContext(com.developmentontheedge.be5.metadata.serialization.LoadContext) Yaml(org.yaml.snakeyaml.Yaml) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 55 with Yaml

use of org.yaml.snakeyaml.Yaml in project be5 by DevelopmentOnTheEdge.

the class Templates method getTemplatesProject.

public static Project getTemplatesProject() throws ReadException {
    Project prj = new Project(TEMPLATES_PROJECT_NAME, true);
    LoadContext lc = new LoadContext();
    for (String template : TEMPLATES) {
        URL url = Templates.class.getResource("templates/" + template + ".yaml");
        Node content;
        try (InputStream is = url.openStream()) {
            content = new Yaml().compose(new InputStreamReader(is, StandardCharsets.UTF_8));
        } catch (MarkedYAMLException e) {
            throw new ReadException(new Exception((e.getProblemMark().getLine() + 1) + ":" + (e.getProblemMark().getColumn() + 1) + ": " + e.getMessage()), getPath(url), ReadException.LEE_INVALID_STRUCTURE);
        } catch (YAMLException | IOException e) {
            throw new ReadException(new Exception(e.getMessage()), getPath(url), ReadException.LEE_INVALID_STRUCTURE);
        }
        try {
            Object obj = Serialization.derepresent(content);
            @SuppressWarnings("unchecked") Map<String, Object> root = (Map<String, Object>) obj;
            @SuppressWarnings("unchecked") Map<String, Object> entityContent = (Map<String, Object>) root.get(template);
            DataElementUtils.saveQuiet(YamlDeserializer.readEntity(lc, template, entityContent, prj.getApplication()));
        } catch (RuntimeException e) {
            throw new ReadException(e, getPath(url), ReadException.LEE_INTERNAL_ERROR);
        }
        lc.check();
    }
    return prj;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Node(org.yaml.snakeyaml.nodes.Node) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException) YAMLException(org.yaml.snakeyaml.error.YAMLException) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException) IOException(java.io.IOException) URL(java.net.URL) Yaml(org.yaml.snakeyaml.Yaml) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException) YAMLException(org.yaml.snakeyaml.error.YAMLException) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) LoadContext(com.developmentontheedge.be5.metadata.serialization.LoadContext) Map(java.util.Map)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)276 Map (java.util.Map)104 HashMap (java.util.HashMap)85 IOException (java.io.IOException)58 FileInputStream (java.io.FileInputStream)49 InputStream (java.io.InputStream)49 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)42 Constructor (org.yaml.snakeyaml.constructor.Constructor)30 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)22 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)22 List (java.util.List)21 Writer (java.io.Writer)18 Path (java.nio.file.Path)17 LinkedHashMap (java.util.LinkedHashMap)17 Reader (java.io.Reader)16 Properties (java.util.Properties)14 InputStreamReader (java.io.InputStreamReader)13