Java Code Examples for org.yaml.snakeyaml.DumperOptions#setDefaultScalarStyle()

The following examples show how to use org.yaml.snakeyaml.DumperOptions#setDefaultScalarStyle() . 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
Source File: LongTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testLongRepresenter() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    Representer repr = new Representer();
    repr.addClassTag(Long.class, new Tag("!!java.lang.Long"));
    Yaml yaml = new Yaml(repr, options);

    Foo foo = new Foo();
    String output = yaml.dump(foo);
    // System.out.println(output);
    Foo foo2 = (Foo) yaml.load(output);
    assertEquals(new Long(42L), foo2.getBar());
}
 
Example 2
Source File: ClusterDefinitionService.java    From karamel with Apache License 2.0 6 votes vote down vote up
public static String jsonToYaml(JsonCluster jsonCluster) throws KaramelException {
  YamlCluster yamlCluster = new YamlCluster(jsonCluster);
  DumperOptions options = new DumperOptions();
  options.setIndent(2);
  options.setWidth(120);
  options.setExplicitEnd(false);
  options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  options.setPrettyFlow(true);
  options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
  YamlPropertyRepresenter yamlPropertyRepresenter = new YamlPropertyRepresenter();
  yamlPropertyRepresenter.addClassTag(YamlCluster.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Ec2.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Baremetal.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Gce.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Nova.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Occi.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Cookbook.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(YamlGroup.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(HashSet.class, Tag.MAP);
  Yaml yaml = new Yaml(yamlPropertyRepresenter, options);
  String content = yaml.dump(yamlCluster);
  return content;
}
 
Example 3
Source File: SafeRepresenterTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testStyle() {
    List<Integer> list = new ArrayList<Integer>();
    list.add(new Integer(1));
    list.add(new Integer(1));
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("list", list);
    map.put("name", "Ubuntu");
    map.put("age", 5);
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    assertTrue(output.contains("\"age\": !!int \"5\""));
    assertTrue(output.contains("\"name\": \"Ubuntu\""));
    assertTrue(output.contains("- !!int \"1\""));
}
 
Example 4
Source File: SafeRepresenterTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testStyle2() {
    List<Integer> list = new ArrayList<Integer>();
    list.add(new Integer(1));
    list.add(new Integer(1));
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("age", 5);
    map.put("name", "Ubuntu");
    map.put("list", list);
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    assertEquals("{'age': !!int '5', 'name': 'Ubuntu', 'list': [!!int '1', !!int '1']}\n",
            output);
}
 
Example 5
Source File: SafeRepresenterTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testStyle2Pretty() {
    List<Integer> list = new ArrayList<Integer>();
    list.add(new Integer(1));
    list.add(new Integer(1));
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("age", 5);
    map.put("name", "Ubuntu");
    map.put("list", list);
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    options.setPrettyFlow(true);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    assertEquals(
            "{\n  'age': !!int '5',\n  'name': 'Ubuntu',\n  'list': [\n    !!int '1',\n    !!int '1']\n  \n}\n",
            output);
}
 
Example 6
Source File: EmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testWriteSingleQuoted() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.SINGLE_QUOTED);
    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon = "'aaa': '0123456789 0123456789\n\n  0123456789 0123456789'\n'bbb': '\n\n  bla-bla'\n";
    assertEquals(etalon, output);
}
 
Example 7
Source File: YamlConfigSource.java    From smallrye-config with Apache License 2.0 5 votes vote down vote up
private static void flattenList(String key, List<Object> source, Map<String, String> target) {
    if (source.stream().allMatch(o -> o instanceof String)) {
        target.put(key, source.stream().map(o -> {
            StringBuilder sb = new StringBuilder();
            escapeCommas(sb, o.toString(), 0);
            return sb.toString();
        }).collect(Collectors.joining(",")));
    } else {
        final DumperOptions dumperOptions = new DumperOptions();
        dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
        dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.FOLDED);
        target.put(key,
                new Yaml(dumperOptions).dump(Collections.singletonMap(key.substring(key.lastIndexOf(".") + 1), source)));
    }
}
 
Example 8
Source File: EmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testWriteDoubleQuoted() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon = "\"aaa\": \"0123456789 0123456789\\n0123456789 0123456789\"\n\"bbb\": \"\\nbla-bla\"\n";
    assertEquals(etalon, output);
}
 
Example 9
Source File: EmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testWritePlain() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.PLAIN);
    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon = "aaa: |-\n  0123456789 0123456789\n  0123456789 0123456789\nbbb: |2-\n\n  bla-bla\n";
    assertEquals(etalon, output);
}
 
Example 10
Source File: EmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testWriteLiteral() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.LITERAL);
    String folded = "0123456789 0123456789 0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla\n");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon = "\"aaa\": |-\n  0123456789 0123456789 0123456789 0123456789\n\"bbb\": |2\n\n  bla-bla\n";
    assertEquals(etalon, output);
}
 
Example 11
Source File: EmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testWriteFolded() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.FOLDED);
    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla\n");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon = "\"aaa\": >-\n  0123456789 0123456789\n\n  0123456789 0123456789\n\"bbb\": >2\n\n  bla-bla\n";
    assertEquals(etalon, output);
}
 
Example 12
Source File: TagsTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * test fix for issue 18 -
 * http://code.google.com/p/snakeyaml/issues/detail?id=18
 */
public void testLong() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    Yaml yaml = new Yaml(options);
    Foo foo = new Foo();
    String output = yaml.dump(foo);
    // System.out.println(output);
    Foo foo2 = (Foo) yaml.load(output);
    assertEquals(new Long(42L), foo2.getBar());
}
 
Example 13
Source File: LongTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testLongFail() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    Yaml yaml = new Yaml(options);
    Foo foo = new Foo();
    String output = yaml.dump(foo);
    // System.out.println(output);
    try {
        yaml.load(output);
    } catch (Exception e) {
        assertTrue(e.getMessage(), e.getMessage().contains("argument type mismatch"));
    }
}
 
Example 14
Source File: LineBreakDooubleQuotedTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDoubleQuotedStyleNoLineSplit() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
    options.setWidth(20);
    options.setSplitLines(false);
    options.setIndent(4);
    Yaml yaml = new Yaml(options);
    String etalon = "12345678901234567890\n\n123  456";
    String output = yaml.dump(etalon);
    // System.out.println(output);
    assertEquals("\"12345678901234567890\\n\\n123  456\"\n", output);
    String parsed = (String) yaml.load(output);
    assertEquals(etalon, parsed);
}
 
Example 15
Source File: YamlFormatter.java    From structlog4j with MIT License 5 votes vote down vote up
@Override
protected Yaml initialValue() {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);

    return new Yaml(options);
}
 
Example 16
Source File: UnicodeStyleTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDoubleQuotedStyle() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump("í");
    // System.out.println(output);
    assertEquals("\"í\"\n", output);
}
 
Example 17
Source File: ConfigurationAsCode.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@VisibleForTesting
@Restricted(NoExternalUse.class)
public static void serializeYamlNode(Node root, Writer writer) throws IOException {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(BLOCK);
    options.setDefaultScalarStyle(PLAIN);
    options.setSplitLines(true);
    options.setPrettyFlow(true);
    Serializer serializer = new Serializer(new Emitter(writer, options), new Resolver(),
            options, null);
    serializer.open();
    serializer.serialize(root);
    serializer.close();
}
 
Example 18
Source File: ResourceConfig.java    From halyard with Apache License 2.0 5 votes vote down vote up
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) // snake yaml is not thread safe
// (https://bitbucket.org/asomov/snakeyaml/wiki/Documentation#markdown-header-threading)
Yaml yamlParser() {
  DumperOptions options = new DumperOptions();
  options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
  return new Yaml(new SafeConstructor(), new Representer(), options);
}
 
Example 19
Source File: PrintableUnicodeTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
private Yaml createYaml() {
    DumperOptions options = new DumperOptions();
    options.setAllowUnicode(false);
    options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
    return new Yaml(options);
}
 
Example 20
Source File: SkipperStreamDeployer.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
private Package createDependentPackage(String packageVersion, AppDeploymentRequest appDeploymentRequest) {
	Package pkg = new Package();
	String packageName = appDeploymentRequest.getDefinition().getName();

	PackageMetadata packageMetadata = new PackageMetadata();
	packageMetadata.setApiVersion(SkipperStream.SKIPPER_DEFAULT_API_VERSION);
	packageMetadata.setKind(SkipperStream.SKIPPER_DEFAULT_KIND);
	packageMetadata.setName(packageName);
	packageMetadata.setVersion(packageVersion);
	packageMetadata.setMaintainer(SkipperStream.SKIPPER_DEFAULT_MAINTAINER);

	pkg.setMetadata(packageMetadata);

	ConfigValues configValues = new ConfigValues();
	Map<String, Object> configValueMap = new HashMap<>();
	Map<String, Object> metadataMap = new HashMap<>();
	Map<String, Object> specMap = new HashMap<>();

	// Add metadata
	metadataMap.put("name", packageName);

	// Add spec
	String resourceWithoutVersion = this.appRegistryService.getResourceWithoutVersion(appDeploymentRequest.getResource());
	specMap.put("resource", resourceWithoutVersion);
	specMap.put("applicationProperties", appDeploymentRequest.getDefinition().getProperties());
	specMap.put("deploymentProperties", appDeploymentRequest.getDeploymentProperties());
	String version = this.appRegistryService.getResourceVersion(appDeploymentRequest.getResource());
	// Add version, including possible override via deploymentProperties - hack to store version in cmdline args
	if (appDeploymentRequest.getCommandlineArguments().size() == 1) {
		specMap.put("version", appDeploymentRequest.getCommandlineArguments().get(0));
	}
	else {
		specMap.put("version", version);
	}
	// Add metadata and spec to top level map
	configValueMap.put("metadata", metadataMap);
	configValueMap.put("spec", specMap);

	DumperOptions dumperOptions = new DumperOptions();
	dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
	dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
	dumperOptions.setPrettyFlow(false);
	dumperOptions.setSplitLines(false);
	Yaml yaml = new Yaml(dumperOptions);
	configValues.setRaw(yaml.dump(configValueMap));

	pkg.setConfigValues(configValues);
	pkg.setTemplates(createGenericTemplate());
	return pkg;

}