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

The following examples show how to use org.yaml.snakeyaml.Yaml#addImplicitResolver() . 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: ResolverTest.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void testAddImplicitResolver() {
    Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter());
    Pattern regexp = Pattern.compile("\\d\\d-\\d\\d-\\d\\d\\d");
    yaml.addImplicitResolver(new Tag(Tag.PREFIX + "Phone"), regexp, "0123456789");
    Phone phone1 = new Phone("12-34-567");
    Phone phone2 = new Phone("11-22-333");
    Phone phone3 = new Phone("44-55-777");
    List<Phone> etalonList = new ArrayList<Phone>();
    etalonList.add(phone1);
    etalonList.add(phone2);
    etalonList.add(phone3);
    String output = yaml.dump(etalonList);
    assertEquals("[12-34-567, 11-22-333, 44-55-777]\n", output);
    List<Phone> parsedList = (List<Phone>) yaml.load(output);
    assertEquals(3, parsedList.size());
    assertEquals(phone1, parsedList.get(0));
    assertEquals(phone2, parsedList.get(1));
    assertEquals(phone3, parsedList.get(2));
    assertEquals(etalonList, parsedList);
}
 
Example 2
Source Project: snake-yaml   File: DiceExampleTest.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testImplicitResolver() {
    Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter());
    // the tag must start with a digit
    yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), "123456789");
    // dump
    Map<String, Dice> treasure = new HashMap<String, Dice>();
    treasure.put("treasure", new Dice(10, 20));
    String output = yaml.dump(treasure);
    assertEquals("{treasure: 10d20}\n", output);
    // load
    Object data = yaml.load("{damage: 5d10}");
    Map<String, Dice> map = (Map<String, Dice>) data;
    assertEquals(new Dice(5, 10), map.get("damage"));
}
 
Example 3
Source Project: snake-yaml   File: DiceExampleTest.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testImplicitResolverWithNull() {
    Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter());
    // the tag may start with anything
    yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), null);
    // dump
    Map<String, Dice> treasure = new HashMap<String, Dice>();
    treasure.put("treasure", new Dice(10, 20));
    String output = yaml.dump(treasure);
    assertEquals("{treasure: 10d20}\n", output);
    // load
    Object data = yaml.load("{damage: 5d10}");
    Map<String, Dice> map = (Map<String, Dice>) data;
    assertEquals(new Dice(5, 10), map.get("damage"));
}
 
Example 4
Source Project: snake-yaml   File: DiceExampleTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testImplicitResolverJavaBean() {
    Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter());
    // the tag must start with a digit
    yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), "123456789");
    // dump
    DiceBean bean = new DiceBean();
    bean.treasure = new Dice(10, 20);
    String output = yaml.dump(bean);
    assertEquals("!!examples.DiceExampleTest$DiceBean {treasure: 10d20}\n", output);
    // load
    Object loaded = yaml.load(output);
    assertEquals(loaded, bean);
}
 
Example 5
@SuppressWarnings("unchecked")
public void testImplicit() {
    Yaml yaml = new Yaml(new BigConstructor());
    yaml.addImplicitResolver(CUSTOM_TAG, CUSTOM_PATTERN, "-0123456789");
    Map<String, Object> obj = (Map<String, Object>) yaml.load("bar: 50%");
    assertEquals("0.5", obj.get("bar").toString());
    assertEquals(BigDecimal.class, obj.get("bar").getClass());
}
 
Example 6
public void testImplicitFailure() {
    Yaml yaml = new Yaml(new BigConstructor());
    yaml.addImplicitResolver(CUSTOM_TAG, Pattern.compile("\\d+%"), "-0123456789");
    try {
        yaml.load("bar: !!float 50%");
        fail("Both implicit and explicit are present.");
    } catch (NumberFormatException e) {
        assertEquals("For input string: \"50%\"", e.getMessage());
    }
}
 
Example 7
Source Project: snake-yaml   File: UuidSupportTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void dumpAsString() {
    UUID uuid = UUID.randomUUID();
    String str = uuid.toString();
    Yaml yaml = new Yaml();
    yaml.addImplicitResolver(UUID_TAG, UUID_PATTERN, null);
    String output = yaml.dump(str);
    assertEquals("'" + str + "'\n", output);
    assertEquals(str + "\n", yaml.dump(uuid));
}
 
Example 8
Source Project: snake-yaml   File: UuidSupportTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void loadAsUuid() {
    Yaml yaml = new Yaml();
    yaml.addImplicitResolver(UUID_TAG, UUID_PATTERN, null);
    UUID uuid = (UUID) yaml.load("7f511847-781a-45df-9c8d-1e32e028b9b3");
    assertEquals("7f511847-781a-45df-9c8d-1e32e028b9b3", uuid.toString());
}
 
Example 9
Source Project: snake-yaml   File: ResolverTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testAddImplicitResolver2() {
    Yaml yaml = new Yaml(new PointRepresenter());
    Pattern regexp = Pattern.compile("\\d\\d-\\d\\d-\\d\\d\\d");
    yaml.addImplicitResolver(new Tag(Tag.PREFIX + "Phone"), regexp, "\0");
    Pattern regexp2 = Pattern.compile("x\\d_y\\d");
    // try any scalar, and not only those which start with 'x'
    yaml.addImplicitResolver(new Tag(Tag.PREFIX + "Point"), regexp2, null);
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("a", new Phone("12-34-567"));
    map.put("b", new Point(1, 5));
    String output = yaml.dump(map);
    assertEquals("{a: 12-34-567, b: x1_y5}\n", output);
}
 
Example 10
Source Project: snake-yaml   File: ImplicitResolverTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testMain() {
    Map<String, String> config = new HashMap<String, String>();
    config.put("user.home", "HOME");
    Constructor constructor = new ConfigurationConstructor(config);
    constructor.addTypeDescription(new TypeDescription(TestBean.class, "!testbean"));
    Yaml yaml = new Yaml(constructor);
    yaml.addImplicitResolver(CFG, Pattern.compile("\\$\\([a-zA-Z\\d\\u002E\\u005F]+\\)"), "$");
    TestBean bean = (TestBean) yaml.load("!testbean {myval: !cfg $(user.home)}");
    // System.out.println(bean.toString());
    assertEquals("Explicit tag must be respected", "HOME", bean.getMyval());
    bean = (TestBean) yaml.load("!testbean {myval: $(user.home)}");
    // System.out.println(bean.toString());
    assertEquals("Implicit tag must be respected", "HOME", bean.getMyval());
}
 
Example 11
Source Project: biomedicus   File: YamlSerialization.java    License: Apache License 2.0 5 votes vote down vote up
public static Yaml createYaml(BidirectionalDictionary bidirectionalDictionary) {
  Yaml yaml = new Yaml(constructor(bidirectionalDictionary), representer(bidirectionalDictionary));
  yaml.addImplicitResolver(new Tag("!cui"), CUI.CUI_PATTERN, "C");
  yaml.addImplicitResolver(new Tag("!tui"), TUI.TUI_PATTERN, "T");
  yaml.addImplicitResolver(new Tag("!sui"), SUI.SUI_PATTERN, "S");
  return yaml;
}