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

The following examples show how to use org.yaml.snakeyaml.DumperOptions#setExplicitStart() . 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: RubyTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testEmitNoTags() {
    TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
    DumperOptions options = new DumperOptions();
    options.setExplicitStart(true);
    Yaml yaml2 = new Yaml(options);
    String output = yaml2.dumpAsMap(result);
    assertFalse("No tags expected.", output.contains("Sub1"));
    // System.out.println(output);
    // parse back. Without tags it shall still work
    Yaml beanLoader = new Yaml();
    TestObject result2 = beanLoader.loadAs(output, TestObject.class);
    assertEquals(0, result2.getSub1().getAtt2());
    assertEquals("MyString", result2.getSub2().getAtt1());
    assertEquals(1, result2.getSub2().getAtt2().size());
    assertEquals(12345, result2.getSub2().getAtt3());
}
 
Example 2
Source File: RubyTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testEmitWithTags() {
    TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
    DumperOptions options = new DumperOptions();
    options.setExplicitStart(true);
    Representer repr = new Representer();
    repr.addClassTag(TestObject.class, new Tag("!ruby/object:Test::Module::Object"));
    repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1"));
    repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2"));
    Yaml yaml2 = new Yaml(repr, options);
    String output = yaml2.dump(result);
    // System.out.println(output);
    assertTrue("Tags must be present.",
            output.startsWith("--- !ruby/object:Test::Module::Object"));
    assertTrue("Tags must be present: " + output,
            output.contains("!ruby/object:Test::Module::Sub1"));
    assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2"));
    // parse back.
    TestObject result2 = parseObject(output);
    assertEquals(0, result2.getSub1().getAtt2());
    assertEquals("MyString", result2.getSub2().getAtt1());
    assertEquals(1, result2.getSub2().getAtt2().size());
    assertEquals(12345, result2.getSub2().getAtt3());
}
 
Example 3
Source File: RubyTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testEmitWithTags2WithoutTagForParentJavabean() {
    TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
    DumperOptions options = new DumperOptions();
    options.setExplicitStart(true);
    Representer repr = new Representer();
    repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1"));
    repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2"));
    Yaml yaml2 = new Yaml(repr, options);
    String output = yaml2.dump(result);
    // System.out.println(output);
    assertTrue("Tags must be present.",
            output.startsWith("--- !!org.yaml.snakeyaml.ruby.TestObject"));
    assertTrue("Tags must be present: " + output,
            output.contains("!ruby/object:Test::Module::Sub1"));
    assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2"));
    // parse back.
    TestObject result2 = parseObject(output);
    assertEquals(0, result2.getSub1().getAtt2());
    assertEquals("MyString", result2.getSub2().getAtt1());
    assertEquals(1, result2.getSub2().getAtt2().size());
    assertEquals(12345, result2.getSub2().getAtt3());
}
 
Example 4
Source File: YamlProcessor.java    From jphp with Apache License 2.0 6 votes vote down vote up
@Signature(@Arg(value = "flags", optional = @Optional("0")))
public Memory __construct(Environment env, Memory... args) {
    DumperOptions options = new DumperOptions();

    int flags = args[0].toInteger();

    options.setPrettyFlow((flags & SERIALIZE_PRETTY_FLOW) == SERIALIZE_PRETTY_FLOW);
    options.setDefaultFlowStyle((flags & SERIALIZE_PRETTY_FLOW) == SERIALIZE_PRETTY_FLOW ? DumperOptions.FlowStyle.BLOCK : DumperOptions.FlowStyle.AUTO);

    options.setCanonical((flags & SERIALIZE_CANONICAL) == SERIALIZE_CANONICAL);
    options.setExplicitStart((flags & SERIALIZE_EXPLICIT_START) == SERIALIZE_EXPLICIT_START);
    options.setExplicitEnd((flags & SERIALIZE_EXPLICIT_END) == SERIALIZE_EXPLICIT_END);
    options.setSplitLines(!((flags & SERIALIZE_NOT_SPLIT_LINES) == SERIALIZE_NOT_SPLIT_LINES));

    yaml = new Yaml(options);
    return Memory.NULL;
}
 
Example 5
Source File: DumpExampleTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDumpMany() {
    List<Integer> docs = new ArrayList<Integer>();
    for (int i = 1; i < 4; i++) {
        docs.add(i);
    }
    DumperOptions options = new DumperOptions();
    options.setExplicitStart(true);
    Yaml yaml = new Yaml(options);
    String result = yaml.dumpAll(docs.iterator());
    assertNotNull(result);
    assertTrue(result.contains("--- 2"));
}
 
Example 6
Source File: ManifestUtil.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
public static String getContents(Map<Object, Object> rawMap) {
    DumperOptions options = new DumperOptions();
    options.setExplicitStart(true);
    options.setCanonical(false);
    options.setPrettyFlow(true);
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    Yaml yaml = new Yaml(options);
    return (yaml.dump(rawMap));
}