@@ -189,4 +189,46 @@ void mergeJsonObjectWithOverwriteSuccess() {
189189 jsonObject4 .merge (jsonObject2 , true );
190190 Assertions .assertEquals ("{\" k2\" :{\" k1\" :\" v1\" }}" , jsonObject4 .toJSONString ());
191191 }
192+
193+ @ Test
194+ void mergeJsonArrayWithObjectSuccess () {
195+ JSONObject jsonObject1 = new JSONObject ();
196+ jsonObject1 .appendField ("k1" , "v1" );
197+
198+ JSONArray jsonArray1 = new JSONArray ();
199+ jsonArray1 .add (jsonObject1 );
200+ Assertions .assertEquals ("[{\" k1\" :\" v1\" }]" , jsonArray1 .toJSONString ());
201+
202+ JSONObject jsonObject2 = new JSONObject ();
203+ jsonObject2 .appendField ("k2" , "v2" );
204+
205+ /*
206+ test merge json object ( before fix issue #51, these will fail.
207+ throw java.lang.ClassCastException: class net.minidev.json.JSONObject cannot be cast to class net.minidev.json.JSONArray)
208+ */
209+ jsonArray1 .merge (jsonObject2 );
210+ Assertions .assertEquals ("[{\" k1\" :\" v1\" },{\" k2\" :\" v2\" }]" , jsonArray1 .toJSONString ());
211+
212+ jsonArray1 .merge ("s1" );
213+ Assertions .assertEquals ("[{\" k1\" :\" v1\" },{\" k2\" :\" v2\" },\" s1\" ]" , jsonArray1 .toJSONString ());
214+
215+ jsonArray1 .merge (1 );
216+ Assertions .assertEquals (
217+ "[{\" k1\" :\" v1\" },{\" k2\" :\" v2\" },\" s1\" ,1]" , jsonArray1 .toJSONString ());
218+
219+ jsonArray1 .merge (true );
220+ Assertions .assertEquals (
221+ "[{\" k1\" :\" v1\" },{\" k2\" :\" v2\" },\" s1\" ,1,true]" , jsonArray1 .toJSONString ());
222+
223+ // test merge json array
224+ JSONObject jsonObject3 = new JSONObject ();
225+ jsonObject3 .appendField ("k3" , "v3" );
226+ JSONArray jsonArray2 = new JSONArray ();
227+ jsonArray2 .add (jsonObject3 );
228+
229+ jsonArray1 .merge (jsonArray2 );
230+ Assertions .assertEquals (
231+ "[{\" k1\" :\" v1\" },{\" k2\" :\" v2\" },\" s1\" ,1,true,{\" k3\" :\" v3\" }]" ,
232+ jsonArray1 .toJSONString ());
233+ }
192234}
0 commit comments