Java Code Examples for org.yaml.snakeyaml.representer.Representer#addClassTag()

The following examples show how to use org.yaml.snakeyaml.representer.Representer#addClassTag() . 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: Main.java    From rundeck-cli with Apache License 2.0 6 votes vote down vote up
private static void configYamlFormat(final ToolBelt belt, final RdClientConfig config) {
    DumperOptions dumperOptions = new DumperOptions();
    dumperOptions.setDefaultFlowStyle(
            "BLOCK".equalsIgnoreCase(config.getString("RD_YAML_FLOW", "BLOCK")) ?
            DumperOptions.FlowStyle.BLOCK :
            DumperOptions.FlowStyle.FLOW
    );
    dumperOptions.setPrettyFlow(config.getBool("RD_YAML_PRETTY", true));
    Representer representer = new Representer();
    representer.addClassTag(JobItem.class, Tag.MAP);
    representer.addClassTag(ScheduledJobItem.class, Tag.MAP);
    representer.addClassTag(DateInfo.class, Tag.MAP);
    representer.addClassTag(Execution.class, Tag.MAP);
    belt.formatter(new YamlFormatter(DataOutputAsFormatable, new Yaml(representer, dumperOptions)));
    belt.channels().infoEnabled(false);
    belt.channels().warningEnabled(false);
    belt.channels().errorEnabled(false);
}
 
Example 2
Source File: TestUtils.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
/**
 * Create master key file.
 *
 * @param file                   file instance
 * @param masterKeyConfiguration master key configuration
 */
public static void createMasterKeyFile(File file, MasterKeyConfiguration masterKeyConfiguration) {
    try {
        file.createNewFile();
        file.deleteOnExit();
        FileWriter fileWriter = new FileWriter(file);

        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

        Representer representer = new Representer();
        representer.addClassTag(MasterKeyConfiguration.class, Tag.MAP);
        Yaml yaml = new Yaml(representer, options);

        yaml.setBeanAccess(BeanAccess.FIELD);
        yaml.dump(masterKeyConfiguration, fileWriter);
    } catch (IOException e) {
        Assert.fail("Failed to create temp password file");
    }
}
 
Example 3
Source File: YamlFactory.java    From waggle-dance with Apache License 2.0 6 votes vote down vote up
public static Yaml newYaml() {
  PropertyUtils propertyUtils = new AdvancedPropertyUtils();
  propertyUtils.setSkipMissingProperties(true);

  Constructor constructor = new Constructor(Federations.class);
  TypeDescription federationDescription = new TypeDescription(Federations.class);
  federationDescription.putListPropertyType("federatedMetaStores", FederatedMetaStore.class);
  constructor.addTypeDescription(federationDescription);
  constructor.setPropertyUtils(propertyUtils);

  Representer representer = new AdvancedRepresenter();
  representer.setPropertyUtils(new FieldOrderPropertyUtils());
  representer.addClassTag(Federations.class, Tag.MAP);
  representer.addClassTag(AbstractMetaStore.class, Tag.MAP);
  representer.addClassTag(WaggleDanceConfiguration.class, Tag.MAP);
  representer.addClassTag(YamlStorageConfiguration.class, Tag.MAP);
  representer.addClassTag(GraphiteConfiguration.class, Tag.MAP);

  DumperOptions dumperOptions = new DumperOptions();
  dumperOptions.setIndent(2);
  dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK);

  return new Yaml(constructor, representer, dumperOptions);
}
 
Example 4
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 5
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 6
Source File: ContainerDataYaml.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Given a ContainerType this method returns a Yaml representation of
 * the container properties.
 *
 * @param containerType type of container
 * @return Yamal representation of container properties
 *
 * @throws StorageContainerException if the type is unrecognized
 */
public static Yaml getYamlForContainerType(ContainerType containerType)
    throws StorageContainerException {
  PropertyUtils propertyUtils = new PropertyUtils();
  propertyUtils.setBeanAccess(BeanAccess.FIELD);
  propertyUtils.setAllowReadOnlyProperties(true);

  switch (containerType) {
  case KeyValueContainer:
    Representer representer = new ContainerDataRepresenter();
    representer.setPropertyUtils(propertyUtils);
    representer.addClassTag(
        KeyValueContainerData.class,
        KeyValueContainerData.KEYVALUE_YAML_TAG);

    Constructor keyValueDataConstructor = new ContainerDataConstructor();

    return new Yaml(keyValueDataConstructor, representer);
  default:
    throw new StorageContainerException("Unrecognized container Type " +
        "format " + containerType, ContainerProtos.Result
        .UNKNOWN_CONTAINER_TYPE);
  }
}
 
Example 7
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 8
Source File: ClassTagsTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testDumpClassTag() {
    Car car = new Car();
    car.setPlate("12-XP-F4");
    List<Wheel> wheels = new ArrayList<Wheel>();
    for (int i = 1; i < 6; i++) {
        Wheel wheel = new Wheel();
        wheel.setId(i);
        wheels.add(wheel);
    }
    car.setWheels(wheels);
    Representer representer = new Representer();
    representer.addClassTag(Car.class, new Tag("!car"));
    representer.addClassTag(Wheel.class, Tag.MAP);
    Yaml yaml = new Yaml(representer);
    String output = yaml.dump(car);
    assertEquals(Util.getLocalResource("constructor/car-without-tags.yaml"), output);
}
 
Example 9
Source File: ProjectConfigurator.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
private void dumpConfiguration(Path configFilePath) {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);

    Representer representer = new Representer() {
        @Override
        protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,
            Tag customTag) {
            // if value of property is null, ignore it.
            if (propertyValue == null) {
                return null;
            } else {
                return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
            }
        }
    };

    representer.addClassTag(Configuration.class, Tag.MAP);

    Yaml yaml = new Yaml(representer, options);
    String config = yaml.dump(configuration);

    for (TestExecutionPlannerFactory factory : new JavaSPILoader().all(TestExecutionPlannerFactory.class)) {
        StrategyConfiguration strategyConfig = factory.strategyConfiguration();
        config = config.replaceAll("!!" + strategyConfig.getClass().getName(), strategyConfig.name() + ":");
    }
    try {
        Files.write(configFilePath, config.getBytes());
    } catch (IOException e) {
        throw new RuntimeException("Failed to dump configuration in file " + configFilePath, e);
    }
}
 
Example 10
Source File: YamlUtil.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 将对象dump成yaml格式的字符串
 */
public static String dump(Object obj) {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);
    options.setPrettyFlow(false);
    Representer representer = new Representer();
    representer.addClassTag(obj.getClass(), Tag.MAP);
    Yaml yaml = new Yaml(representer, options);
    return yaml.dump(obj);
}
 
Example 11
Source File: PropOrderInfluenceWhenAliasedInGenericCollectionTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testABWithCustomTag() {
    SuperSaverAccount supersaver = new SuperSaverAccount();
    GeneralAccount generalAccount = new GeneralAccount();

    CustomerAB customerAB = new CustomerAB();
    ArrayList<Account> all = new ArrayList<Account>();
    all.add(supersaver);
    all.add(generalAccount);
    ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    general.add(generalAccount);
    general.add(supersaver);

    customerAB.aAll = all;
    customerAB.bGeneral = general;

    Constructor constructor = new Constructor();
    Representer representer = new Representer();
    Tag generalAccountTag = new Tag("!GA");
    constructor
            .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag));
    representer.addClassTag(GeneralAccount.class, generalAccountTag);

    Yaml yaml = new Yaml(constructor, representer);
    String dump = yaml.dump(customerAB);
    // System.out.println(dump);
    CustomerAB parsed = (CustomerAB) yaml.load(dump);
    assertNotNull(parsed);
}
 
Example 12
Source File: PropOrderInfluenceWhenAliasedInGenericCollectionTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testABPropertyWithCustomTag() {
    SuperSaverAccount supersaver = new SuperSaverAccount();
    GeneralAccount generalAccount = new GeneralAccount();

    CustomerAB_Property customerAB_property = new CustomerAB_Property();
    ArrayList<Account> all = new ArrayList<Account>();
    all.add(supersaver);
    all.add(generalAccount);
    ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    general.add(generalAccount);
    general.add(supersaver);

    customerAB_property.acc = generalAccount;
    customerAB_property.bGeneral = general;

    Constructor constructor = new Constructor();
    Representer representer = new Representer();

    Tag generalAccountTag = new Tag("!GA");
    constructor
            .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag));
    representer.addClassTag(GeneralAccount.class, generalAccountTag);

    Yaml yaml = new Yaml(constructor, representer);
    String dump = yaml.dump(customerAB_property);
    // System.out.println(dump);
    CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump);
    assertNotNull(parsed);
}
 
Example 13
Source File: NonAsciiCharsInClassNameTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDumpCustomTag() {
    Académico obj = new Académico();
    obj.setId(123);
    obj.setName("Foo bar 123");
    Representer repr = new Representer();
    repr.addClassTag(Académico.class, new Tag("!foo"));
    Yaml yaml = new Yaml(repr);
    String result = yaml.dump(obj);
    assertEquals("!foo {id: 123, name: Foo bar 123}\n", result);
}
 
Example 14
Source File: NonAsciiCharsInClassNameTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDumpEscapedTag() {
    Académico obj = new Académico();
    obj.setId(123);
    obj.setName("Foo bar 123");
    Representer repr = new Representer();
    repr.addClassTag(Académico.class, new Tag("!Académico"));
    Yaml yaml = new Yaml(repr);
    String result = yaml.dump(obj);
    assertEquals("!Acad%C3%A9mico {id: 123, name: Foo bar 123}\n", result);
}
 
Example 15
Source File: TypeSafeCollectionsTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testWithGlobalTag() {
    Map<MyWheel, Date> wheels = new TreeMap<MyWheel, Date>();
    long time = 1248212168084L;
    for (int i = 1; i < 6; i++) {
        MyWheel mw = new MyWheel();
        mw.setId(i);
        mw.setBrand(mw.getBrand() + String.valueOf(i));
        wheels.put(mw, new Date(time + i));
    }
    MyCar c = new MyCar();
    c.setPlate("00-FF-Q2");
    c.setWheels(wheels);
    Representer representer = new Representer();
    representer.addClassTag(MyWheel.class, Tag.MAP);
    Yaml yaml = new Yaml(representer);
    String output = yaml.dump(c);
    assertEquals(Util.getLocalResource("javabeans/mycar-with-global-tag1.yaml"), output);
    // load
    Yaml beanLoader = new Yaml();
    MyCar car = beanLoader.loadAs(output, MyCar.class);
    assertNotNull(car);
    assertEquals("00-FF-Q2", car.getPlate());
    assertEquals(5, car.getWheels().size());
    for (Date d : car.getWheels().values()) {
        // give a day for any timezone
        assertTrue(d.before(new Date(time + 1000 * 60 * 60 * 24)));
        assertTrue(d.after(new Date(time)));
    }
    Object wheel = car.getWheels().keySet().iterator().next();
    assertTrue(wheel instanceof MyWheel);
    MyWheel w = (MyWheel) wheel;
    assertEquals(1, w.getId());
    assertEquals("Pirelli1", w.getBrand());
}
 
Example 16
Source File: HumanGenericsTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
public void testChildren2() throws IOException, IntrospectionException {
    if (!GenericsBugDetector.isProperIntrospection()) {
        return;
    }
    HumanGen2 father = new HumanGen2();
    father.setName("Father");
    father.setBirthday(new Date(1000000000));
    father.setBirthPlace("Leningrad");
    father.setBankAccountOwner(father);
    //
    HumanGen2 mother = new HumanGen2();
    mother.setName("Mother");
    mother.setBirthday(new Date(100000000000L));
    mother.setBirthPlace("Saint-Petersburg");
    father.setPartner(mother);
    mother.setPartner(father);
    mother.setBankAccountOwner(father);
    //
    HumanGen2 son = new HumanGen2();
    son.setName("Son");
    son.setBirthday(new Date(310000000000L));
    son.setBirthPlace("Munich");
    son.setBankAccountOwner(father);
    son.setFather(father);
    son.setMother(mother);
    //
    HumanGen2 daughter = new HumanGen2();
    daughter.setName("Daughter");
    daughter.setBirthday(new Date(420000000000L));
    daughter.setBirthPlace("New York");
    daughter.setBankAccountOwner(father);
    daughter.setFather(father);
    daughter.setMother(mother);
    //
    HashMap<HumanGen2, String> children = new LinkedHashMap<HumanGen2, String>(2);
    children.put(son, "son");
    children.put(daughter, "daughter");
    father.setChildren(children);
    mother.setChildren(children);
    //
    Representer representer = new Representer();
    representer.addClassTag(HumanGen2.class, Tag.MAP);
    Yaml yaml = new Yaml(representer);
    String output = yaml.dump(son);
    // System.out.println(output);
    String etalon = Util.getLocalResource("recursive/generics/with-children-2.yaml");
    assertEquals(etalon, output);
    // load
    TypeDescription humanDescription = new TypeDescription(HumanGen2.class);
    humanDescription.putMapPropertyType("children", HumanGen2.class, String.class);
    Yaml beanLoader = new Yaml(new Constructor(humanDescription));
    //
    HumanGen2 son2 = beanLoader.loadAs(output, HumanGen2.class);
    assertNotNull(son2);
    assertEquals("Son", son.getName());

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

    Map<HumanGen2, String> children2 = father2.getChildren();
    assertEquals(2, children2.size());
    assertSame(father2.getPartner().getChildren(), children2);

}
 
Example 17
Source File: YamlParser.java    From nexus-repository-helm with Eclipse Public License 1.0 3 votes vote down vote up
private Representer setupRepresenter() {
  Representer representer = new JodaTimeRepresenter();

  representer.addClassTag(ChartEntry.class, Tag.MAP);

  return representer;
}