Skip to content
Browse files

Date support added in JsonWriter

  • Loading branch information...
1 parent e15023c commit e62376126a9a02f0f4a7d3fd72711990a2c33c09 @bbansal bbansal committed Apr 14, 2009
Showing with 27 additions and 9 deletions.
  1. +27 −9 src/java/voldemort/serialization/json/JsonWriter.java
View
36 src/java/voldemort/serialization/json/JsonWriter.java
@@ -18,6 +18,7 @@
import java.io.IOException;
import java.io.Writer;
+import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -36,16 +37,26 @@ public JsonWriter(Writer writer) {
this.writer = writer;
}
- public void write(Object o) throws IOException {
+ public void write(Object o) {
- if(o instanceof Map) {
- writeMap((Map<String, Object>) o);
- } else if(o instanceof List) {
- writeList((List<Object>) o);
- } else if(o instanceof String) {
- writer.write((String) o);
- } else {
- writer.write(o.toString());
+ try {
+ if(null == o) {
+ writer.write("null");
+ } else if(o instanceof Map) {
+ writeMap((Map<String, Object>) o);
+ } else if(o instanceof List) {
+ writeList((List<Object>) o);
+ } else if(o instanceof Date) {
+ writeDate((Date) o);
+ } else if(o instanceof String) {
+ writer.write("'");
+ writer.write((String) o);
+ writer.write("'");
+ } else {
+ writer.write(o.toString());
+ }
+ } catch(Exception e) {
+ throw new RuntimeException("JsonWriter failed to write Object(" + o + ") as String", e);
}
}
@@ -54,7 +65,10 @@ public void writeMap(Map<String, Object> values) throws IOException {
Set<Map.Entry<String, Object>> entrySet = values.entrySet();
int index = 0;
for(Map.Entry<String, Object> entry: entrySet) {
+ writer.write("'");
writer.write(entry.getKey());
+ writer.write("'");
+
writer.write(':');
// write Object
@@ -80,4 +94,8 @@ public void writeList(List<Object> list) throws IOException {
}
writer.write(']');
}
+
+ public void writeDate(Date date) throws IOException {
+ writer.write("" + date.getTime());
+ }
}

0 comments on commit e623761

Please sign in to comment.
Something went wrong with that request. Please try again.