1414
1515package zuo .biao .apijson ;
1616
17+ import static zuo .biao .apijson .StringUtil .UTF_8 ;
18+
19+ import java .io .UnsupportedEncodingException ;
20+ import java .net .URLDecoder ;
21+ import java .net .URLEncoder ;
22+
23+
1724/**encapsulator for request JSONObject
1825 * @author Lemon
1926 * @see #toArray
@@ -28,18 +35,45 @@ public class JSONRequest extends JSONObject {
2835 public JSONRequest () {
2936 super ();
3037 }
38+ /**
39+ * encode = true
40+ * {@link #JSONRequest(String, Object)}
41+ * @param object
42+ */
3143 public JSONRequest (Object object ) {
3244 this (null , object );
3345 }
46+ /**
47+ * encode = true
48+ * {@link #JSONRequest(String, Object, boolean)}
49+ * @param name
50+ * @param object
51+ */
3452 public JSONRequest (String name , Object object ) {
53+ this (name , object , true );
54+ }
55+ /**
56+ * {@link #JSONRequest(String, Object, boolean)}
57+ * @param object
58+ * @param encode
59+ */
60+ public JSONRequest (Object object , boolean encode ) {
61+ this (null , object , encode );
62+ }
63+ /**
64+ * {@link #put(String, Object, boolean)}
65+ * @param name
66+ * @param object
67+ * @param encode
68+ */
69+ public JSONRequest (String name , Object object , boolean encode ) {
3570 this ();
36- put (name , object );
71+ put (name , object , encode );
3772 }
38-
39-
40-
41- public static final String KEY_TAG = "tag" ;//放最外层及[]里的不会和其它字段冲突
42- public static final String KEY_COLUMNS = "@columns" ;//只有放在请求table的Object里的关键字才用@key形式
73+
74+
75+
76+ public static final String KEY_TAG = "tag" ;
4377 /**set tag
4478 * @param tag
4579 * @return
@@ -52,17 +86,6 @@ public String getTag() {
5286 return getString (KEY_TAG );
5387 }
5488
55- /**set columns need to be returned
56- * @param columns "column0,column1,column2..."
57- * @return
58- */
59- public JSONObject setColumns (String columns ) {
60- put (KEY_COLUMNS , columns );
61- return this ;
62- }
63- public String getColumns () {
64- return getString (KEY_COLUMNS );
65- }
6689
6790
6891
@@ -87,9 +110,9 @@ public int getPage() {
87110 return getIntValue (KEY_PAGE );
88111 }
89112 //array object >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
90-
91-
92-
113+
114+
115+
93116
94117 /**
95118 * @param value
@@ -106,20 +129,74 @@ public Object putPath(String key, String... parts) {
106129 return put (key , path );
107130 }
108131
132+ /**
133+ * decode = true
134+ * @param key
135+ * return {@link #get(Object, boolean)}
136+ */
137+ @ Override
138+ public Object get (Object key ) {
139+ return get (key , true );
140+ }
141+ /**
142+ * @param key
143+ * @param decode if decode && value instanceof String, value = URLDecoder.decode((String) value, UTF_8)
144+ * @return
145+ */
146+ public Object get (Object key , boolean decode ) {
147+ Object value = super .get (key );
148+ if (decode && value instanceof String ) {
149+ try {
150+ return URLDecoder .decode ((String ) value , UTF_8 );
151+ } catch (UnsupportedEncodingException e ) {
152+ e .printStackTrace ();
153+ }
154+ }
155+ return value ;
156+ }
109157
110- /**put(value.getClass().getSimpleName(), value);
158+ /**
159+ * encode = true
111160 * @param value
112- * @return
161+ * @return {@link #put(String, Object)}
113162 */
114163 public Object put (Object value ) {
115- return put (null , value );
164+ return put (value , true );
165+ }
166+ /**
167+ * @param value
168+ * @param encode
169+ * @return {@link #put(String, Object, boolean)}
170+ */
171+ public Object put (Object value , boolean encode ) {
172+ return put (null , value , encode );
116173 }
117174 /**
118- * return putWithEncode(key, value);
175+ * encode = true
176+ * @param key
177+ * @param value
178+ * return {@link #put(String, Object, boolean)}
119179 */
120180 @ Override
121181 public Object put (String key , Object value ) {
122- return super .put (StringUtil .isNotEmpty (key , true ) ? key : value .getClass ().getSimpleName (), JSON .parseObject (value ));
182+ return put (key , value , true );
183+ }
184+ /**
185+ * @param key => StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName()
186+ * @param value URLEncoder.encode((String) value, UTF_8);
187+ * @param encode if value instanceof String >> value = URLEncoder.encode((String) value, UTF_8);
188+ * @return
189+ */
190+ public Object put (String key , Object value , boolean encode ) {
191+ if (encode && value instanceof String ) {
192+ try {
193+ value = URLEncoder .encode ((String ) value , UTF_8 );
194+ //just encode /, not need to encode [] ? URLEncoder.encode(key, UTF_8)
195+ } catch (UnsupportedEncodingException e ) {
196+ e .printStackTrace ();
197+ }
198+ }
199+ return super .put (StringUtil .isNotEmpty (key , true ) ? key : value .getClass ().getSimpleName (), value );
123200 }
124201
125202
@@ -142,7 +219,6 @@ public JSONRequest toArray(int count, int page, String name) {
142219 }
143220
144221
145-
146222 /**设置搜索
147223 * @param key
148224 * @param value
@@ -162,7 +238,7 @@ public void putSearch(String key, String value, int type) {
162238 if (key .endsWith ("$" ) == false ) {
163239 key += "$" ;
164240 }
165- put (key , getSearch (value , type ));
241+ put (key , getSearch (value , type ), true );
166242 }
167243
168244 public static final int SEARCH_TYPE_CONTAIN_FULL = 0 ;
@@ -245,4 +321,5 @@ public static String getSearch(String key, int type, boolean ignoreCase) {
245321 }
246322 }
247323
324+
248325}
0 commit comments