@@ -181,6 +181,57 @@ public void typeChecksShouldSucceed() {
181181 assertEquals (Collections .emptyList (), client .jsonType ("foobar" , Path2 .of (".fooErr" )));
182182 }
183183
184+ @ Test
185+ public void testJsonMerge () {
186+ // Test with root path
187+ JSONObject json = new JSONObject ("{\" person\" :{\" name\" :\" John Doe\" ,\" age\" :25,\" address\" :{\" home\" :\" 123 Main Street\" },\" phone\" :\" 123-456-7890\" }}" );
188+ assertEquals ("OK" , client .jsonSet ("test_merge" , json ));
189+
190+ json = new JSONObject ("{\" person\" :{\" name\" :\" John Doe\" ,\" age\" :30,\" address\" :{\" home\" :\" 123 Main Street\" },\" phone\" :\" 123-456-7890\" }}" );
191+ assertEquals ("OK" , client .jsonMerge ("test_merge" , Path2 .of ("$" ), "{\" person\" :{\" age\" :30}}" ));
192+
193+ assertJsonArrayEquals (jsonArray (json ), client .jsonGet ("test_merge" , Path2 .of ("$" )));
194+
195+ // Test with root path path $.a.b
196+ assertEquals ("OK" , client .jsonMerge ("test_merge" , Path2 .of ("$.person.address" ), "{\" work\" :\" Redis office\" }" ));
197+ json = new JSONObject ("{\" person\" :{\" name\" :\" John Doe\" ,\" age\" :30,\" address\" :{\" home\" :\" 123 Main Street\" ,\" work\" :\" Redis office\" },\" phone\" :\" 123-456-7890\" }}" );
198+ assertJsonArrayEquals (jsonArray (json ), client .jsonGet ("test_merge" , Path2 .of ("$" )));
199+
200+ // Test with null value to delete a value
201+ assertEquals ("OK" , client .jsonMerge ("test_merge" , Path2 .of ("$.person" ), "{\" age\" :null}" ));
202+ json = new JSONObject ("{\" person\" :{\" name\" :\" John Doe\" ,\" address\" :{\" home\" :\" 123 Main Street\" ,\" work\" :\" Redis office\" },\" phone\" :\" 123-456-7890\" }}" );
203+ assertJsonArrayEquals (jsonArray (json ), client .jsonGet ("test_merge" , Path2 .of ("$" )));
204+
205+ // cleanup
206+ assertEquals (1L , client .del ("test_merge" ));
207+ }
208+
209+ @ Test
210+ public void testJsonMergeArray ()
211+ {
212+ // Test merge on an array
213+ JSONObject json = new JSONObject ("{\" a\" :{\" b\" :{\" c\" :[\" d\" ,\" e\" ]}}}" );
214+ assertEquals ("OK" , (client .jsonSet ("test_merge_array" , Path2 .of ("$" ), json )));
215+ assertEquals ("OK" , (client .jsonMerge ("test_merge_array" , Path2 .of ("$.a.b.c" ), "[\" f\" ]" )));
216+
217+ json = new JSONObject ("{\" a\" :{\" b\" :{\" c\" :[\" f\" ]}}}" );
218+ assertJsonArrayEquals (jsonArray (json ), client .jsonGet ("test_merge_array" , Path2 .of ("$" )));
219+
220+ // assertEquals("{{a={b={c=[f]}}}", client.jsonGet("test_merge_array", Path2.of("$")));
221+
222+ // Test merge an array on a value
223+ assertEquals ("OK" , (client .jsonSet ("test_merge_array" , Path2 .of ("$" ), "{\" a\" :{\" b\" :{\" c\" :\" d\" }}}" )));
224+ assertEquals ("OK" , (client .jsonMerge ("test_merge_array" , Path2 .of ("$.a.b.c" ), "[\" f\" ]" )));
225+ json = new JSONObject ("{\" a\" :{\" b\" :{\" c\" :[\" f\" ]}}}" );
226+ assertJsonArrayEquals (jsonArray (json ), client .jsonGet ("test_merge_array" , Path2 .of ("$" )));
227+
228+ // Test with null value to delete an array value
229+ assertEquals ("OK" , (client .jsonSet ("test_merge_array" , Path2 .of ("$" ), "{\" a\" :{\" b\" :{\" c\" :[\" d\" ,\" e\" ]}}}" )));
230+ assertEquals ("OK" , (client .jsonMerge ("test_merge_array" , Path2 .of ("$.a.b" ), "{\" c\" :null}" )));
231+ json = new JSONObject ("{\" a\" :{\" b\" :{}}}" );
232+ assertJsonArrayEquals (jsonArray (json ), client .jsonGet ("test_merge_array" , Path2 .of ("$" )));
233+ }
234+
184235 @ Test
185236 public void mgetWithPathWithAllKeysExist () {
186237 Baz baz1 = new Baz ("quuz1" , "grault1" , "waldo1" );
0 commit comments