1414
1515package zuo .biao .apijson ;
1616
17+ import static zuo .biao .apijson .StringUtil .UTF_8 ;
1718import static zuo .biao .apijson .StringUtil .bigAlphaPattern ;
1819import static zuo .biao .apijson .StringUtil .namePattern ;
1920
21+ import java .io .UnsupportedEncodingException ;
22+ import java .net .URLDecoder ;
23+ import java .net .URLEncoder ;
2024import java .util .Set ;
2125
2226
23- /**use this class instead of com.alibaba.fastjson.JSONObject
27+ /**use this class instead of com.alibaba.fastjson.JSONObject, not encode in default cases
2428 * @author Lemon
2529 */
2630public class JSONObject extends com .alibaba .fastjson .JSONObject {
@@ -32,33 +36,72 @@ public JSONObject() {
3236 super (true );
3337 }
3438 /**transfer Object to JSONObject
39+ * encode = false;
3540 * @param object
41+ * @see {@link #JSONObject(Object, boolean)}
3642 */
3743 public JSONObject (Object object ) {
38- this (toJSONString (object ));
44+ this (object , false );
45+ }
46+ /**transfer Object to JSONObject
47+ * @param object
48+ * @param encode
49+ * @see {@link #JSONObject(String, boolean)}
50+ */
51+ public JSONObject (Object object , boolean encode ) {
52+ this (toJSONString (object ), encode );
3953 }
4054 /**parse JSONObject with JSON String
55+ * encode = false;
4156 * @param json
57+ * @see {@link #JSONObject(String, boolean)}
4258 */
4359 public JSONObject (String json ) {
44- this (parseObject (json ));
60+ this (json , false );
61+ }
62+ /**parse JSONObject with JSON String
63+ * @param json
64+ * @param encode
65+ * @see {@link #JSONObject(com.alibaba.fastjson.JSONObject, boolean)}
66+ */
67+ public JSONObject (String json , boolean encode ) {
68+ this (parseObject (json ), encode );
4569 }
4670 /**transfer com.alibaba.fastjson.JSONObject to JSONObject
71+ * encode = false;
4772 * @param object
73+ * @see {@link #JSONObject(com.alibaba.fastjson.JSONObject, boolean)}
4874 */
4975 public JSONObject (com .alibaba .fastjson .JSONObject object ) {
76+ this (object , false );
77+ }
78+ /**transfer com.alibaba.fastjson.JSONObject to JSONObject
79+ * @param object
80+ * @param encode
81+ * @see {@link #add(com.alibaba.fastjson.JSONObject, boolean)}
82+ */
83+ public JSONObject (com .alibaba .fastjson .JSONObject object , boolean encode ) {
5084 this ();
51- add (object );
85+ add (object , encode );
5286 }
5387
5488
5589
5690
5791 /**put key-value in object into this
92+ * encode = false;
5893 * @param object
59- * @return this
94+ * @return {@link #add(com.alibaba.fastjson.JSONObject, boolean)}
6095 */
6196 public JSONObject add (com .alibaba .fastjson .JSONObject object ) {
97+ return add (object , false );
98+ }
99+ /**put key-value in object into this
100+ * @param object
101+ * @param encode
102+ * @return this
103+ */
104+ public JSONObject add (com .alibaba .fastjson .JSONObject object , boolean encode ) {
62105 Set <String > set = object == null ? null : object .keySet ();
63106 if (set != null ) {
64107 for (String key : set ) {
@@ -68,12 +111,83 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object) {
68111 return this ;
69112 }
70113
71-
114+
115+
116+ /**
117+ * @param key if decode && key instanceof String, key = URLDecoder.decode((String) key, UTF_8)
118+ * @param decode if decode && value instanceof String, value = URLDecoder.decode((String) value, UTF_8)
119+ * @return
120+ */
121+ public Object get (Object key , boolean decode ) {
122+ if (decode ) {
123+ if (key instanceof String ) {
124+ try {
125+ key = URLDecoder .decode ((String ) key , UTF_8 );
126+ } catch (UnsupportedEncodingException e ) {
127+ e .printStackTrace ();
128+ return null ;
129+ }
130+ }
131+ Object value = super .get (key );
132+ if (value instanceof String ) {
133+ try {
134+ return URLDecoder .decode ((String ) value , UTF_8 );
135+ } catch (UnsupportedEncodingException e ) {
136+ e .printStackTrace ();
137+ }
138+ }
139+ return null ;
140+ }
141+ return super .get (key );
142+ }
143+
144+ /**
145+ * encode = false
146+ * @param value
147+ * @return {@link #put(String, boolean)}
148+ */
149+ public Object put (Object value ) {
150+ return put (value , false );
151+ }
152+ /**
153+ * @param value
154+ * @param encode
155+ * @return {@link #put(String, Object, boolean)}
156+ */
157+ public Object put (Object value , boolean encode ) {
158+ return put (null , value , encode );
159+ }
160+ /**
161+ * @param key StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName()
162+ * <br> >> if decode && key instanceof String, key = URLDecoder.decode((String) key, UTF_8)
163+ * @param value URLEncoder.encode((String) value, UTF_8);
164+ * @param encode if value instanceof String, value = URLEncoder.encode((String) value, UTF_8);
165+ * @return
166+ */
167+ public Object put (String key , Object value , boolean encode ) {
168+ key = StringUtil .isNotEmpty (key , true ) ? key : value .getClass ().getSimpleName ();
169+ if (encode ) {
170+ try {
171+ key = URLEncoder .encode (key , UTF_8 );
172+ } catch (UnsupportedEncodingException e ) {
173+ e .printStackTrace ();
174+ }
175+ if (value instanceof String ) {
176+ try {
177+ value = URLEncoder .encode ((String ) value , UTF_8 );
178+ } catch (UnsupportedEncodingException e ) {
179+ e .printStackTrace ();
180+ }
181+ }
182+ }
183+ return super .put (key , value );
184+ }
185+
72186
73187
74188 //judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
75189 public static final String KEY_ARRAY = "[]" ;
76-
190+
77191 /**判断是否为Array的key
78192 * @param key
79193 * @return
@@ -96,9 +210,9 @@ public static boolean isWord(String key) {
96210 return StringUtil .isNotEmpty (key , false ) && namePattern .matcher (key ).matches ();
97211 }
98212 //judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
99-
100-
101-
213+
214+
215+
102216 public static final String KEY_COLUMNS = "@columns" ;//@key关键字都放这个类
103217
104218 /**set columns need to be returned
@@ -112,6 +226,6 @@ public JSONObject setColumns(String columns) {
112226 public String getColumns () {
113227 return getString (KEY_COLUMNS );
114228 }
115-
116-
229+
230+
117231}
0 commit comments