java-json-tools / json-patch

An RFC 6902 (JSON Patch) and reverse, plus RFC 7386 (JSON Merge Patch), implementation in Java using Jackson (2.2.x)
Other
643 stars 186 forks source link

Can not deserialize instance of java.util.ArrayList out of FIELD_NAME token #29

Closed slvs closed 9 years ago

slvs commented 9 years ago

I'm using json-patch-1.9, and the following simple code doesn't work:

        String origJson = "{\"id\":1}";
        String newJson =  "{\"id\":1}";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode origNode = mapper.readTree(origJson);
        JsonNode newNode = mapper.readTree(newJson);
        JsonPatch patchNew = JsonPatch.fromJson(newNode);
        JsonNode diffs = patchNew.apply(origNode);

The call to JsonPatch.fromJson(newNode) throws the following exception:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of FIELD_NAME token

Any idea what is wrong?

ghost commented 9 years ago

I assume the JsonPatch expects json that looks like [{"op": "add", "path": "/id", "value": 1}]. Described at: https://tools.ietf.org/html/rfc6902

Maybe you are thinking of JsonMergePatch? https://tools.ietf.org/html/rfc7386

Which uses the JsonMergePatch.fromJson function.

fge commented 9 years ago

Exactly what @jhuffaker said; here you are trying to use a JSON Merge Patch, not a JSON Patch.