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

The following examples show how to use org.yaml.snakeyaml.Yaml#dumpAsMap() . 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
Source Project: snake-yaml   File: TriangleBeanTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetTriangle() {
    Triangle triangle = new Triangle();
    triangle.setName("Triangle25");
    TriangleBean bean = new TriangleBean();
    bean.setShape(triangle);
    bean.setName("Bean25");
    Yaml beanDumper = new Yaml();
    String output = beanDumper.dumpAsMap(bean);
    assertEquals(
            "name: Bean25\nshape: !!org.yaml.snakeyaml.javabeans.Triangle\n  name: Triangle25\n",
            output);
    Yaml beanLoader = new Yaml();
    TriangleBean loadedBean = beanLoader.loadAs(output, TriangleBean.class);
    assertNotNull(loadedBean);
    assertEquals("Bean25", loadedBean.getName());
    assertEquals(7, loadedBean.getShape().process());
}
 
Example 2
/**
 * use wrapper with no tag
 */
public void testRootBean() {
    JavaBeanWithStaticState bean = new JavaBeanWithStaticState();
    bean.setName("Bahrack");
    bean.setAge(-47);
    JavaBeanWithStaticState.setType("Type3");
    JavaBeanWithStaticState.color = "Violet";
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(new Wrapper(bean));
    // System.out.println(output);
    assertEquals("age: -47\ncolor: Violet\nname: Bahrack\ntype: Type3\n", output);
    // parse back to instance
    Yaml loader = new Yaml();
    Wrapper wrapper = loader.loadAs(output, Wrapper.class);
    JavaBeanWithStaticState bean2 = wrapper.createBean();
    assertEquals(-47, bean2.getAge());
    assertEquals("Bahrack", bean2.getName());
}
 
Example 3
Source Project: snake-yaml   File: ListFileldBeanTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testDumpList() {
    ListFieldBean bean = new ListFieldBean();
    List<String> list = new ArrayList<String>();
    list.add("aaa");
    list.add("bbb");
    bean.setChildren(list);
    List<Developer> developers = new ArrayList<Developer>();
    developers.add(new Developer("Fred", "creator"));
    developers.add(new Developer("John", "committer"));
    bean.developers = developers;
    bean.setName("Bean123");
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    String etalon = Util.getLocalResource("examples/list-bean-1.yaml");
    assertEquals(etalon, output);
}
 
Example 4
Source Project: snake-yaml   File: CalendarTest.java    License: Apache License 2.0 6 votes vote down vote up
private void check(long time, String timeZone, String warning, String etalon) {
    CalendarBean bean = new CalendarBean();
    bean.setName("lunch");
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(time));
    cal.setTimeZone(TimeZone.getTimeZone(timeZone));
    bean.setCalendar(cal);
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    assertEquals(warning, "calendar: " + etalon + "\nname: lunch\n", output);
    //
    Yaml loader = new Yaml();
    CalendarBean parsed = loader.loadAs(output, CalendarBean.class);
    assertFalse("TimeZone must deviate.", bean.getCalendar().equals(parsed.getCalendar()));
    assertEquals(bean.getCalendar().getTimeInMillis(), parsed.getCalendar().getTimeInMillis());
}
 
Example 5
Source Project: snake-yaml   File: GenericMapBeanTest.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void testGenericMap() {
    Yaml yaml = new Yaml();
    MapProvider<String, Integer> listProvider = new MapProvider<String, Integer>();
    listProvider.getMap().put("foo", 17);
    listProvider.getMap().put("bar", 19);
    String s = yaml.dumpAsMap(listProvider);
    // System.out.println(s);
    assertEquals("map:\n  foo: 17\n  bar: 19\n", s);
    // parse
    Yaml loader = new Yaml();
    MapProvider<String, Integer> listProvider2 = loader.loadAs(s, MapProvider.class);
    assertEquals(new Integer(17), listProvider2.getMap().get("foo"));
    assertEquals(new Integer(19), listProvider2.getMap().get("bar"));
    assertEquals(listProvider, listProvider2);
}
 
Example 6
Source Project: snake-yaml   File: GenericMapBeanTest.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
public void testGenericBean() {
    Yaml yaml = new Yaml();
    MapProvider<String, Bean> listProvider = new MapProvider<String, Bean>();
    Bean foo = new Bean();
    foo.setName("foo");
    listProvider.getMap().put("foo", foo);
    Bean bar = new Bean();
    bar.setName("bar");
    bar.setNumber(3);
    listProvider.getMap().put("bar", bar);
    String s = yaml.dumpAsMap(listProvider);
    // System.out.println(s);
    String etalon = Util.getLocalResource("issues/issue61-2.yaml");
    assertEquals(etalon, s);
    // parse
    Yaml loader = new Yaml();
    MapProvider listProvider2 = loader.loadAs(s, MapProvider.class);
    Bean foo2 = (Bean) listProvider2.getMap().get("foo");
    assertEquals("foo", foo2.getName());
    assertEquals(0, foo2.getNumber());
    Bean bar2 = (Bean) listProvider2.getMap().get("bar");
    assertEquals("bar", bar2.getName());
    assertEquals(3, bar2.getNumber());
}
 
Example 7
Source Project: snake-yaml   File: GenericArrayTypeTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testJavaBean() throws IntrospectionException {
    GenericArray ga = new GenericArray();
    ArrayBean bean = new ArrayBean();
    bean.setId("ID556677");
    bean.setGa(ga);
    Yaml dumper = new Yaml();
    String doc = dumper.dumpAsMap(bean);
    // System.out.println(doc);
    assertEquals(Util.getLocalResource("javabeans/genericArray-1.yaml"), doc);
    //
    Yaml beanLoader = new Yaml();
    if (GenericsBugDetector.isProperIntrospection()) {
        ArrayBean loaded = beanLoader.loadAs(doc, ArrayBean.class);
        assertEquals("ID556677", loaded.getId());
        assertEquals("Array3", loaded.getGa().getName());
        assertEquals(3, loaded.getGa().getHome().length);
    } else {
        try {
            beanLoader.load(doc);
        } catch (Exception e) {
            // TODO Check GenericArrayType
            String message = "Cannot create property=home for JavaBean=org.yaml.snakeyaml.generics.GenericArrayTypeTest$GenericArray";
            assertTrue(e.getMessage(), e.getMessage().contains(message));
        }
    }
}
 
Example 8
Source Project: snake-yaml   File: BasicDumpTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testTag2() {
    DataSource base = new DataSource();
    JDBCDataSource baseJDBC = new JDBCDataSource();
    baseJDBC.setParent(base);

    ArrayList<DataSource> dataSources = new ArrayList<DataSource>();
    dataSources.add(base);
    dataSources.add(baseJDBC);

    DataSources ds = new DataSources();
    ds.setDataSources(dataSources);

    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(ds);

    String etalon = Util.getLocalResource("javabeans/issue10-2.yaml");
    assertEquals(etalon.trim(), output.trim());
}
 
Example 9
Source Project: snake-yaml   File: BirdTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testHome() throws IntrospectionException {
    Bird bird = new Bird();
    bird.setName("Eagle");
    Nest home = new Nest();
    home = new Nest();
    home.setHeight(3);
    bird.setHome(home);
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(bird);
    Bird parsed;
    String javaVendor = System.getProperty("java.vm.name");
    Yaml loader = new Yaml();
    if (GenericsBugDetector.isProperIntrospection()) {
        // no global tags
        System.out.println("java.vm.name: " + javaVendor);
        assertEquals("no global tags must be emitted.", "home:\n  height: 3\nname: Eagle\n",
                output);
        parsed = loader.loadAs(output, Bird.class);

    } else {
        // with global tags
        System.out
                .println("JDK requires global tags for JavaBean properties with Java Generics. java.vm.name: "
                        + javaVendor);
        assertEquals("global tags are inevitable here.",
                "home: !!org.yaml.snakeyaml.generics.Nest\n  height: 3\nname: Eagle\n", output);
        parsed = loader.loadAs(output, Bird.class);
    }
    assertEquals(bird.getName(), parsed.getName());
    assertEquals(bird.getHome().getHeight(), parsed.getHome().getHeight());
}
 
Example 10
Source Project: snake-yaml   File: BigDataLoadTest.java    License: Apache License 2.0 5 votes vote down vote up
private String getLongYamlDocument(int size) {
    List<DataBean> beans = new ArrayList<DataBean>();
    Yaml yaml = new Yaml();
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < size; i++) {
        List<String> list = new ArrayList<String>();
        for (int j = 0; j < 10; j++) {
            list.add(String.valueOf(i + j));
        }
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (int j = 0; j < 10; j++) {
            map.put(String.valueOf(j), i + j);
        }
        DataBean bean = new DataBean();
        bean.setId("id" + i);
        bean.setList(list);
        bean.setMap(map);
        beans.add(bean);
        String ooo = yaml.dumpAsMap(bean);
        String[] lines = ooo.split("\n");
        builder.append("-\n");
        for (int j = 0; j < lines.length; j++) {
            builder.append("  ");
            builder.append(lines[j]);
            builder.append("\n");
        }
    }
    String data = builder.toString();
    System.out.println("Long data size: " + data.length() / 1024 + " kBytes.");
    return data;
}
 
Example 11
Source Project: snake-yaml   File: VelocityTest.java    License: 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 Project: snake-yaml   File: TypeSafeMap2Test.java    License: Apache License 2.0 5 votes vote down vote up
public void testMap2() {
    MapBean2 bean = new MapBean2();
    Map<Developer2, Color> data = new LinkedHashMap<Developer2, Color>();
    data.put(new Developer2("Andy", "tester"), Color.BLACK);
    data.put(new SuperMan("Bill", "cleaner", false), Color.BLACK);
    data.put(new Developer2("Lisa", "owner"), Color.RED);
    bean.setData(data);
    Map<Color, Developer2> developers = new LinkedHashMap<Color, Developer2>();
    developers.put(Color.WHITE, new Developer2("Fred", "creator"));
    developers.put(Color.RED, new SuperMan("Jason", "contributor", true));
    developers.put(Color.BLACK, new Developer2("John", "committer"));
    bean.setDevelopers(developers);
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    String etalon = Util.getLocalResource("examples/map-bean-13.yaml");
    assertEquals(etalon, output);
    // load
    Yaml beanLoader = new Yaml();
    MapBean2 parsed = beanLoader.loadAs(etalon, MapBean2.class);
    assertNotNull(parsed);
    Map<Developer2, Color> parsedData = parsed.getData();
    assertEquals(3, parsedData.size());
    assertTrue(parsedData.containsKey(new SuperMan("Bill", "cleaner", false)));
    assertEquals(Color.BLACK, parsedData.get(new SuperMan("Bill", "cleaner", false)));
    //
    Map<Color, Developer2> parsedDevelopers = parsed.getDevelopers();
    assertEquals(3, parsedDevelopers.size());
    assertEquals(new SuperMan("Jason", "contributor", true), parsedDevelopers.get(Color.RED));
}
 
Example 13
public void testWithoutNull() {
    Bean bean = new Bean();
    bean.setValue(5);
    Yaml yaml = new Yaml(new MyRepresenter());
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    assertEquals("value: 5\n", output);
}
 
Example 14
Source Project: snake-yaml   File: CollectionTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testCollectionSet() {
    CollectionSet bean = new CollectionSet();
    Yaml yaml = new Yaml();
    String doc = yaml.dumpAsMap(bean);
    // System.out.println(doc);
    Yaml beanLoader = new Yaml();
    CollectionSet parsed = beanLoader.loadAs(doc, CollectionSet.class);
    assertTrue(parsed.getRoles().contains(11));
    assertTrue(parsed.getRoles().contains(13));
    assertEquals(2, parsed.getRoles().size());
}
 
Example 15
Source Project: snake-yaml   File: HouseTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * no root global tag
 */
public void testDump1() {
    House house = new House();
    FrontDoor frontDoor = new FrontDoor("qaz1", 5);
    frontDoor.setKeytype("qwerty123");
    house.setFrontDoor(frontDoor);
    List<Room> rooms = new ArrayList<Room>();
    rooms.add(new Room("Hall"));
    rooms.add(new Room("Kitchen"));
    house.setRooms(rooms);
    Map<String, String> reminders = new TreeMap<String, String>();
    reminders.put("today", "do nothig");
    reminders.put("tomorrow", "go shoping");
    house.setReminders(reminders);
    house.setNumber(1);
    house.setStreet("Wall Street");
    Yaml beanDumper = new Yaml();
    String yaml = beanDumper.dumpAsMap(house);
    String etalon = Util.getLocalResource("javabeans/house-dump1.yaml");
    assertEquals(etalon, yaml);
    // load
    Yaml beanLoader = new Yaml();
    House loadedHouse = beanLoader.loadAs(yaml, House.class);
    assertNotNull(loadedHouse);
    assertEquals("Wall Street", loadedHouse.getStreet());
    // dump again
    String yaml3 = beanDumper.dumpAsMap(loadedHouse);
    assertEquals(yaml, yaml3);
}
 
Example 16
@Override
public void execute(final String site) throws UpgradeException {
    Path descriptorFile = getRepositoryPath(site).getParent().resolve(descriptorPath);
    if (Files.notExists(descriptorFile)) {
        logger.info("Plugin descriptor file not found for site {0}", site);
        return;
    }
    try (Reader reader = Files.newBufferedReader(descriptorFile)) {
        PluginDescriptor descriptor = descriptorReader.read(reader);
        if (descriptor.getDescriptorVersion().equals(descriptorVersion)) {
            logger.info("Plugin descriptor already update for site " + site);
            return;
        }
        logger.info("Updating plugin descriptor for site " + site);
        doPluginDescriptorUpdates(descriptor);
        descriptor.setDescriptorVersion(descriptorVersion);

        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        options.setPrettyFlow(true);
        Yaml yaml = new Yaml(new Representer() {
            @Override
            protected NodeTuple representJavaBeanProperty(final Object javaBean, final Property property,
                                                          final Object propertyValue, final Tag customTag) {
                if (propertyValue != null) {
                    return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
                }
                return null;
            }
        }, options);
        String content = yaml.dumpAsMap(descriptor);

        writeToRepo(site, descriptorPath, new ByteArrayInputStream(content.getBytes()));

        commitAllChanges(site);
    } catch (Exception e) {
        throw new UpgradeException("Plugin descriptor can't be read for site " + site);
    }
}
 
Example 17
Source Project: snake-yaml   File: HumanGenericsTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * the YAML document should contain no global tags
 * 
 * @throws IntrospectionException
 */
public void testNoChildren2() throws IOException, IntrospectionException {
    if (!GenericsBugDetector.isProperIntrospection()) {
        return;
    }
    HumanGen father = new HumanGen();
    father.setName("Father");
    father.setBirthday(new Date(1000000000));
    father.setBirthPlace("Leningrad");
    father.setBankAccountOwner(father);
    HumanGen mother = new HumanGen();
    mother.setName("Mother");
    mother.setBirthday(new Date(100000000000L));
    mother.setBirthPlace("Saint-Petersburg");
    father.setPartner(mother);
    mother.setPartner(father);
    mother.setBankAccountOwner(father);
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(father);
    String etalon = Util.getLocalResource("recursive/generics/no-children-2.yaml");
    assertEquals(etalon, output);
    //
    Yaml loader = new Yaml();
    HumanGen father2 = (HumanGen) loader.loadAs(etalon, HumanGen.class);
    assertNotNull(father2);
    assertEquals("Father", father2.getName());
    assertEquals("Mother", father2.getPartner().getName());
    assertEquals("Father", father2.getBankAccountOwner().getName());
    assertSame(father2, father2.getBankAccountOwner());
}
 
Example 18
public void testYaml() {
    Blog original = createTestBlog();
    Yaml yamlDumper = constructYamlDumper();
    String serialized = yamlDumper.dumpAsMap(original);
    // System.out.println(serialized);
    assertEquals(Util.getLocalResource("issues/issue55_1.txt"), serialized);
    Yaml blogLoader = new Yaml();
    blogLoader.setBeanAccess(BeanAccess.FIELD);
    Blog rehydrated = blogLoader.loadAs(serialized, Blog.class);
    checkTestBlog(rehydrated);
}
 
Example 19
Source Project: snake-yaml   File: HumanTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testChildren() {
    Human father = new Human();
    father.setName("Father");
    father.setBirthday(new Date(1000000000));
    father.setBirthPlace("Leningrad");
    father.setBankAccountOwner(father);
    //
    Human mother = new Human();
    mother.setName("Mother");
    mother.setBirthday(new Date(100000000000L));
    mother.setBirthPlace("Saint-Petersburg");
    father.setPartner(mother);
    mother.setPartner(father);
    mother.setBankAccountOwner(father);
    //
    Human son = new Human();
    son.setName("Son");
    son.setBirthday(new Date(310000000000L));
    son.setBirthPlace("Munich");
    son.setBankAccountOwner(father);
    son.setFather(father);
    son.setMother(mother);
    //
    Human daughter = new Human();
    daughter.setName("Daughter");
    daughter.setBirthday(new Date(420000000000L));
    daughter.setBirthPlace("New York");
    daughter.setBankAccountOwner(father);
    daughter.setFather(father);
    daughter.setMother(mother);
    //
    Set<Human> children = new LinkedHashSet<Human>(2);
    children.add(son);
    children.add(daughter);
    father.setChildren(children);
    mother.setChildren(children);
    //
    Yaml beanDumper = new Yaml();
    String output = beanDumper.dumpAsMap(son);
    // System.out.println(output);
    String etalon = Util.getLocalResource("recursive/with-children.yaml");
    assertEquals(etalon, output);
    TypeDescription humanDescription = new TypeDescription(Human.class);
    humanDescription.putMapPropertyType("children", Human.class, Object.class);
    Yaml beanLoader = new Yaml(new Constructor(humanDescription));
    //
    Human son2 = beanLoader.loadAs(output, Human.class);
    assertNotNull(son2);
    assertEquals("Son", son.getName());

    Human 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());

    Set<Human> children2 = father2.getChildren();
    assertEquals(2, children2.size());
    assertSame(father2.getPartner().getChildren(), children2);

    for (Object child : children2) {
        // check if type descriptor was correct
        assertSame(Human.class, child.getClass());
    }

    // check if hashCode is correct
    validateSet(children2);
}
 
Example 20
Source Project: snake-yaml   File: HumanTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testChildrenWithoutRootTag() {
    Human father = new Human();
    father.setName("Father");
    father.setBirthday(new Date(1000000000));
    father.setBirthPlace("Leningrad");
    father.setBankAccountOwner(father);
    //
    Human mother = new Human();
    mother.setName("Mother");
    mother.setBirthday(new Date(100000000000L));
    mother.setBirthPlace("Saint-Petersburg");
    father.setPartner(mother);
    mother.setPartner(father);
    mother.setBankAccountOwner(father);
    //
    Human son = new Human();
    son.setName("Son");
    son.setBirthday(new Date(310000000000L));
    son.setBirthPlace("Munich");
    son.setBankAccountOwner(father);
    son.setFather(father);
    son.setMother(mother);
    //
    Human daughter = new Human();
    daughter.setName("Daughter");
    daughter.setBirthday(new Date(420000000000L));
    daughter.setBirthPlace("New York");
    daughter.setBankAccountOwner(father);
    daughter.setFather(father);
    daughter.setMother(mother);
    //
    Set<Human> children = new LinkedHashSet<Human>(2);
    children.add(son);
    children.add(daughter);
    father.setChildren(children);
    mother.setChildren(children);
    //
    Yaml beanDumper = new Yaml();
    String output = beanDumper.dumpAsMap(son);
    // System.out.println(output);
    String etalon = Util.getLocalResource("recursive/with-children-no-root-tag.yaml");
    assertEquals(etalon, output);
    TypeDescription humanDescription = new TypeDescription(Human.class);
    humanDescription.putMapPropertyType("children", Human.class, Object.class);
    Yaml beanLoader = new Yaml(new Constructor(humanDescription));
    //
    Human son2 = beanLoader.loadAs(output, Human.class);
    assertNotNull(son2);
    assertEquals("Son", son.getName());

    Human 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());

    Set<Human> children2 = father2.getChildren();
    assertEquals(2, children2.size());
    assertSame(father2.getPartner().getChildren(), children2);

    for (Object child : children2) {
        // check if type descriptor was correct
        assertSame(Human.class, child.getClass());
    }

    // check if hashCode is correct
    validateSet(children2);
}