Skip to content

Misinterpretation of numeric JSON Pointers as array indexes? #517

Description

@kristian-lange

Description

Diffson appears to interpret numeric JSON Pointer tokens as array indexes when parsing the pointer, rather than interpreting them according to the type of the JSON value encountered while evaluating the pointer.

This causes problems when JSON objects have numeric property names.

For example, given:

{
  "shared": {
    "a": 123
  },
  "100": {
    "a": 456
  }
}

this JSON Patch works:

[
  {
    "op": "copy",
    "from": "/shared/a",
    "path": "/100/b"
  }
]

but the reverse direction fails:

[
  {
    "op": "copy",
    "from": "/100/a",
    "path": "/shared/a"
  }
]

It looks like the 100 is interpreted as an array index instead of an object key.

Expected behavior

As I understand RFC 6901, a reference token itself does not distinguish between an object property and an array index. Its interpretation depends on the JSON value being traversed.

For example:

{
  "0": "object value",
  "array": [
    "array value"
  ]
}

both of these pointers should be valid:

/0
/array/0

/0 should address the object member named "0", while /array/0 should address array index 0.

RFC 6901

Section 4 of RFC 6901 seems to specify that evaluation depends on the type of the current referenced value:

If the currently referenced value is a JSON object, the new referenced value is the object member with the name identified by the reference token.

Only if the currently referenced value is an array is the token interpreted as an array index.

Therefore, I would expect the distinction between an object key and an array index to be made during pointer evaluation rather than during pointer parsing.

Would you consider the current behavior a bug, or am I overlooking something in Diffson's interpretation of RFC 6901?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions