json-patch / json-patch2

A possibile revision to the JSON-Patch format
44 stars 0 forks source link

Escaping Syntax #1

Open mnot opened 10 years ago

mnot commented 10 years ago

(from an RFC Errata)

The escape syntax seems weird and confusing. Rather than ~0 and ~1, why not use a repeated (double) slash to escape a slash? This is similar to how SQL escapes single quotes in string literals by using the single quote twice.

We have JSON functions in Presto (prestodb.io) that could benefit from an improved syntax (they currently use JSONPath), but I can't see understanding ~0 and ~1.

espadrine commented 7 years ago

The double slash feels similarly weird.

I wish for JSON Patch 2 to allow having arrays as paths, circumventing the problem of escaping and making it much faster for machines to parse the path.

For instance, { "op": "add", "path": ["foo", "/", "~1"], "insert data here" } applied to {"foo": {"/": {}}} would result in {"foo": {"/": {"~1": "insert data here"}}}.

It is backwards-compatible, as libraries can detect whether the path is a string or an array.

gregsdennis commented 5 years ago

The escape syntax is well-established from JSON Pointer. Why are we looking to deviate?

they currently use JSONPath

JSON Path is a query syntax that has the potential to return multiple results. Many use cases for JSON Patch require a single result, for which JSON Pointer is much more well-suited.

The only time I could see wanting JSON Path is for when we explicitly want potentially multiple matches (like a SQL where clause).

(See also #23)


I agree with support for arrays as an option, however.

streamich commented 3 years ago

+1 for using as array ["foo", "bar", 1] to specify the path. FWIW, my library—json-joy—supports Json Pointer as well as array syntax.

Array syntax is very useful for operations which are semantically very different for objects and arrays. For example "insert" operation is very different for objects and arrays, in object simply a new key is inserted, but in arrays all elements past insertion point are shifted.

When using an array as the path one can infer that if path step is a number it is indexing an array, if it is a string, it is referencing a key in object. That information is useful when doing operational transformation, when you need to transform one operation agains the other. Now if we use Json Pointer you basically need to see if the key step looks like a number, then it is likely that it is indexing an array and then one, kinda, can do the operational transform assuming the pointer is referencing an array, but it could also be completely wrong, if in reality the pointer is referencing an object with a key that looks like a number.


FWIW, Slate.js is using and array as a pointer. json1-ot is also using arrays.