Add example to dump JodaTime
diff --git a/AUTHORS b/AUTHORS
index 77aaf92..66a93b6 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -23,3 +23,4 @@
Magne at edb
infinity0x
obastard
+Antony Stubbs
diff --git a/pom.xml b/pom.xml
index 3f6f335..e3d7b35 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,6 +63,12 @@
<version>1.6.2</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>1.6</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
diff --git a/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java b/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java
index 4601935..415792c 100644
--- a/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java
+++ b/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java
@@ -58,7 +58,7 @@
classTags = new HashMap<Class<? extends Object>, Tag>();
}
- private Tag getTag(Class<?> clazz, Tag defaultTag) {
+ protected Tag getTag(Class<?> clazz, Tag defaultTag) {
if (classTags.containsKey(clazz)) {
return classTags.get(clazz);
} else {
diff --git a/src/test/java/examples/jodatime/JodaTimeExampleTest.java b/src/test/java/examples/jodatime/JodaTimeExampleTest.java
new file mode 100644
index 0000000..f1e8533
--- /dev/null
+++ b/src/test/java/examples/jodatime/JodaTimeExampleTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.io.IOException;
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.yaml.snakeyaml.Dumper;
+import org.yaml.snakeyaml.DumperOptions;
+import org.yaml.snakeyaml.Yaml;
+
+public class JodaTimeExampleTest extends TestCase {
+ 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);
+ String date = new Yaml().dump(new Date(timestamp));
+ assertEquals(date, joda);
+ }
+
+ 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);
+ }
+}
diff --git a/src/test/java/examples/jodatime/JodaTimeRepresenter.java b/src/test/java/examples/jodatime/JodaTimeRepresenter.java
new file mode 100644
index 0000000..036282d
--- /dev/null
+++ b/src/test/java/examples/jodatime/JodaTimeRepresenter.java
@@ -0,0 +1,100 @@
+/**
+ * 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 org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.yaml.snakeyaml.nodes.Node;
+import org.yaml.snakeyaml.nodes.Tag;
+import org.yaml.snakeyaml.representer.Represent;
+import org.yaml.snakeyaml.representer.Representer;
+
+class JodaTimeRepresenter extends Representer {
+ public JodaTimeRepresenter() {
+ multiRepresenters.put(DateTime.class, new RepresentJodaDateTime());
+ }
+
+ private class RepresentJodaDateTime implements Represent {
+
+ public Node representData(Object data) {
+ DateTime dt = (DateTime) data;
+ int years = dt.getYear();
+ int months = dt.getMonthOfYear();
+ int days = dt.getDayOfMonth();
+ int hour24 = dt.getHourOfDay();
+ System.out.println(hour24);
+ int minutes = dt.getMinuteOfHour();
+ int seconds = dt.getSecondOfMinute();
+ int millis = dt.getMillisOfSecond();
+ StringBuilder buffer = new StringBuilder(String.valueOf(years));
+ buffer.append("-");
+ if (months < 10) {
+ buffer.append("0");
+ }
+ buffer.append(String.valueOf(months));
+ buffer.append("-");
+ if (days < 10) {
+ buffer.append("0");
+ }
+ buffer.append(String.valueOf(days));
+ buffer.append("T");
+ if (hour24 < 10) {
+ buffer.append("0");
+ }
+ buffer.append(String.valueOf(hour24));
+ buffer.append(":");
+ if (minutes < 10) {
+ buffer.append("0");
+ }
+ buffer.append(String.valueOf(minutes));
+ buffer.append(":");
+ if (seconds < 10) {
+ buffer.append("0");
+ }
+ buffer.append(String.valueOf(seconds));
+ if (millis > 0) {
+ if (millis < 10) {
+ buffer.append(".00");
+ } else if (millis < 100) {
+ buffer.append(".0");
+ } else {
+ buffer.append(".");
+ }
+ buffer.append(String.valueOf(millis));
+ }
+ if (DateTimeZone.UTC.equals(dt.getZone())) {
+ buffer.append("Z");
+ } else {
+ // Get the Offset from GMT taking DST into account
+ int gmtOffset = dt.getZone().getOffset(dt);
+ int minutesOffset = gmtOffset / (60 * 1000);
+ int hoursOffset = minutesOffset / 60;
+ int partOfHour = minutesOffset % 60;
+ if (hoursOffset > 0) {
+ buffer.append("");
+ }
+ buffer.append(hoursOffset + ":");
+ if (partOfHour < 10) {
+ buffer.append("0" + partOfHour);
+ } else {
+ buffer.append(partOfHour);
+ }
+ }
+ return representScalar(getTag(data.getClass(), Tag.TIMESTAMP), buffer.toString(), null);
+ }
+ }
+
+}
\ No newline at end of file