1
1
package com .fasterxml .jackson .datatype .jsr353 ;
2
2
3
3
import com .fasterxml .jackson .core .JsonParser ;
4
+ import com .fasterxml .jackson .core .JsonToken ;
4
5
import com .fasterxml .jackson .databind .DeserializationContext ;
5
6
import com .fasterxml .jackson .databind .deser .std .StdDeserializer ;
7
+ import com .fasterxml .jackson .databind .exc .InvalidFormatException ;
6
8
import com .fasterxml .jackson .databind .type .LogicalType ;
7
9
8
10
import javax .json .Json ;
11
+ import javax .json .JsonArray ;
9
12
import javax .json .JsonPatch ;
13
+ import javax .json .JsonValue ;
14
+ import javax .json .JsonValue .ValueType ;
10
15
import java .io .IOException ;
11
16
12
17
public class JsonPatchDeserializer extends StdDeserializer <JsonPatch > {
@@ -25,7 +30,19 @@ public LogicalType logicalType() {
25
30
26
31
@ Override
27
32
public JsonPatch deserialize (JsonParser p , DeserializationContext ctxt ) throws IOException {
28
- return Json .createPatch (jsonValueDeser ._deserializeArray (p , ctxt ));
33
+ if (p .currentToken () != JsonToken .START_ARRAY ) {
34
+ throw getException (p , p .getText ());
35
+ }
36
+ final JsonArray patch = jsonValueDeser ._deserializeArray (p , ctxt );
37
+ for (final JsonValue element : patch .getValuesAs (JsonValue .class )) {
38
+ if (element .getValueType () != ValueType .OBJECT ) {
39
+ throw getException (p , element .toString ());
40
+ }
41
+ }
42
+ return Json .createPatch (patch );
29
43
}
30
44
45
+ private InvalidFormatException getException (final JsonParser p , final String value ) {
46
+ return InvalidFormatException .from (p , "JSON patch has to be an array of objects" , value , handledType ());
47
+ }
31
48
}
0 commit comments