Search in sources :

Example 26 with Representer

use of org.yaml.snakeyaml.representer.Representer in project swagger-parser by swagger-api.

the class DeserializationUtils method buildSnakeYaml.

public static org.yaml.snakeyaml.Yaml buildSnakeYaml(BaseConstructor constructor) {
    try {
        LoaderOptions.class.getMethod("getMaxAliasesForCollections");
    } catch (NoSuchMethodException e) {
        return new org.yaml.snakeyaml.Yaml(constructor);
    }
    try {
        LoaderOptions loaderOptions = new LoaderOptions();
        Method method = LoaderOptions.class.getMethod("setMaxAliasesForCollections", int.class);
        method.invoke(loaderOptions, options.getMaxYamlAliasesForCollections());
        method = LoaderOptions.class.getMethod("setAllowRecursiveKeys", boolean.class);
        method.invoke(loaderOptions, options.isYamlAllowRecursiveKeys());
        method = LoaderOptions.class.getMethod("setAllowDuplicateKeys", boolean.class);
        method.invoke(loaderOptions, false);
        org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(constructor, new Representer(), new DumperOptions(), loaderOptions, new CustomResolver());
        return yaml;
    } catch (ReflectiveOperationException e) {
        // 
        LOGGER.debug("using snakeyaml < 1.25, not setting YAML Billion Laughs Attack snakeyaml level protection");
    }
    return new org.yaml.snakeyaml.Yaml(constructor);
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Method(java.lang.reflect.Method) Yaml(io.swagger.v3.core.util.Yaml)

Example 27 with Representer

use of org.yaml.snakeyaml.representer.Representer in project BoofCV by lessthanoptimal.

the class CalibrationIO method loadOpenCV.

/**
 * Loads intrinsic parameters in OpenCV format.
 */
public static CameraPinholeBrown loadOpenCV(Reader reader) {
    CameraPinholeBrown out = new CameraPinholeBrown();
    try {
        String filtered = IOUtils.toString(reader);
        // It doesn't like the header or that class def
        filtered = filtered.replace("%YAML:1.0", "");
        filtered = filtered.replace("!!opencv-matrix", "");
        Representer representer = new Representer();
        representer.getPropertyUtils().setSkipMissingProperties(true);
        Yaml yaml = new Yaml(new Constructor(), representer);
        Map<String, Object> map = yaml.load(filtered);
        int width = getOrThrow(map, "image_width");
        int height = getOrThrow(map, "image_height");
        DMatrixRMaj K = loadOpenCVMatrix(getOrThrow(map, "camera_matrix"));
        DMatrixRMaj distortion = loadOpenCVMatrix(getOrThrow(map, "distortion_coefficients"));
        PerspectiveOps.matrixToPinhole(K, width, height, out);
        if (distortion.getNumElements() >= 5)
            out.setRadial(distortion.get(0), distortion.get(1), distortion.get(4));
        else if (distortion.getNumElements() >= 2)
            out.setRadial(distortion.get(0), distortion.get(1));
        if (distortion.getNumElements() >= 5)
            out.fsetTangental(distortion.get(2), distortion.get(3));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return out;
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) Constructor(org.yaml.snakeyaml.constructor.Constructor) DMatrixRMaj(org.ejml.data.DMatrixRMaj) Yaml(org.yaml.snakeyaml.Yaml)

Example 28 with Representer

use of org.yaml.snakeyaml.representer.Representer in project testcases by coheigea.

the class YamlTest method referencesWithRecursiveKeysNotAllowedByDefaultAtAPILevel.

@org.junit.Test
public void referencesWithRecursiveKeysNotAllowedByDefaultAtAPILevel() {
    String output = createDump(30);
    // Load
    LoaderOptions settings = new LoaderOptions();
    settings.setMaxAliasesForCollections(150);
    Yaml yaml = new Yaml(new CustomConstructor(), new Representer(), new DumperOptions(), settings);
    try {
        yaml.load(output);
        fail();
    } catch (Exception e) {
        assertEquals("Recursive key for mapping is detected but it is not configured to be allowed.", e.getMessage());
    }
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 29 with Representer

use of org.yaml.snakeyaml.representer.Representer in project today-framework by TAKETODAY.

the class YamlProcessor method createYaml.

/**
 * Create the {@link Yaml} instance to use.
 * <p>The default implementation sets the "allowDuplicateKeys" flag to {@code false},
 * enabling built-in duplicate key handling in SnakeYAML 1.18+.
 * <p>If custom {@linkplain #setSupportedTypes supported types}
 * have been configured, the default implementation creates
 * a {@code Yaml} instance that filters out unsupported types encountered in
 * YAML documents. If an unsupported type is encountered, an
 * {@link IllegalStateException} will be thrown when the node is processed.
 *
 * @see LoaderOptions#setAllowDuplicateKeys(boolean)
 */
protected Yaml createYaml() {
    LoaderOptions loaderOptions = new LoaderOptions();
    loaderOptions.setAllowDuplicateKeys(false);
    return new Yaml(new FilteringConstructor(loaderOptions), new Representer(), new DumperOptions(), loaderOptions);
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 30 with Representer

use of org.yaml.snakeyaml.representer.Representer in project today-framework by TAKETODAY.

the class OriginTrackedYamlLoader method createYaml.

private Yaml createYaml(LoaderOptions loaderOptions) {
    BaseConstructor constructor = new OriginTrackingConstructor(loaderOptions);
    Representer representer = new Representer();
    DumperOptions dumperOptions = new DumperOptions();
    LimitedResolver resolver = new LimitedResolver();
    return new Yaml(constructor, representer, dumperOptions, loaderOptions, resolver);
}
Also used : BaseConstructor(org.yaml.snakeyaml.constructor.BaseConstructor) Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Representer (org.yaml.snakeyaml.representer.Representer)82 Yaml (org.yaml.snakeyaml.Yaml)77 DumperOptions (org.yaml.snakeyaml.DumperOptions)54 Constructor (org.yaml.snakeyaml.constructor.Constructor)25 LoaderOptions (org.yaml.snakeyaml.LoaderOptions)21 FileInputStream (java.io.FileInputStream)12 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)11 IOException (java.io.IOException)7 Tag (org.yaml.snakeyaml.nodes.Tag)7 File (java.io.File)6 TypeDescription (org.yaml.snakeyaml.TypeDescription)6 BaseConstructor (org.yaml.snakeyaml.constructor.BaseConstructor)5 FileNotFoundException (java.io.FileNotFoundException)4 InputStream (java.io.InputStream)4 PropertyUtils (org.yaml.snakeyaml.introspector.PropertyUtils)4 FileOutputStream (java.io.FileOutputStream)3 FileReader (java.io.FileReader)3 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Map (java.util.Map)3