Skip to content

Commit 4762ee7

Browse files
committed
Server:同步eclipse版至studio版
1 parent 56070f7 commit 4762ee7

6 files changed

Lines changed: 265 additions & 34 deletions

File tree

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/JSONObject.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object) {
6868
return this;
6969
}
7070

71-
72-
71+
7372

7473

7574
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -97,5 +96,22 @@ public static boolean isWord(String key) {
9796
return StringUtil.isNotEmpty(key, false) && namePattern.matcher(key).matches();
9897
}
9998
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
99+
100+
101+
102+
public static final String KEY_COLUMNS = "@columns";//@key关键字都放这个类
100103

104+
/**set columns need to be returned
105+
* @param columns "column0,column1,column2..."
106+
* @return
107+
*/
108+
public JSONObject setColumns(String columns) {
109+
put(KEY_COLUMNS, columns);
110+
return this;
111+
}
112+
public String getColumns() {
113+
return getString(KEY_COLUMNS);
114+
}
115+
116+
101117
}

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/JSONRequest.java

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
package 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
}

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/JSONResponse.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
* @author Lemon
2929
* @see #getList
3030
* @see #toArray
31-
* @see QueryActivity#onHttpResponse
3231
* @use JSONResponse response = new JSONResponse(...);
3332
* <br> JSONArray array = JSONResponse.toArray(response.getJSONObject(KEY_ARRAY));//not a must
3433
* <br> User user = JSONResponse.getObject(response, User.class);//not a must

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/Controller.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.alibaba.fastjson.JSONObject;
2525

2626
import zuo.biao.apijson.JSON;
27-
import zuo.biao.apijson.JSONRequest;
2827
import zuo.biao.apijson.JSONResponse;
2928
import zuo.biao.apijson.RequestMethod;
3029
import zuo.biao.apijson.StringUtil;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package zuo.biao.apijson.server;
16+
17+
import zuo.biao.apijson.JSON;
18+
import zuo.biao.apijson.StringUtil;
19+
20+
/**JSONRequest for Server to replace zuo.biao.apijson.JSONRequest,
21+
* put JSON.parseObject(value) and not encode in default cases
22+
* @author Lemon
23+
* @see #put(String, Object, boolean)
24+
*/
25+
public class JSONRequest extends zuo.biao.apijson.JSONRequest {
26+
27+
private static final long serialVersionUID = -2223023180338466812L;
28+
29+
public JSONRequest() {
30+
super();
31+
}
32+
/**
33+
* encode = true
34+
* {@link #JSONRequest(String, Object)}
35+
* @param object
36+
*/
37+
public JSONRequest(Object object) {
38+
this(null, object);
39+
}
40+
/**
41+
* encode = false
42+
* {@link #JSONRequest(String, Object, boolean)}
43+
* @param name
44+
* @param object
45+
*/
46+
public JSONRequest(String name, Object object) {
47+
this(name, object, false);
48+
}
49+
/**
50+
* {@link #JSONRequest(String, Object, boolean)}
51+
* @param object
52+
* @param encode
53+
*/
54+
public JSONRequest(Object object, boolean encode) {
55+
super(object, encode);
56+
}
57+
/**
58+
* {@link #put(String, Object, boolean)}
59+
* @param name
60+
* @param object
61+
* @param encode
62+
*/
63+
public JSONRequest(String name, Object object, boolean encode) {
64+
super(name, object, encode);
65+
}
66+
67+
68+
69+
/**
70+
* decode = true
71+
* @param key
72+
* return {@link #get(Object, boolean)}
73+
*/
74+
@Override
75+
public Object get(Object key) {
76+
return get(key, false);
77+
}
78+
79+
/**
80+
* encode = true
81+
* @param value
82+
* @return {@link #put(String, Object)}
83+
*/
84+
@Override
85+
public Object put(Object value) {
86+
return put(null, value);
87+
}
88+
/**
89+
* @param value
90+
* @param encode
91+
* @return {@link #put(String, Object, boolean)}
92+
*/
93+
@Override
94+
public Object put(Object value, boolean encode) {
95+
return put(null, value, false);
96+
}
97+
/**
98+
* encode = true
99+
* @param key
100+
* @param value
101+
* return {@link #put(String, Object, boolean)}
102+
*/
103+
@Override
104+
public Object put(String key, Object value) {
105+
return put(key, value, false);
106+
}
107+
/**自定义类型必须转为JSONObject或JSONArray,否则RequestParser解析不了
108+
*/
109+
@Override
110+
public Object put(String key, Object value, boolean encode) {
111+
return super.put(StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName() //must handle key here
112+
, JSON.parseObject(value), encode);
113+
}
114+
115+
/**设置搜索
116+
* @param key
117+
* @param value
118+
*/
119+
@Override
120+
public void putSearch(String key, String value) {
121+
putSearch(key, value, SEARCH_TYPE_CONTAIN_FULL);
122+
}
123+
/**设置搜索
124+
* @param key
125+
* @param value
126+
* @param type
127+
*/
128+
@Override
129+
public void putSearch(String key, String value, int type) {
130+
put(key, getSearch(value, type));
131+
}
132+
133+
}

0 commit comments

Comments
 (0)