Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Rough draft #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# JSONPath axiomatic semantics

** WORK IN PROGRESS **

## sequences of JSON values

A JSONPath is modelled as mapping one sequence of JSON values to another sequence of JSON values.
The `<>` notation is used for sequences, to try to distinguish these from JSON arrays.

Empty sequence: `<>`.

Concatenation: `<a, b> = <a> ^ <b>`.

So, for example, the sequence `<a, b, c>` is equivalent to the concatenation of three single item
sequences `<a> ^ <b> ^ <c>`.

## sequence semantics
A selector `s` takes a sequence of values and applies to each one separately.
The results are then concatenated.

```
<> s = <>
(x^y)s = (x s)^(y s)
Comment on lines +22 to +23
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much better, but we are no nearer understanding selectors from this. Can we assert that a selected sequence is a subsequence (not necessarily contiguous) of the original?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but each value in the selected sequence is a descendant (or equal to) of a value in the original sequence.

```

## JSON object construction

If `m` and `n` are JSON objects with no keys in common, then `m ∪ n` is the JSON object containing
all the mappings of `m` and `n` (but no others).
Comment on lines +28 to +29
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything to say about common keys? The overuse of the term 'mapping' is potentially confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to define a partial operation as that's all we need in the remainder of the spec (I think). If we had to handle common keys, we'd need something like Z's function override. How about the following?

Suggested change
If `m` and `n` are JSON objects with no keys in common, then `m ∪ n` is the JSON object containing
all the mappings of `m` and `n` (but no others).
If `m` and `n` are JSON objects with no keys in common, then `m ∪ n` is the JSON object containing
all the pairs of keys and values from `m` and `n` (but no others).


## compound path semantics

If JSONPath `p` is selector `s` followed by JSONPath `t`, then for all JSON values `v`:
```
<v>p = (<v>s)t
```

## root selector semantics
`$` is effectively a no-op. If `t` is a JSONPath (without a leading `$`), then:
```
<v>$t = <v>t
```
Comment on lines +39 to +42
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of thing is $? Is it a selector, a JsonPath or something else? What does it mean for a JSONPath to have a "leading $", and why does it matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are at least two schools of thought. I have tended to think of $ as a special selector which takes the input value and produces a sequence consisting of just that value. Others think of $ as a kind of delimiter which says "start at the root of the input value". The latter is more consistent with the use of $ in filter notation (which we'll come to later).

The original JSONPath article required all JSONPaths to start with $, but some implementations make this optional, just to confuse matters.

Question: do we need to use something like quasi quotes to clarify the above?

## selectors

The following sections describe primitive selectors which can be strung together to form a JSONPath.
### dot child
If `o` is a JSON object which does not have key `k`, then:
```
<o>.k = <>
```
and:
```
<o ∪ {k:v}>.k = <v>
```

### union
```
<a>[u, v] = (<a>[u]) ^ (<a>[v])
```

### bracket child
If `o` is a JSON object which does not have key `k` (for certain forms of k...), then:
```
<o>[k] = <>
```
and:
```
<o ∪ {k:v}>[k] = <v>
```

### array slice
If `a` is an array, then:
```
... details! ...
```

If `a` is not an array and `sl` is a slice expression, then:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a here is a value, I take it. Or an object. Hmm, I'm getting really confused here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a is probably the worst name for this case. It's any value which isn't an array. It could be an object or a scalar.

```
<a>[sl] = <>
```

### recursive descent
If `z` is a scalar:
```
<z>.. = <z>
```
If `o` is an empty object:
```
<o>.. = <>
```

If `o` is a JSON object which does not have key `k`, then:
If `o` is an object comprised of `k:v` and an object p:
```
<o ∪ {k:v}>.. = (<v>)^(<v>..)^(<o>..)
```
Note: since an object can be deconstructed to the form `o ∪ {k:v}` for arbitrary key `k` in
the original object, the ordering of the result is only partially defined.

If `a` is an empty array:
```
<a>.. = <>
```
If `h` is a JSON value and `t` is a JSON array:
```
(<h>^t).. = (<h>)^(<h>..)^(t..)
```