Add example to load JodaTime
diff --git a/src/test/java/examples/jodatime/JodaTimeContructor.java b/src/test/java/examples/jodatime/JodaTimeContructor.java
new file mode 100644
index 0000000..fb828e5
--- /dev/null
+++ b/src/test/java/examples/jodatime/JodaTimeContructor.java
@@ -0,0 +1,38 @@
+/**
+ * Copyright (c) 2008-2010, http://code.google.com/p/snakeyaml/
+ *
+ * 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.jodatime;
+
+import java.util.Date;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.yaml.snakeyaml.constructor.SafeConstructor;
+import org.yaml.snakeyaml.nodes.Node;
+import org.yaml.snakeyaml.nodes.Tag;
+
+public class JodaTimeContructor extends SafeConstructor {
+ public JodaTimeContructor() {
+ this.yamlConstructors.put(Tag.TIMESTAMP, new ConstructJodaTimestamp());
+ }
+
+ private class ConstructJodaTimestamp extends ConstructYamlTimestamp {
+ public Object construct(Node node) {
+ Date date = (Date) super.construct(node);
+ return new DateTime(date, DateTimeZone.UTC);
+ }
+ }
+}
diff --git a/src/test/java/examples/jodatime/JodaTimeExampleTest.java b/src/test/java/examples/jodatime/JodaTimeExampleTest.java
index f1e8533..f1ec384 100644
--- a/src/test/java/examples/jodatime/JodaTimeExampleTest.java
+++ b/src/test/java/examples/jodatime/JodaTimeExampleTest.java
@@ -24,11 +24,13 @@
import org.joda.time.DateTimeZone;
import org.yaml.snakeyaml.Dumper;
import org.yaml.snakeyaml.DumperOptions;
+import org.yaml.snakeyaml.Loader;
import org.yaml.snakeyaml.Yaml;
public class JodaTimeExampleTest extends TestCase {
+ private static final long timestamp = 1000000000000L;
+
public void testDump() throws IOException {
- long timestamp = 1000000000000L;
DateTime time = new DateTime(timestamp, DateTimeZone.UTC);
Yaml yaml = new Yaml(new Dumper(new JodaTimeRepresenter(), new DumperOptions()));
String joda = yaml.dump(time);
@@ -37,9 +39,8 @@
}
public void testLoad() throws IOException {
- Yaml yaml = new Yaml();
- // TODO DateTime time = (DateTime) yaml.load("2001-09-09T01:46:40Z");
- Date time = (Date) yaml.load("2001-09-09T01:46:40Z");
- assertEquals(new Date(1000000000000L), time);
+ Yaml yaml = new Yaml(new Loader(new JodaTimeContructor()));
+ DateTime time = (DateTime) yaml.load("2001-09-09T01:46:40Z");
+ assertEquals(new DateTime(timestamp, DateTimeZone.UTC), time);
}
}