Date: Tue Dec 6 21:10:01 2011
New Revision: 1211150
URL: http://svn.apache.org/viewvc?rev=1211150&view=rev
Log:
use an interface for extensible writing
Modified:
labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java
labs/noggit/src/test/java/org/apache/noggit/TestJSONWriter.java
Modified: labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java?rev=1211150&r1=1211149&r2=1211150&view=diff
==============================================================================
--- labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java (original)
+++ labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java Tue Dec 6 21:10:01 2011
@@ -24,9 +24,15 @@ import java.util.*;
* @version $Id$
*/
public class JSONWriter {
- int level;
- int indent;
- final CharArr out;
+
+ /** Implement this interface on your class to support serialization */
+ public static interface Writable {
+ public void write(JSONWriter writer);
+ }
+
+ protected int level;
+ protected int indent;
+ protected final CharArr out;
/**
* @param out the CharArr to write the output to.
@@ -81,7 +87,10 @@ public class JSONWriter {
write(Arrays.asList((Object[])o));
} else if (o instanceof Boolean) {
write(((Boolean)o).booleanValue());
- } else if (o instanceof int[]) {
+ } else if (o instanceof Writable) {
+ ((Writable) o).write(this);
+ }
+ else if (o instanceof int[]) {
write((int[])o);
} else if (o instanceof float[]) {
write((float[])o);
@@ -102,6 +111,7 @@ public class JSONWriter {
}
}
+ /** Override this method for custom handling of unknown classes. Also see the Writable interface. */
public void handleUnknownClass(Object o) {
writeString(out.toString());
}
Modified: labs/noggit/src/test/java/org/apache/noggit/TestJSONWriter.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/test/java/org/apache/noggit/TestJSONWriter.java?rev=1211150&r1=1211149&r2=1211150&view=diff
==============================================================================
--- labs/noggit/src/test/java/org/apache/noggit/TestJSONWriter.java (original)
+++ labs/noggit/src/test/java/org/apache/noggit/TestJSONWriter.java Tue Dec 6 21:10:01 2011
@@ -20,10 +20,7 @@ package org.apache.noggit;
import junit.framework.TestCase;
import java.io.IOException;
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
/**
* @author yonik
@@ -63,4 +60,18 @@ public class TestJSONWriter extends Test
test("['\\r\\n\\u0000\\'']", L("\r\n\u0000\""),2);
}
+
+ public static class Custom implements JSONWriter.Writable {
+ public void write(JSONWriter writer) {
+ Map val = new LinkedHashMap();
+ val.put("a",1);
+ val.put("b",2);
+ writer.write(val);
+ }
+ }
+
+ public void testWritable() throws Exception {
+ test("[{'a':1,'b':2}]", L(new Custom()), -1);
+ test("[10,{'a':1,'b':2},20]", L(10, new Custom(), 20), -1);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org
No comments:
Post a Comment