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

The following examples show how to use org.yaml.snakeyaml.DumperOptions#setAllowReadOnlyProperties() . 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: SetAsSequenceTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testDump() {
    Blog blog = new Blog("Test Me!");
    blog.addPost(new Post("Title1", "text 1"));
    blog.addPost(new Post("Title2", "text text 2"));
    blog.numbers.add(19);
    blog.numbers.add(17);
    TreeSet<String> labels = new TreeSet<String>();
    labels.add("Java");
    labels.add("YAML");
    labels.add("SnakeYAML");
    blog.setLabels(labels);
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(blog);
    // System.out.println(output);
    assertEquals(Util.getLocalResource("issues/issue73-1.txt"), output);
}
 
Example 2
Source File: ReadOnlyPropertiesTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testBean2() {
    IncompleteBean bean = new IncompleteBean();
    bean.setName("lunch");
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    assertEquals("id: 10\nname: lunch\n", output);
    //
    Yaml loader = new Yaml();
    try {
        loader.loadAs(output, IncompleteBean.class);
        fail("Setter is missing.");
    } catch (YAMLException e) {
        String message = e.getMessage();
        assertTrue(
                message,
                message.contains("Unable to find property 'id' on class: org.yaml.snakeyaml.issues.issue47.IncompleteBean"));
    }
}
 
Example 3
Source File: DumpExampleTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDumpCustomJavaClass() {
    Hero hero = new Hero("Galain Ysseleg", -3, 2);
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(hero);
    assertEquals("!!examples.Hero {hp: -3, name: Galain Ysseleg, sp: 2}\n", output);
}
 
Example 4
Source File: DiceExampleTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testRepresenter() {
    Dice dice = new Dice(3, 6);
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(dice);
    assertEquals("!!examples.Dice {a: 3, b: 6}\n", output);
}
 
Example 5
Source File: DumpSetAsSequenceExampleTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDumpFlow() {
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(new SetRepresenter(), options);
    String output = yaml.dump(createBlog());
    // System.out.println(output);
    assertEquals(Util.getLocalResource("issues/issue73-dump7.txt"), output);
    //
    check(output);
}
 
Example 6
Source File: DumpSetAsSequenceExampleTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDumpBlock() {
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    Yaml yaml = new Yaml(new SetRepresenter(), options);
    String output = yaml.dump(createBlog());
    // System.out.println(output);
    assertEquals(Util.getLocalResource("issues/issue73-dump8.txt"), output);
    //
    check(output);
}
 
Example 7
Source File: PrattleRepresenterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void test2beans() {
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    Person person = new Person("Alan", "Gutierrez", 9);
    String etalon = "!!org.yaml.snakeyaml.issues.issue8.Person {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n";
    assertEquals(etalon, yaml.dump(person));
    Horse horse = new Horse("Tom", person);
    String etalon2 = "!!org.yaml.snakeyaml.issues.issue8.PrattleRepresenterTest$Horse\nname: Tom\nowner: {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n";
    assertEquals(etalon2, yaml.dump(horse));
}
 
Example 8
Source File: GenericMapTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testMap() throws Exception {
    BeanWithMap fact = new BeanWithMap();
    GenericMap<Integer> shash = fact.getMap();
    shash.put("toto", new Integer(10));
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    // String txt = yaml.dump(fact);
    // assertTrue(txt.contains("org.yaml.snakeyaml.issues.issue143.GenericMapTest"));
}
 
Example 9
Source File: RepresenterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testRepresenterNoConstructorAvailable() {
    MyBean2 bean = new MyBean2("Gnome", true);
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    assertEquals("!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean2 {valid: true}\n",
            yaml.dump(bean));
}
 
Example 10
Source File: IncompleteBeanConstructorTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testRepresentor() {
    IncompleteJavaBean bean = new IncompleteJavaBean();
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(bean);
    String className = this.getClass().getPackage().getName();
    assertEquals("!!" + className + ".IncompleteJavaBean {name: No name}\n", output);
}
 
Example 11
Source File: VelocityTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testNoTemplate() {
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(options);
    String output = yaml.dumpAsMap(createBean());
    // System.out.println(output);
    assertEquals(Util.getLocalResource("template/etalon1.yaml"), output);
}
 
Example 12
Source File: TypesRegistryServiceImpl.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
private String toYaml(Object obj) {
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    options.setIndent(4);
    return new Yaml(options).dump(obj);
}
 
Example 13
Source File: YamlSerializer.java    From copper-engine with Apache License 2.0 5 votes vote down vote up
protected Yaml initialYaml() {
    DumperOptions dO = new DumperOptions();
    dO.setAllowReadOnlyProperties(true);
    Yaml yaml = new Yaml(dO);
    yaml.setBeanAccess(BeanAccess.FIELD);
    return yaml;
}
 
Example 14
Source File: AbstractChartWriter.java    From microbean-helm with Apache License 2.0 3 votes vote down vote up
/**
 * Creates and returns a new {@link Yaml} instance for (optional)
 * use in writing {@link ConfigOrBuilder} and {@link
 * MetadataOrBuilder} objects.
 *
 * <p>This method never returns {@code null}.</p>
 *
 * <p>Overrides of this method must not return {@code null}.</p>
 *
 * <p>Behavior is undefined if overrides of this method interact
 * with other methods defined by this class.</p>
 *
 * @return a non-{@code null} {@link Yaml} instance
 */
protected Yaml createYaml() {
  final Representer representer = new TerseRepresenter();
  representer.setPropertyUtils(new CustomPropertyUtils());
  final DumperOptions options = new DumperOptions();
  options.setAllowReadOnlyProperties(true);
  return new Yaml(new SafeConstructor(), representer, options);
}