Add example to ignore unknown tags
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1d2499b..3d81e39 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -8,6 +8,9 @@
<body>
<release version="1.6-SNAPSHOT" date="in Mercurial" description="development">
<action dev="py4fun" type="add">
+ Add example to ignore unknown tags(2009-12-08)
+ </action>
+ <action dev="py4fun" type="add">
Add Ruby example (2009-12-08)
</action>
<action dev="py4fun" type="update">
diff --git a/src/test/java/examples/IgnoreTagsExampleTest.java b/src/test/java/examples/IgnoreTagsExampleTest.java
new file mode 100644
index 0000000..4f58600
--- /dev/null
+++ b/src/test/java/examples/IgnoreTagsExampleTest.java
@@ -0,0 +1,83 @@
+/**
+ * Copyright (c) 2008-2009 Andrey Somov
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package examples;
+
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.yaml.snakeyaml.Loader;
+import org.yaml.snakeyaml.Util;
+import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.constructor.AbstractConstruct;
+import org.yaml.snakeyaml.constructor.Construct;
+import org.yaml.snakeyaml.constructor.Constructor;
+import org.yaml.snakeyaml.error.YAMLException;
+import org.yaml.snakeyaml.nodes.Node;
+import org.yaml.snakeyaml.nodes.Tags;
+
+public class IgnoreTagsExampleTest extends TestCase {
+ @SuppressWarnings("unchecked")
+ public void testLoad() {
+ String input = Util.getLocalResource("examples/unknown-tags-example.yaml");
+ System.out.println(input);
+ Yaml yaml = new Yaml(new Loader(new MyConstructor()));
+ Map<String, Object> result = (Map<String, Object>) yaml.load(input);
+ // Check the result
+ assertNotNull(result);
+ assertEquals(3, result.size());
+ assertEquals("123", result.get("aaa"));
+ //
+ List<Object> bbb = (List<Object>) result.get("bbb");
+ assertEquals(2, bbb.size());
+ assertEquals(new Integer(111), bbb.get(0));
+ assertEquals("ddd", bbb.get(1));
+ //
+ Map<String, Object> ccc = (Map<String, Object>) result.get("ccc");
+ assertEquals(2, ccc.size());
+ assertEquals(1.0, ccc.get("x"));
+ assertEquals(3.1416, ccc.get("y"));
+ }
+
+ private class MyConstructor extends Constructor {
+ private Construct original;
+
+ public MyConstructor() {
+ original = this.yamlConstructors.get(null);
+ this.yamlConstructors.put(null, new IgnoringConstruct());
+ }
+
+ private class IgnoringConstruct extends AbstractConstruct {
+ public Object construct(Node node) {
+ if (node.getTag().startsWith("!KnownTag")) {
+ return original.construct(node);
+ } else {
+ switch (node.getNodeId()) {
+ case scalar:
+ return yamlConstructors.get(Tags.STR).construct(node);
+ case sequence:
+ return yamlConstructors.get(Tags.SEQ).construct(node);
+ case mapping:
+ return yamlConstructors.get(Tags.MAP).construct(node);
+ default:
+ throw new YAMLException("Unexpected node");
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/resources/examples/unknown-tags-example.yaml b/src/test/resources/examples/unknown-tags-example.yaml
new file mode 100644
index 0000000..c8ecf23
--- /dev/null
+++ b/src/test/resources/examples/unknown-tags-example.yaml
@@ -0,0 +1,8 @@
+--- !Foo
+aaa: !Bar1 123
+bbb: !Bar2
+- 111
+- ddd
+ccc: !Bar3
+ x: !!float 1
+ y: 3.1416