2
2
3
3
import com .fasterxml .jackson .databind .ObjectMapper ;
4
4
5
+ import jakarta .json .JsonArray ;
5
6
import jakarta .json .JsonMergePatch ;
6
7
import jakarta .json .JsonObject ;
7
8
import jakarta .json .JsonString ;
@@ -16,7 +17,7 @@ public class JsonMergePatchDeserializationTest extends TestBase {
16
17
17
18
private static final ObjectMapper MAPPER = newMapper ();
18
19
19
- public void testDeserializationAndPatching () throws Exception {
20
+ public void testObjectDeserializationAndPatching () throws Exception {
20
21
final String json = "{" +
21
22
"\" name\" :\" Json\" " +
22
23
"}" ;
@@ -39,6 +40,46 @@ public void testDeserializationAndPatching() throws Exception {
39
40
assertThat (patchedPerson , is (new Person ("Json" , "Smith" )));
40
41
}
41
42
43
+ public void testArrayDeserializationAndPatching () throws Exception {
44
+ final String json = "[" +
45
+ "\" name\" ,\" Json\" " +
46
+ "]" ;
47
+
48
+ final JsonMergePatch jsonMergePatch = MAPPER .readValue (json , JsonMergePatch .class );
49
+ final JsonValue jsonPatchAsJsonValue = jsonMergePatch .toJsonValue ();
50
+ assertThat (jsonPatchAsJsonValue , instanceOf (JsonArray .class ));
51
+
52
+ final JsonArray jsonPatchAsJsonArray = jsonPatchAsJsonValue .asJsonArray ();
53
+ assertThat (jsonPatchAsJsonArray .size (), is (2 ));
54
+ assertThat (jsonPatchAsJsonArray .get (0 ), instanceOf (JsonString .class ));
55
+ assertThat (((JsonString )jsonPatchAsJsonArray .get (0 )).getString (), is ("name" ));
56
+ assertThat (jsonPatchAsJsonArray .get (1 ), instanceOf (JsonString .class ));
57
+ assertThat (((JsonString )jsonPatchAsJsonArray .get (1 )).getString (), is ("Json" ));
58
+
59
+ assertThat (serializeAsString (jsonPatchAsJsonValue ), is (json ));
60
+
61
+ final Person person = new Person ("John" , "Smith" );
62
+ final JsonValue personJson = MAPPER .convertValue (person , JsonValue .class );
63
+ final JsonValue patchedPersonJson = jsonMergePatch .apply (personJson );
64
+ assertThat (patchedPersonJson , instanceOf (JsonArray .class ));
65
+ }
66
+
67
+ public void testScalarDeserializationAndPatching () throws Exception {
68
+ final String json = "\" name\" " ;
69
+
70
+ final JsonMergePatch jsonMergePatch = MAPPER .readValue (json , JsonMergePatch .class );
71
+ final JsonValue jsonPatchAsJsonValue = jsonMergePatch .toJsonValue ();
72
+ assertThat (jsonPatchAsJsonValue , instanceOf (JsonString .class ));
73
+
74
+ assertThat (serializeAsString (jsonPatchAsJsonValue ), is (json ));
75
+
76
+ final Person person = new Person ("John" , "Smith" );
77
+ final JsonValue personJson = MAPPER .convertValue (person , JsonValue .class );
78
+ final JsonValue patchedPersonJson = jsonMergePatch .apply (personJson );
79
+ assertThat (patchedPersonJson , instanceOf (JsonString .class ));
80
+ assertThat (((JsonString )patchedPersonJson ).getString (), is ("name" ));
81
+ }
82
+
42
83
static class Person {
43
84
private String name ;
44
85
private String lastName ;
0 commit comments