@@ -105,7 +105,7 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object, boolean encode) {
105105 Set <String > set = object == null ? null : object .keySet ();
106106 if (set != null ) {
107107 for (String key : set ) {
108- put (key , object .get (key ));
108+ put (key , object .get (key ), encode );
109109 }
110110 }
111111 return this ;
@@ -121,22 +121,24 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object, boolean encode) {
121121 public Object get (Object key , boolean decode ) {
122122 if (decode ) {
123123 if (key instanceof String ) {
124- try {
125- key = URLDecoder .decode ((String ) key , UTF_8 );
126- } catch (UnsupportedEncodingException e ) {
127- e .printStackTrace ();
128- return null ;
124+ if (((String ) key ).endsWith ("+" ) || ((String ) key ).endsWith ("-" )) {
125+ try {//多层encode导致内部Comment[]传到服务端decode后最终变为Comment%5B%5D
126+ key = URLDecoder .decode ((String ) key , UTF_8 );
127+ } catch (UnsupportedEncodingException e ) {
128+ e .printStackTrace ();
129+ return null ;
130+ }
129131 }
130132 }
131133 Object value = super .get (key );
132134 if (value instanceof String ) {
133135 try {
134- return URLDecoder .decode ((String ) value , UTF_8 );
136+ value = URLDecoder .decode ((String ) value , UTF_8 );
135137 } catch (UnsupportedEncodingException e ) {
136138 e .printStackTrace ();
137139 }
138140 }
139- return null ;
141+ return value ;
140142 }
141143 return super .get (key );
142144 }
@@ -150,6 +152,7 @@ public Object put(Object value) {
150152 return put (value , false );
151153 }
152154 /**
155+ * key = value.getClass().getSimpleName()
153156 * @param value
154157 * @param encode
155158 * @return {@link #put(String, Object, boolean)}
@@ -158,21 +161,26 @@ public Object put(Object value, boolean encode) {
158161 return put (null , value , encode );
159162 }
160163 /**
161- * @param key StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName()
164+ * @param key if StringUtil.isNotEmpty(key, true) == false,
165+ * <br> key = value == null ? null : value.getClass().getSimpleName();
162166 * <br> >> if decode && key instanceof String, key = URLDecoder.decode((String) key, UTF_8)
163167 * @param value URLEncoder.encode((String) value, UTF_8);
164168 * @param encode if value instanceof String, value = URLEncoder.encode((String) value, UTF_8);
165169 * @return
166170 */
167171 public Object put (String key , Object value , boolean encode ) {
168- key = StringUtil .isNotEmpty (key , true ) ? key : value .getClass ().getSimpleName ();
172+ if (StringUtil .isNotEmpty (key , true ) == false ) {
173+ key = value == null ? null : value .getClass ().getSimpleName ();
174+ }
169175 if (encode ) {
170- try {
171- key = URLEncoder .encode (key , UTF_8 );
172- } catch (UnsupportedEncodingException e ) {
173- e .printStackTrace ();
176+ if (key .endsWith ("+" ) || key .endsWith ("-" )) {
177+ try {//多层encode导致内部Comment[]传到服务端decode后最终变为Comment%5B%5D
178+ key = URLEncoder .encode (key , UTF_8 );
179+ } catch (UnsupportedEncodingException e ) {
180+ e .printStackTrace ();
181+ }
174182 }
175- if (value instanceof String ) {
183+ if (value instanceof String ) {//只在value instanceof String时encode key?{@link #get(Object, boolean)}内做不到
176184 try {
177185 value = URLEncoder .encode ((String ) value , UTF_8 );
178186 } catch (UnsupportedEncodingException e ) {
0 commit comments