• R/O
  • SSH
  • HTTPS

jsonic: コミット


コミットメタ情報

リビジョン1850 (tree)
日時2014-12-14 20:50:54
作者hizuno

ログメッセージ

(メッセージはありません)

変更サマリ

差分

--- trunk/jsonic-1.3/src/net/arnx/jsonic/JSON.java (revision 1849)
+++ trunk/jsonic-1.3/src/net/arnx/jsonic/JSON.java (revision 1850)
@@ -1008,7 +1008,7 @@
10081008 }
10091009
10101010 context.enter(ROOT, null);
1011- source = context.preformatInternal(source);
1011+ source = context.preformatInternal((source != null) ? source.getClass() : Object.class, source);
10121012 context.formatInternal(source, out);
10131013 context.exit();
10141014 out.flush();
@@ -1033,6 +1033,10 @@
10331033 return new JSONWriter(new Context(), out);
10341034 }
10351035
1036+ protected Object preformatNull(Context context, Type type) throws Exception {
1037+ return null;
1038+ }
1039+
10361040 /**
10371041 * Converts Any Java Object to JSON recognizable Java object before format.
10381042 *
@@ -1605,20 +1609,21 @@
16051609 return sb.toString();
16061610 }
16071611
1608- final Object preformatInternal(Object value) {
1609- if (value == null) {
1610- return null;
1611- } else if (getDepth() > getMaxDepth()) {
1612- return null;
1613- } else if (JSON.this.getClass() != JSON.class) {
1614- try {
1612+ final Object preformatInternal(Type type, Object value) {
1613+ try {
1614+ if (value == null) {
1615+ return preformatNull(this, type);
1616+ } else if (getDepth() > getMaxDepth()) {
1617+ return null;
1618+ } else if (JSON.this.getClass() != JSON.class) {
16151619 return preformat(this, value);
1616- } catch (Exception e) {
1617- throw new JSONException(getMessage("json.format.ConversionError", value, this),
1618- JSONException.PREFORMAT_ERROR, e);
1620+ } else {
1621+ return value;
16191622 }
1623+ } catch (Exception e) {
1624+ throw new JSONException(getMessage("json.format.ConversionError", value, this),
1625+ JSONException.PREFORMAT_ERROR, e);
16201626 }
1621- return value;
16221627 }
16231628
16241629 final Formatter formatInternal(final Object src, final OutputSource ap) throws IOException {
--- trunk/jsonic-1.3/src/net/arnx/jsonic/Formatter.java (revision 1849)
+++ trunk/jsonic-1.3/src/net/arnx/jsonic/Formatter.java (revision 1850)
@@ -692,6 +692,7 @@
692692
693693 Class<?> lastClass = null;
694694 Formatter lastFormatter = null;
695+ Class<?> cType = array.getClass().getComponentType();
695696
696697 out.append('[');
697698 int i = 0;
@@ -706,7 +707,7 @@
706707 context.appendIndent(out, context.getDepth() + 1);
707708 }
708709 context.enter(i, hint);
709- item = context.preformatInternal(item);
710+ item = context.preformatInternal(cType, item);
710711 if (item == null) {
711712 NullFormatter.INSTANCE.format(context, src, item, out);
712713 } else if (hint == null) {
@@ -891,7 +892,7 @@
891892 context.appendIndent(out, context.getDepth() + 1);
892893 }
893894 context.enter(count, hint);
894- item = context.preformatInternal(item);
895+ item = context.preformatInternal((item != null) ? item.getClass() : Object.class, item);
895896 if (item == null) {
896897 NullFormatter.INSTANCE.format(context, src, item, out);
897898 } else if (hint == null) {
@@ -948,7 +949,7 @@
948949 context.appendIndent(out, context.getDepth() + 1);
949950 }
950951 context.enter(count, hint);
951- item = context.preformatInternal(item);
952+ item = context.preformatInternal((item != null) ? item.getClass() : Object.class, item);
952953 if (item == null) {
953954 NullFormatter.INSTANCE.format(context, src, item, out);
954955 } else if (hint == null) {
@@ -1022,7 +1023,7 @@
10221023 context.appendIndent(out, context.getDepth() + 1);
10231024 }
10241025 context.enter(count, hint);
1025- item = context.preformatInternal(item);
1026+ item = context.preformatInternal((item != null) ? item.getClass() : Object.class, item);
10261027 if (item == null) {
10271028 NullFormatter.INSTANCE.format(context, src, item, out);
10281029 } else if (hint == null) {
@@ -1084,7 +1085,7 @@
10841085 out.append(':');
10851086 if (context.isPrettyPrint()) out.append(' ');
10861087 context.enter(key, hint);
1087- value = context.preformatInternal(value);
1088+ value = context.preformatInternal((value != null) ? value.getClass() : Object.class, value);
10881089 if (value == null) {
10891090 NullFormatter.INSTANCE.format(context, src, value, out);
10901091 } else if (hint == null) {
@@ -1157,7 +1158,7 @@
11571158 context.enter(key, hint);
11581159 key = null;
11591160
1160- value = context.preformatInternal(value);
1161+ value = context.preformatInternal(prop.getReadGenericType(), value);
11611162 if (value == null) {
11621163 NullFormatter.INSTANCE.format(context, src, value, out);
11631164 } else if (hint == null) {
@@ -1310,7 +1311,7 @@
13101311 if (context.isPrettyPrint()) out.append(' ');
13111312 context.enter(key, hint);
13121313 key = null;
1313- value = context.preformatInternal(value);
1314+ value = context.preformatInternal(dp.getType(), value);
13141315 context.formatInternal(value, out);
13151316 context.exit();
13161317 count++;
@@ -1412,7 +1413,7 @@
14121413 context.appendIndent(out, context.getDepth() + 1);
14131414 }
14141415 context.enter(i + 2, hint);
1415- value = context.preformatInternal(value);
1416+ value = context.preformatInternal(value.getClass(), value);
14161417 context.formatInternal(value, out);
14171418 context.exit();
14181419 }
--- trunk/jsonic-1.3/src/net/arnx/jsonic/JSONWriter.java (revision 1849)
+++ trunk/jsonic-1.3/src/net/arnx/jsonic/JSONWriter.java (revision 1850)
@@ -1,12 +1,12 @@
1-/*
1+/*
22 * Copyright 2014 Hidekatsu Izuno
3- *
3+ *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
7- *
7+ *
88 * http://www.apache.org/licenses/LICENSE-2.0
9- *
9+ *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,14 +23,14 @@
2323 public class JSONWriter {
2424 private Context context;
2525 private OutputSource out;
26-
26+
2727 private Stack stack = new Stack();
28-
28+
2929 JSONWriter(Context context, OutputSource out) {
3030 this.context = context;
3131 this.out = out;
3232 }
33-
33+
3434 public JSONWriter beginObject() throws IOException {
3535 State state = stack.peek();
3636 if(state == null) {
@@ -43,7 +43,7 @@
4343 context.enter(state.name);
4444 } else {
4545 throw new JSONException(
46- context.getMessage("json.format.IllegalMethodCallError", "beginObject"),
46+ context.getMessage("json.format.IllegalMethodCallError", "beginObject"),
4747 JSONException.FORMAT_ERROR);
4848 }
4949 } else if (state.type == JSONDataType.ARRAY) {
@@ -56,17 +56,17 @@
5656 } else {
5757 throw new IllegalStateException();
5858 }
59-
59+
6060 stack.push(JSONDataType.OBJECT);
6161 out.append('{');
6262 return this;
6363 }
64-
64+
6565 public JSONWriter endObject() throws IOException {
6666 State state = stack.peek();
6767 if(state == null) {
6868 throw new JSONException(
69- context.getMessage("json.format.IllegalMethodCallError", "endObject"),
69+ context.getMessage("json.format.IllegalMethodCallError", "endObject"),
7070 JSONException.FORMAT_ERROR);
7171 } else if (state.type == JSONDataType.OBJECT) {
7272 if (context.isPrettyPrint() && state.index > 0) {
@@ -75,14 +75,14 @@
7575 }
7676 } else {
7777 throw new JSONException(
78- context.getMessage("json.format.ArrayNotClosedError"),
78+ context.getMessage("json.format.ArrayNotClosedError"),
7979 JSONException.FORMAT_ERROR);
8080 }
8181 stack.pop();
82-
82+
8383 out.append('}');
8484 context.exit();
85-
85+
8686 if (stack.size == 0) {
8787 out.flush();
8888 }
@@ -101,7 +101,7 @@
101101 context.enter(state.name);
102102 } else {
103103 throw new JSONException(
104- context.getMessage("json.format.IllegalMethodCallError", "beginArray"),
104+ context.getMessage("json.format.IllegalMethodCallError", "beginArray"),
105105 JSONException.FORMAT_ERROR);
106106 }
107107 } else if (state.type == JSONDataType.ARRAY) {
@@ -114,17 +114,17 @@
114114 } else {
115115 throw new IllegalStateException();
116116 }
117-
117+
118118 stack.push(JSONDataType.ARRAY);
119119 out.append('[');
120120 return this;
121121 }
122-
122+
123123 public JSONWriter endArray() throws IOException {
124124 State state = stack.peek();
125125 if(state == null) {
126126 throw new JSONException(
127- context.getMessage("json.format.IllegalMethodCallError", "endArray"),
127+ context.getMessage("json.format.IllegalMethodCallError", "endArray"),
128128 JSONException.FORMAT_ERROR);
129129 } else if (state.type == JSONDataType.ARRAY) {
130130 if (context.isPrettyPrint() && state.index > 0) {
@@ -133,29 +133,29 @@
133133 }
134134 } else {
135135 throw new JSONException(
136- context.getMessage("json.format.ObjectNotClosedError"),
136+ context.getMessage("json.format.ObjectNotClosedError"),
137137 JSONException.FORMAT_ERROR);
138138 }
139139 stack.pop();
140-
140+
141141 out.append(']');
142142 context.exit();
143-
143+
144144 if (stack.size == 0) {
145145 out.flush();
146146 }
147147 return this;
148148 }
149-
149+
150150 public JSONWriter name(String name) throws IOException {
151151 State state = stack.peek();
152152 if (state == null) {
153153 throw new JSONException(
154- context.getMessage("json.format.IllegalMethodCallError", "name"),
154+ context.getMessage("json.format.IllegalMethodCallError", "name"),
155155 JSONException.FORMAT_ERROR);
156156 } else if (state.type == JSONDataType.OBJECT) {
157157 state.name = name;
158-
158+
159159 if (state.index > 0) out.append(',');
160160 if (context.isPrettyPrint()) {
161161 out.append('\n');
@@ -163,19 +163,19 @@
163163 }
164164 } else {
165165 throw new JSONException(
166- context.getMessage("json.format.IllegalMethodCallError", "name"),
166+ context.getMessage("json.format.IllegalMethodCallError", "name"),
167167 JSONException.FORMAT_ERROR);
168168 }
169-
169+
170170 StringFormatter.serialize(context, name, out);
171171 out.append(':');
172172 if (context.isPrettyPrint()) {
173173 out.append(' ');
174174 }
175-
175+
176176 return this;
177177 }
178-
178+
179179 public JSONWriter value(Object value) throws IOException {
180180 State state = stack.peek();
181181 if(state == null) {
@@ -188,7 +188,7 @@
188188 context.enter(state.name);
189189 } else {
190190 throw new JSONException(
191- context.getMessage("json.format.IllegalMethodCallError", "value"),
191+ context.getMessage("json.format.IllegalMethodCallError", "value"),
192192 JSONException.FORMAT_ERROR);
193193 }
194194 } else if (state.type == JSONDataType.ARRAY) {
@@ -201,30 +201,30 @@
201201 } else {
202202 throw new IllegalStateException();
203203 }
204-
205- value = context.preformatInternal(value);
204+
205+ value = context.preformatInternal((value != null) ? value.getClass() : Object.class, value);
206206 context.formatInternal(value, out);
207207 context.exit();
208-
208+
209209 if (state != null) {
210210 state.index++;
211211 }
212-
212+
213213 if (stack.size == 0) {
214214 out.flush();
215215 }
216216 return this;
217217 }
218-
218+
219219 public JSONWriter flush() throws IOException {
220220 out.flush();
221221 return this;
222222 }
223-
223+
224224 static final class Stack {
225225 private int size = 0;
226226 private State[] list = new State[8];
227-
227+
228228 public State push(JSONDataType type) {
229229 size++;
230230 if (size >= list.length) {
@@ -244,7 +244,7 @@
244244 state.type = type;
245245 return state;
246246 }
247-
247+
248248 public State peek() {
249249 if (size < list.length) {
250250 return list[size];
@@ -252,7 +252,7 @@
252252 return null;
253253 }
254254 }
255-
255+
256256 public State pop() {
257257 if (size >= 0 && size < list.length) {
258258 return list[size--];
@@ -260,12 +260,12 @@
260260 return null;
261261 }
262262 }
263-
263+
264264 public int size() {
265265 return size;
266266 }
267267 }
268-
268+
269269 static final class State {
270270 public JSONDataType type;
271271 public String name;
--- trunk/jsonic-1.3/src/net/arnx/jsonic/Converter.java (revision 1849)
+++ trunk/jsonic-1.3/src/net/arnx/jsonic/Converter.java (revision 1850)
@@ -160,7 +160,7 @@
160160 public Object convert(Context context, Object value, Class<?> c, Type t) throws Exception {
161161 Context context2 = context.copy();
162162 context2.skipHint = context.getHint();
163- value = context2.preformatInternal(value);
163+ value = context2.preformatInternal((value != null) ? value.getClass() : Object.class, value);
164164 StringBuilderOutputSource out = new StringBuilderOutputSource(200);
165165 try {
166166 context2.formatInternal(value, out);
--- trunk/jsonic-1.3/docs/index.html (revision 1849)
+++ trunk/jsonic-1.3/docs/index.html (revision 1850)
@@ -865,10 +865,11 @@
865865
866866 <h2 id="releasenote">リリースノート</h2>
867867
868-<h3>2014/12/14 version 1.3.7</h3>
868+<h3>2014/12/14 version 1.3.7 ベータ1</h3>
869869 <ul>
870870 <li>[不具合修正] JSONHint に type を指定しても、type のプロパティに値が設定されない問題を修正しました。</li>
871871 <li>[不具合修正] JSON object に PrittyPrint モードで encode/format する際、閉じ括弧のインデントがずれる問題を修正しました。</li>
872+<li>[機能追加] null に対して preformat は動作しない問題に対応するため preformatNull メソッドを追加しました。</li>
872873 </ul>
873874
874875 <h3>2014/10/26 version 1.3.6</h3>
--- trunk/jsonic-1.3/pom.xml (revision 1849)
+++ trunk/jsonic-1.3/pom.xml (revision 1850)
@@ -7,7 +7,7 @@
77 <artifactId>jsonic</artifactId>
88 <packaging>jar</packaging>
99 <name>jsonic</name>
10- <version>1.3.6</version>
10+ <version>1.3.7</version>
1111 <description>simple json encoder/decoder for java</description>
1212 <url>http://jsonic.sourceforge.jp/</url>
1313 <licenses>
--- trunk/jsonic-1.3/test/net/arnx/jsonic/JSONBugTest.java (revision 1849)
+++ trunk/jsonic-1.3/test/net/arnx/jsonic/JSONBugTest.java (revision 1850)
@@ -2,6 +2,8 @@
22
33 import static org.junit.Assert.assertEquals;
44
5+import java.lang.reflect.Type;
6+
57 import org.junit.Test;
68
79 import com.google.inject.internal.Objects;
@@ -33,6 +35,22 @@
3335 assertEquals(test, JSON.decode("{\"test\":{\"hoge\":\"bar\"}}", TestClass.class));
3436 }
3537
38+ public void testPreformatNull() {
39+ JSON json = new JSON() {
40+ @Override
41+ protected Object preformatNull(Context context, Type type) throws Exception {
42+ if (type == String.class) {
43+ return "";
44+ }
45+
46+ return super.preformatNull(context, type);
47+ }
48+ };
49+
50+ assertEquals("null", json.format(null));
51+ assertEquals("{\"str\":\"\",\"num\":null,\"bool\":null}", json.format(new PreformatNullBean()));
52+ }
53+
3654 public static class TestClass {
3755 @JSONHint(type = SubClass.class)
3856 public SuperClass test = new SubClass();
@@ -125,4 +143,15 @@
125143 return "SubClass [hoge=" + hoge + "]";
126144 }
127145 }
146+
147+ public static class PreformatNullBean {
148+ public PreformatNullBean() {
149+ }
150+
151+ public String str;
152+
153+ public Integer num;
154+
155+ public Boolean bool;
156+ }
128157 }
旧リポジトリブラウザで表示