Permalink
Browse files

恢复 Json 的那个去掉重复的功能,见 #162 , @wendal 还原的时候弄的太多了 :P

  • Loading branch information...
zozoh committed May 17, 2012
1 parent 89c4971 commit d84c84e27211d19f08e8574c2a608c641dcb6dbe
@@ -36,16 +36,16 @@
private static String NL = "\n";
private JsonFormat format;
-
+
private Writer writer;
-
+
private Set<Object> memo = new HashSet<Object>();
-
+
public void render(Object obj) throws IOException {
if (null == obj) {
writer.write("null");
} else if (obj instanceof JsonRender) {
- ((JsonRender)obj).render(null);
+ ((JsonRender) obj).render(null);
} else if (obj instanceof Class) {
string2Json(((Class<?>) obj).getName());
} else if (obj instanceof Mirror) {
@@ -84,12 +84,14 @@ else if (obj.getClass().isArray()) {
}
// 普通 Java 对象
else {
+ memo.add(obj);
pojo2Json(obj);
+ memo.remove(obj);
}
}
}
}
-
+
public JsonRenderImpl(Writer writer, JsonFormat format) {
this.format = format;
this.writer = writer;
@@ -118,13 +120,13 @@ private void appendPairSep() throws IOException {
}
protected void appendPair(boolean needPairEnd, String name, Object value) throws IOException {
- appendPairBegin();
- appendName(name);
- appendPairSep();
- render(value);
- if(needPairEnd){
- appendPairEnd();
- }
+ appendPairBegin();
+ appendName(name);
+ appendPairSep();
+ render(value);
+ if (needPairEnd) {
+ appendPairEnd();
+ }
}
private boolean isIgnore(String name, Object value) {
@@ -232,8 +234,6 @@ private void pojo2Json(Object obj) throws IOException {
if (mirror.isPojo()) {
if (memo.contains(value))
value = null;
- else
- memo.add(value);
}
}
// 加入输出列表 ...
@@ -244,15 +244,15 @@ private void pojo2Json(Object obj) throws IOException {
}
writeItem(list);
}
-
- private void writeItem(List<Pair> list) throws IOException{
+
+ private void writeItem(List<Pair> list) throws IOException {
Iterator<Pair> it = list.iterator();
while (it.hasNext()) {
Pair p = it.next();
appendPair(it.hasNext(), p.name, p.value);
}
- decreaseFormatIndent();
- appendBraceEnd();
+ decreaseFormatIndent();
+ appendBraceEnd();
}
private void decreaseFormatIndent() {
@@ -299,8 +299,6 @@ private void string2Json(String s) throws IOException {
}
}
-
-
private void array2Json(Object obj) throws IOException {
writer.append('[');
int len = Array.getLength(obj) - 1;
@@ -320,11 +318,10 @@ private void coll2Json(Collection iterable) throws IOException {
writer.append('[');
for (Iterator<?> it = iterable.iterator(); it.hasNext();) {
render(it.next());
- if (it.hasNext()){
- appendPairEnd();
- writer.append(' ');
- }
- else
+ if (it.hasNext()) {
+ appendPairEnd();
+ writer.append(' ');
+ } else
break;
}
writer.append(']');
@@ -10,6 +10,6 @@
CustomizedJsonTest.class,
JsonCharsetTest.class,
JsonEntityTest.class,
- GenericTest.class
- })
+ GenericTest.class,
+ JsonRecursiveTest.class})
public class AllJson {}
@@ -0,0 +1,137 @@
+package org.nutz.json;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import org.nutz.lang.Each;
+import org.nutz.lang.ExitLoop;
+import org.nutz.lang.Lang;
+
+public class JsonRecursiveTest {
+
+ public static class R {
+
+ public R() {}
+
+ public R(int id) {
+ this.id = id;
+ }
+
+ public R(int id, R r) {
+ this.id = id;
+ this.recur = r;
+ }
+
+ public int id;
+
+ public R recur;
+
+ public R[] parents;
+
+ public int hashCode() {
+ return id;
+ }
+ }
+
+ @Test
+ public void test_two_render() {
+ R r1 = new R(1);
+ R r2 = new R(2);
+ r1.recur = new R(80);
+ r2.recur = r1.recur;
+
+ R[] rs = Lang.array(r1, r2);
+
+ String json = Json.toJson(rs);
+
+ R[] rs2 = Json.fromJson(R[].class, json);
+ assertEquals(2, rs2.length);
+ assertEquals(80, rs2[0].recur.id);
+ assertEquals(80, rs2[1].recur.id);
+ }
+
+ @Test
+ public void test_nutz_json_parents() {
+ R r = new R(1);
+ r.parents = Lang.array(new R(-1), new R(-2), new R(-3));
+ r.recur = r.parents[1];
+
+ String json = Json.toJson(r);
+
+ R r2 = Json.fromJson(R.class, json);
+ assertEquals(r.id, r2.id);
+ assertEquals(r.parents[0].id, r2.parents[0].id);
+ assertEquals(r.parents[1].id, r2.parents[1].id);
+ assertEquals(r.parents[2].id, r2.parents[2].id);
+ }
+
+ // TODO 这个功能被还原没有了,稍等以后把功能补回来吧
+ // @Test
+ public void test_nutz_json_recur() {
+ R r = new R(1, null);
+ r.recur = r;
+
+ String json = Json.toJson(r);
+ R r2 = Json.fromJson(R.class, json);
+ assertNull(r2.recur);
+
+ json = Json.toJson(r, JsonFormat.compact());
+ r2 = Json.fromJson(R.class, json);
+ assertTrue(r2 == r2.recur);
+ }
+
+ @Test
+ public void testSimpleRecur() {
+ R r1 = new R(1, null);
+ R r2 = new R(2, r1);
+ r1.recur = r2;
+ R rr = Json.fromJson(R.class, Lang.inr(Json.toJson(r1)));
+ assertEquals(r1.id, rr.id);
+ assertEquals(r2.id, rr.recur.id);
+ assertNull(rr.recur.recur);
+ }
+
+ public static class Node {
+
+ public Node() {}
+
+ public Node(int id, Node parent) {
+ this.id = id;
+ this.parent = parent;
+ }
+
+ public int id;
+ public Node parent;
+ public Node[] children;
+ }
+
+ @Test
+ public void testSimpleNode() {
+ int i = 1;
+ Node root = new Node(i++, null);
+ Node chd1 = new Node(i++, root);
+ Node chd11 = new Node(i++, chd1);
+ Node chd12 = new Node(i++, chd1);
+ Node chd2 = new Node(i++, root);
+ root.children = Lang.array(chd1, chd2);
+ chd1.children = Lang.array(chd11, chd12);
+
+ String json = Json.toJson(root, JsonFormat.nice());
+ final Node nd = Json.fromJson(Node.class, Lang.inr(json));
+ assertEquals(root.id, nd.id);
+ assertEquals(root.children.length, nd.children.length);
+ Lang.each(root.children, new Each<Node>() {
+ public void invoke(int index, Node ele, int size) throws ExitLoop {
+ assertNull(nd.children[index].parent);
+ assertEquals(ele.id, nd.children[index].id);
+ }
+ });
+ Lang.each(root.children[0].children, new Each<Node>() {
+ public void invoke(int index, Node ele, int size) throws ExitLoop {
+ assertNull(nd.children[index].parent);
+ assertEquals(ele.id, nd.children[0].children[index].id);
+ }
+ });
+ }
+}

0 comments on commit d84c84e

Please sign in to comment.