Java Code Examples for org.yaml.snakeyaml.Yaml#load()

The following examples show how to use org.yaml.snakeyaml.Yaml#load() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public Map<String, Object> load(InputStream is) throws IOException {
  checkNotNull(is);
  String data = IOUtils.toString(new UnicodeReader(is));

  Map<String, Object> map;

  try {
    Yaml yaml = new Yaml(new Constructor(), new Representer(),
        new DumperOptions(), new Resolver());
    map = yaml.load(data);
  }
  catch (YAMLException e) {
    map = (Map<String, Object>) mapper.readValue(data, Map.class);
  }
  return map;
}
 
Example 2
Source Project: helix   File: HelixRestMain.java    License: Apache License 2.0 6 votes vote down vote up
private static void constructNamespaceFromConfigFile(String filePath, List<HelixRestNamespace> namespaces)
    throws IOException {
  Yaml yaml = new Yaml();
  @SuppressWarnings("unchecked")
  ArrayList<Map<String, String>> configs =
      (ArrayList<Map<String, String>>) yaml.load(new FileInputStream(new File(filePath)));
  for (Map<String, String> config : configs) {
    // Currently we don't support adding default namespace through yaml manifest so all
    // namespaces created here will not be default
    // TODO: support specifying default namespace from config file
    namespaces.add(new HelixRestNamespace(
        config.get(HelixRestNamespace.HelixRestNamespaceProperty.NAME.name()),
        HelixRestNamespace.HelixMetadataStoreType.valueOf(
            config.get(HelixRestNamespace.HelixRestNamespaceProperty.METADATA_STORE_TYPE.name())),
        config.get(HelixRestNamespace.HelixRestNamespaceProperty.METADATA_STORE_ADDRESS.name()),
        false, Boolean.parseBoolean(
        config.get(HelixRestNamespace.HelixRestNamespaceProperty.MULTI_ZK_ENABLED.name())),
        config.get(HelixRestNamespace.HelixRestNamespaceProperty.MSDS_ENDPOINT.name())));
  }
}
 
Example 3
Map<String, String> read() throws IOException {
    String userHome = System.getProperty("user.home");
    Path cbProfileLocation = Paths.get(userHome, ".dp", "config");
    if (!Files.exists(cbProfileLocation)) {
        LOGGER.info("Could not find cb profile file at location {}, falling back to application.yml", cbProfileLocation);
        return Map.of();
    }

    byte[] encoded = Files.readAllBytes(Paths.get(userHome, ".dp", "config"));
    String profileString = new String(encoded, Charset.defaultCharset());

    Yaml yaml = new Yaml();
    Map<String, Object> profiles = yaml.load(profileString);

    return (Map<String, String>) profiles.get(profile);
}
 
Example 4
Source Project: iot-java   File: DeviceConfig.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static DeviceConfig generateFromConfig(String fileName) throws FileNotFoundException {
	Yaml yaml = new Yaml();
	InputStream inputStream = new FileInputStream(fileName);
	Map<String, Object> yamlContents = yaml.load(inputStream);

	if (yamlContents.get("identity") instanceof Map<?, ?>) {
		if (yamlContents.get("auth") instanceof Map<?, ?>) {
			if (yamlContents.get("options") instanceof Map<?, ?>) {
				DeviceConfig cfg = new DeviceConfig(
						DeviceConfigIdentity.generateFromConfig((Map<String, Object>) yamlContents.get("identity")),
						DeviceConfigAuth.generateFromConfig((Map<String, Object>) yamlContents.get("auth")),
						DeviceConfigOptions.generateFromConfig((Map<String, Object>) yamlContents.get("options")));
				return cfg;
			}
			// else options is missing or in the wrong format
		}
		// else auth is missing or in the wrong format
	}
	// else identity is missing or in the wrong format
	return null;
}
 
Example 5
Source Project: hangout   File: TestLowercaseFilter.java    License: MIT License 5 votes vote down vote up
@Test
public void testLowercaseFilter() {
    String c = String.format("%s\n%s\n%s\n%s",
            "fields:",
            "    - name",
            "    - '[metric][value]'",
            "    - '[metric][value2]'"
    );

    Yaml yaml = new Yaml();
    Map config = (Map) yaml.load(c);
    Assert.assertNotNull(config);

    Lowercase lowercaseFilter = new Lowercase(config);

    // Match
    Map event = new HashMap();
    event.put("name", "Liujia");
    event.put("metric", new HashMap() {{
        this.put("value", "Hello");
        this.put("value3", 10);
    }});

    event = lowercaseFilter.process(event);
    Assert.assertEquals(event.get("name"), "liujia");
    Assert.assertEquals(((Map) event.get("metric")).get("value"), "hello");
    Assert.assertNull(((Map) event.get("metric")).get("value2"));
    Assert.assertEquals(((Map) event.get("metric")).get("value3"),10);
}
 
Example 6
Source Project: snake-yaml   File: LongTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testLongConstructor() {
    String doc = "!!org.yaml.snakeyaml.javabeans.LongTest$Foo\n\"bar\": !!int \"42\"";
    // System.out.println(doc);
    Yaml yaml = new Yaml();
    Foo foo2 = (Foo) yaml.load(doc);
    assertEquals(new Long(42L), foo2.getBar());
}
 
Example 7
private static Map<?, ?> readConfig() throws IOException {
	final Yaml yaml = new Yaml();
	try (final InputStream fis = new FileInputStream(PAINTERA_YAML.toFile())) {
		// TODO is this cast always safe?
		// TODO make this type safe, maybe create config class
		final Map<?, ?> data = (Map<?, ?>) yaml.load(fis);
		LOG.debug("Loaded paintera info: {}", data);
		return data;
	} catch (final FileNotFoundException e) {
		LOG.debug("Paintera config file not found: {}", e.getMessage());
	}
	return new HashMap<>();
}
 
Example 8
public void testSafeConstruct() {
    String doc = "- 5\n- !org.yaml.snakeyaml.constructor.Person\n  firstName: Andrey\n  age: 99\n- true";
    Yaml yaml = new Yaml(new SafeConstructor());
    try {
        yaml.load(doc);
        fail("Custom Java classes should not be created.");
    } catch (Exception e) {
        assertEquals(
                "could not determine a constructor for the tag !org.yaml.snakeyaml.constructor.Person\n"
                        + " in 'string', line 2, column 3:\n"
                        + "    - !org.yaml.snakeyaml.constructor. ... \n" + "      ^\n",
                e.getMessage());
    }
}
 
Example 9
Source Project: snake-yaml   File: LoadExampleTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testLoadFromStream() throws IOException {
    InputStream input = new FileInputStream(new File("src/test/resources/reader/utf-8.txt"));
    Yaml yaml = new Yaml();
    Object data = yaml.load(input);
    assertEquals("test", data);
    //
    data = yaml.load(new ByteArrayInputStream("test2".getBytes("UTF-8")));
    assertEquals("test2", data);
    input.close();
}
 
Example 10
@BeforeMethod
public void beforeMethod() throws Exception {
  Yaml yaml = new Yaml();
  try (Reader dataReader = new InputStreamReader(this.getClass().getResourceAsStream("data-sources-config.yml"))) {
    // NOTE: Yes, static typing does this to you.
    Map<String, List<Map<String, Map<String, Object>>>> config = (Map<String, List<Map<String, Map<String, Object>>>>) yaml.load(dataReader);
    this.dataSource = new MockThirdEyeDataSource(config.get("dataSourceConfigs").get(0).get("properties"));
  }
}
 
Example 11
public void testConstructor() {
    Yaml yaml = new Yaml(new SelectiveConstructor());
    List<?> data = (List<?>) yaml
            .load("- 1\n- 2\n- !!examples.MyPersistentObject {amount: 222, id: persistent}");
    // System.out.println(data);
    assertEquals(3, data.size());
    MyPersistentObject myObject = (MyPersistentObject) data.get(2);
    assertEquals(17, myObject.getAmount());
    assertEquals("persistent", myObject.getId());
}
 
Example 12
Source Project: snake-yaml   File: UniqueKeyTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testNotUnique() {
    String data = "{key: 1, key: 2}";
    Yaml yaml = new Yaml(new UniqueKeyConstructor());
    try {
        yaml.load(data);
        fail("The same key must be rejected");
    } catch (Exception e) {
        assertEquals("The key is not unique key", e.getMessage());
    }
}
 
Example 13
/**
 * standard Yaml
 */
public void testEmptyBean() {
    Yaml yaml = new Yaml();
    EmptyBean bean = (EmptyBean) yaml
            .load("!!org.yaml.snakeyaml.javabeans.ConstructEmptyBeanTest$EmptyBean {}");
    assertNotNull(bean);
    assertNull(bean.getFirstName());
    assertEquals(5, bean.getHatSize());
}
 
Example 14
private Map readImageStreamDescriptor(File target) throws FileNotFoundException {
    Yaml yaml = new Yaml();
    InputStream ios = new FileInputStream(target);
    // Parse the YAML file and return the output as a series of Maps and Lists
    return (Map<String,Object>) yaml.load(ios);
}
 
Example 15
Source Project: snake-yaml   File: HumanTest.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void testChildrenListRoot() {
    Human3 father = new Human3();
    father.setName("Father");
    father.setBirthday(new Date(1000000000));
    father.setBirthPlace("Leningrad");
    father.setBankAccountOwner(father);
    //
    Human3 mother = new Human3();
    mother.setName("Mother");
    mother.setBirthday(new Date(100000000000L));
    mother.setBirthPlace("Saint-Petersburg");
    father.setPartner(mother);
    mother.setPartner(father);
    mother.setBankAccountOwner(father);
    //
    Human3 son = new Human3();
    son.setName("Son");
    son.setBirthday(new Date(310000000000L));
    son.setBirthPlace("Munich");
    son.setBankAccountOwner(father);
    son.setFather(father);
    son.setMother(mother);
    //
    Human3 daughter = new Human3();
    daughter.setName("Daughter");
    daughter.setBirthday(new Date(420000000000L));
    daughter.setBirthPlace("New York");
    daughter.setBankAccountOwner(father);
    daughter.setFather(father);
    daughter.setMother(mother);
    //
    ArrayList<Human3> children = new ArrayList<Human3>();
    children.add(son);
    children.add(daughter);
    father.setChildren(children);
    mother.setChildren(children);
    //

    Constructor constructor = new Constructor();
    TypeDescription Human3Description = new TypeDescription(Human3.class);
    Human3Description.putListPropertyType("children", Human3.class);
    constructor.addTypeDescription(Human3Description);

    Yaml yaml = new Yaml(constructor);
    String output = yaml.dump(father.getChildren());
    // System.out.println(output);
    String etalon = Util.getLocalResource("recursive/with-children-as-list.yaml");
    assertEquals(etalon, output);
    //
    List<Human3> children2 = (List<Human3>) yaml.load(output);
    assertNotNull(children2);
    Human3 son2 = children2.iterator().next();
    assertEquals(2, children2.size());

    Human3 father2 = son2.getFather();
    assertEquals("Father", father2.getName());
    assertEquals("Mother", son2.getMother().getName());
    assertSame(father2, father2.getBankAccountOwner());
    assertSame(father2.getPartner(), son2.getMother());
    assertSame(father2, son2.getMother().getPartner());

    assertSame(father2.getPartner().getChildren(), children2);

    for (Object child : children2) {
        // check if type descriptor was correct
        assertSame(Human3.class, child.getClass());
    }
}
 
Example 16
Source Project: snake-yaml   File: ShapeImmutableTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testPoint3d() {
    Yaml yaml = new Yaml();
    Point3d loaded = (Point3d) yaml
            .load("!!org.yaml.snakeyaml.immutable.Point3d [!!org.yaml.snakeyaml.immutable.Point [1.17, 3.14], 345.1]");
    assertEquals(345.1, loaded.getZ());
}
 
Example 17
Source Project: Drop-seq   File: MergeDgeSparseTest.java    License: MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test(dataProvider = "testBasicDataProvider")
public void testBasic(final String testName,
                      final int expectedNumGenes,
                      final int expectedNumCells,
                      final int[] cell_count,
                      final String[] prefix,
                      final String[] filteredGeneRe,
                      final Integer min_cells,
                      final Integer min_genes,
                      final Integer min_transcripts,
                      final DgeHeaderMerger.Stringency headerStringency,
                      final List<File> selectedCellsFiles) throws IOException {
    final File tempDir = Files.createTempDirectory("MergeDgeSparseTest.").toFile();
    tempDir.deleteOnExit();

    final Yaml yamlConverter = new Yaml();
    final Map yamlMap = (Map)yamlConverter.load(IOUtil.openFileForReading(YAML));
    final List datasets = (List)yamlMap.get(MergeDgeSparse.YamlKeys.DATASETS_KEY);

    if (cell_count != null) {
        for (int i = 0; i < cell_count.length; ++i) {
            ((Map)datasets.get(i)).put(MergeDgeSparse.YamlKeys.DatasetsKeys.CELL_COUNT_KEY, cell_count[i]);
        }
    }
    if (prefix != null) {
        for (int i = 0; i < prefix.length; ++i) {
            ((Map)datasets.get(i)).put(MergeDgeSparse.YamlKeys.DatasetsKeys.NAME_KEY, prefix[i]);
        }
    }
    final File outputYaml = new File(tempDir, "test.yaml");
    outputYaml.deleteOnExit();
    final BufferedWriter outputYamlWriter = IOUtil.openFileForBufferedWriting(outputYaml);
    yamlConverter.dump(yamlMap, outputYamlWriter);
    outputYamlWriter.close();

    final File cellSizeOutputFile = new File(tempDir, "test.cell_size.txt");    cellSizeOutputFile.deleteOnExit();
    final File dgeHeaderOutputFile = new File(tempDir, "test.dge_header.txt");  dgeHeaderOutputFile.deleteOnExit();
    final File rawDgeOutputFile = new File(tempDir, "test.raw.dge.txt");        rawDgeOutputFile.deleteOnExit();
    final File scaledDgeOutputFile = new File(tempDir, "test.scaled.dge.txt");  scaledDgeOutputFile.deleteOnExit();
    final File discardedCellsOutputFile = new File(tempDir, "test.discarded_cells.txt"); discardedCellsOutputFile.deleteOnExit();

    final MergeDgeSparse merger = new MergeDgeSparse();
    merger.YAML = outputYaml;
    merger.CELL_SIZE_OUTPUT_FILE = cellSizeOutputFile;
    if (prefix != null) {
        // If not setting prefixes, can't create merged header
        merger.DGE_HEADER_OUTPUT_FILE = dgeHeaderOutputFile;
    }
    merger.RAW_DGE_OUTPUT_FILE = rawDgeOutputFile;
    merger.SCALED_DGE_OUTPUT_FILE = scaledDgeOutputFile;
    merger.HEADER_STRINGENCY = headerStringency;

    if (min_cells != null) {
        merger.MIN_CELLS = min_cells;
    }
    if (min_genes != null) {
        merger.MIN_GENES = min_genes;
    }
    if (min_transcripts != null) {
        merger.MIN_TRANSCRIPTS = min_transcripts;
    }
    if (filteredGeneRe != null) {
        merger.FILTERED_GENE_RE = Arrays.asList(filteredGeneRe);
    } else {
        merger.FILTERED_GENE_RE = Collections.emptyList();
    }
    merger.CELL_BC_FILE = selectedCellsFiles;
    merger.DISCARDED_CELLS_FILE = discardedCellsOutputFile;

    Assert.assertEquals(merger.doWork(), 0);

    final DGEMatrix rawMatrix = DGEMatrix.parseFile(rawDgeOutputFile);
    final DGEMatrix scaledMatrix = DGEMatrix.parseFile(scaledDgeOutputFile);
    Assert.assertEquals(rawMatrix.getGenes().size(), scaledMatrix.getGenes().size(), "nrow(raw matrix) != nrow(scaled matrix)");
    Assert.assertEquals(rawMatrix.getCellBarcodes().size(), scaledMatrix.getCellBarcodes().size(), "ncol(raw matrix) != ncol(scaled matrix)");
    Assert.assertEquals(rawMatrix.getGenes().size(), expectedNumGenes);
    Assert.assertEquals(rawMatrix.getCellBarcodes().size(), expectedNumCells);
}
 
Example 18
Source Project: snake-yaml   File: ShapeImmutableTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testColor() {
    Yaml yaml = new Yaml();
    Color loaded = (Color) yaml.load("!!org.yaml.snakeyaml.immutable.Color BLACK");
    assertEquals("BLACK", loaded.getName());
}
 
Example 19
private Map<?, ?> parseYaml(String line) {
	Yaml yaml = new Yaml();
	Object obj = yaml.load(line);
	return extractMapFromYaml(obj);
}
 
Example 20
public Map<String, MonitoringRules> extract(Reader reader) throws JAXBException {
    
    Yaml yaml = new Yaml();
    Object doc = yaml.load(reader);
    
    return extract((Map)doc);
}