Search in sources :

Example 6 with Representer

use of org.yaml.snakeyaml.representer.Representer in project pai by Microsoft.

the class YamlUtils method toObject.

// Bytes <-> Yaml
public static <T> T toObject(byte[] bytes, Class<T> classRef) {
    Representer representer = new Representer();
    representer.getPropertyUtils().setSkipMissingProperties(true);
    Yaml yaml = new Yaml(new Constructor(classRef), representer);
    return yaml.loadAs(new String(bytes), classRef);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) Constructor(org.yaml.snakeyaml.constructor.Constructor) Yaml(org.yaml.snakeyaml.Yaml)

Example 7 with Representer

use of org.yaml.snakeyaml.representer.Representer in project mxgwd by kamax-io.

the class YamlConfigLoader method loadFromFile.

public static Config loadFromFile(String path) throws IOException {
    Representer rep = new Representer();
    rep.getPropertyUtils().setAllowReadOnlyProperties(true);
    rep.getPropertyUtils().setSkipMissingProperties(true);
    Yaml yaml = new Yaml(new Constructor(Config.class), rep);
    Object o = yaml.load(new FileInputStream(path));
    return GsonUtil.get().fromJson(GsonUtil.get().toJson(o), Config.class);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) Constructor(org.yaml.snakeyaml.constructor.Constructor) Config(io.kamax.mxgwd.config.Config) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream)

Example 8 with Representer

use of org.yaml.snakeyaml.representer.Representer in project pom-manipulation-ext by release-engineering.

the class YamlTest method readYamlViaMap.

@Test
public void readYamlViaMap() throws ManipulationException, IOException {
    Representer representer = new Representer();
    representer.getPropertyUtils().setSkipMissingProperties(true);
    Yaml y = new Yaml(representer);
    Map config = (Map) y.load(new FileInputStream(yamlFile));
    Map usersConfig = (Map) config.get("pme");
    assertTrue(usersConfig.size() > 0);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 9 with Representer

use of org.yaml.snakeyaml.representer.Representer in project product-mi-tooling by wso2.

the class IOUtils method writeYamlFile.

/**
 * Writes k8 secret .yml file
 *
 * @param filePath file destination
 * @param yaml     K8SecretYaml object
 */
public static void writeYamlFile(String filePath, K8SecretYaml yaml) {
    DumperOptions options = new DumperOptions();
    // Configuration to hide the bean type (org.wso2.mi.tooling.security.output.K8SecretYaml)
    Representer representer = new Representer();
    representer.addClassTag(K8SecretYaml.class, Tag.MAP);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    options.setPrettyFlow(true);
    Yaml output = new Yaml(representer, options);
    try (FileWriter writer = new FileWriter(filePath)) {
        output.dump(yaml, writer);
        System.out.println("Kubernetes secret file created in " + filePath + " with default name and namespace");
        System.out.println("You can change the default values as required before applying.");
    } catch (IOException e) {
        throw new EncryptionToolException("Error while creating " + filePath);
    }
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) EncryptionToolException(org.wso2.mi.tooling.security.exception.EncryptionToolException) FileWriter(java.io.FileWriter) DumperOptions(org.yaml.snakeyaml.DumperOptions) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) K8SecretYaml(org.wso2.mi.tooling.security.output.K8SecretYaml)

Example 10 with Representer

use of org.yaml.snakeyaml.representer.Representer in project dockstore by dockstore.

the class DockstoreYamlHelper method readContent.

private static <T> T readContent(final String content, final Constructor constructor) throws DockstoreYamlException {
    try {
        // first check to make sure there aren't any unsafe types
        final Yaml safeYaml = new Yaml(new SafeConstructor());
        safeYaml.load(content);
        Representer representer = new Representer();
        representer.getPropertyUtils().setSkipMissingProperties(true);
        final Yaml yaml = new Yaml(constructor, representer);
        return yaml.load(content);
    } catch (Exception e) {
        final String exceptionMsg = e.getMessage();
        String errorMsg = ERROR_READING_DOCKSTORE_YML;
        errorMsg += exceptionMsg;
        LOG.error(errorMsg, e);
        throw new DockstoreYamlException(errorMsg);
    }
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) Yaml(org.yaml.snakeyaml.Yaml) PatternSyntaxException(java.util.regex.PatternSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Representer (org.yaml.snakeyaml.representer.Representer)75 Yaml (org.yaml.snakeyaml.Yaml)72 DumperOptions (org.yaml.snakeyaml.DumperOptions)50 Constructor (org.yaml.snakeyaml.constructor.Constructor)22 LoaderOptions (org.yaml.snakeyaml.LoaderOptions)21 FileInputStream (java.io.FileInputStream)11 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)11 IOException (java.io.IOException)7 TypeDescription (org.yaml.snakeyaml.TypeDescription)6 File (java.io.File)5 Tag (org.yaml.snakeyaml.nodes.Tag)5 FileNotFoundException (java.io.FileNotFoundException)4 InputStream (java.io.InputStream)4 BaseConstructor (org.yaml.snakeyaml.constructor.BaseConstructor)4 FileOutputStream (java.io.FileOutputStream)3 FileReader (java.io.FileReader)3 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 PropertyUtils (org.yaml.snakeyaml.introspector.PropertyUtils)3 Resolver (org.yaml.snakeyaml.resolver.Resolver)3