Subject: draft-ietf-appsawg-json-pointer-07 - array index for end ofarray

From: "David J. Biesack" <[email-masked]>
Date: Fri, 14 Dec 2012 12:54:17 -0500

I originally posted this in a "Re: Last Call: <draft-ietf-appsawg-json-patch-08.txt> (JSON Patch) to Proposed Standard" thread
but now I've read draft-ietf-appsawg-json-pointer-07 and have changed that suggestion to reflect draft-ietf-appsawg-json-pointer-07

Suggestion: allow negative numbers to indicate a position counted from the end of the array instead "array/-".

For example, a/-1 to point to the last item in an array (length > 0), -2 to point to the penultimate item, and so on. 
The notation -i is an alias for (array.length - i)

It is an error if |i| > array length.

For the json-patch "add" operation, use 

   { "op" : "add", "path" : "...array/-1", "value" : value } 

to append to the end, "...array/-2" to insert before the last item, and so on.
The "path" is the path that the item will be addressed by after inserting it.
I.e. after appending to an array of length l at j < 0, the item will be at the zero-based index (l + 1) + j. 

E.g. given the document with an array of length 4:

{ "a" : [ 0, 1, 2, 3, ] }

then 

{ "op" : "add", "path" : "/a/-1", "value" : 99 }

yields

{ "a" : [ 0, 1, 2, 3, 99] }

i.e. 99 is found at (4 + 1) -1 == 4

{ "op" : "add", "path" : "/a/-2", "value" : 99}

yields

{ "a" : [ 0, 1, 2, 99, 3] }

i.e. 99 is found at (4 + 1) -2 == 3

and finally

{ "op" : "add", "path" : "/a/-5", "value" : 99 }

yields

{ "a" : [ 99, 0, 1, 2, 3] }

i.e. 99 is found at (4 + 1) -5 == 0

-- 
David J. Biesack | Principal API Architect, SAS | @davidbiesack | 919-531-7771 | www.sas.com

---

From: Mark Nottingham <[email-masked]>
Date: Mon, 17 Dec 2012 11:33:41 +1100

David,

This was discussed in the Working Group, but it wasn't felt that the added complexity was worth it; there's a strong feeling that this spec should be as simple as possible.

Cheers,

--
Mark Nottingham   http://www.mnot.net/

---

From: "David J. Biesack" <[email-masked]>
Date: Mon, 17 Dec 2012 09:02:10 -0500

Mark Nottingham <[email-masked]> writes:

>> [some measure of complexity omitted here...]

Understood - simplicity is a great Guiding Principle.

Might I suggest, however, using -1 instead of "-" to refer to the last item in an array, as this provides two benefits:

1) Allows for adding the complexity down the road in a compatible way, should there be need
2) Uniformity; i.e. always using integer values for referring to array elements.

Thanks for the consideration,
djb

---

From: Barry Leiba <[email-masked]>
Date: Mon, 17 Dec 2012 14:54:04 -0500

>> This was discussed in the Working Group, but it wasn't felt that the added
>> complexity was worth it; there's a strong feeling that this spec should be as
>> simple as possible.
>

I have to say that this suggestion sounds very compelling to me, for
both reasons.  I know there's a bunch of running code out there, but
this (and perhaps teasing apart the "add" and "insert" concepts into
separate verbs) seems worth the bother.

Barry, as participant

---

From: Martin Thomson <[email-masked]>
Date: Mon, 17 Dec 2012 13:31:20 -0800

> separate verbs) seems worth the bother.

This is true, it does sound compelling.

There will, no doubt, be questions about why -2 doesn't work.  It's
tempting to implement -2 and -3, so you might have to explain why that
is a bad idea.

index = (index + length) % length

---

From: Mark Nottingham <[email-masked]>
Date: Tue, 18 Dec 2012 11:36:28 +1100

I disagree. Adding a capability for other indices down the road is NOT compatible for existing uses, so adding it will cause confusion ("are you using the old JSON Pointer or the new one?") and interop problems.

IIRC the discussion in the WG went much along these lines, and led to us explicitly choosing a single character, rather than a misleading "-1" construct. I'd be more comfortable with changing the character to something even further away, rather than making this construct even more confusing. 

The other way we could go would be to do full negative indexing; we don't have any use cases for that, and it increases complexity a bit, but at least it would be unsurprising.

---

From: Bill McQuillan <[email-masked]>
Date: Mon, 17 Dec 2012 16:55:26 -0800

ISTM that if the person constructing the pointer wants to append 
to an array and knows the size of the array (as pointed out in 
another thread) he should be able to use "/foo/<len>" where <len> 
is the index of the element just after the last existing one in 
the array. No need for "/foo/-" or any negative integers. The only 
requirement is to allow that one extra index to be used without 
error.

On Mon, 2012-12-17, Mark Nottingham wrote:
> I disagree. Adding a capability for other indices down the
> road is NOT compatible for existing uses, so adding it will
> cause confusion ("are you using the old JSON Pointer or the new one?") and interop problems.

> IIRC the discussion in the WG went much along these lines, and
> led to us explicitly choosing a single character, rather than a
> misleading "-1" construct. I'd be more comfortable with
> changing the character to something even further away, rather
> than making this construct even more confusing. 

> The other way we could go would be to do full negative
> indexing; we don't have any use cases for that, and it
> increases complexity a bit, but at least it would be unsurprising.

-- 
Bill McQuillan <[email-masked]>