Skip to content

Commit 72ec2ba

Browse files
committed
Add docs for query serialization options
1 parent 9024980 commit 72ec2ba

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/docs/quick-start.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ console.log(nodes.values()); // [ 'John', 'Sally', 'Jane' ]
147147

148148
`compile()` is also re-exported to JSON P3's top-level namespace.
149149

150+
### Query serialization
151+
152+
A [compiled `JSONPathQuery`](#compilation) can be serialized back to a string using its [`toString()`](./api/namespaces/jsonpath/classes/JSONPathQuery.md#tostring) method. As of version 2.1.0, the default string representation uses shorthand notation where possible and double quotes for names and string literals rather than single quotes.
153+
154+
```javascript
155+
import { jsonpath } from "json-p3";
156+
157+
const path = jsonpath.compile("$['users'][?@.score < 100]['name']");
158+
console.log(path.toString()); // $.users[?@.score < 100].name
159+
```
160+
161+
To serialize a `JSONPathQuery` using the canonical bracket notation and single quotes, pass a [`SerializationOptions`](./api/namespaces/jsonpath/type-aliases/SerializationOptions.md) object as an argument to `toString()`.
162+
163+
```javascript
164+
import { jsonpath } from "json-p3";
165+
166+
const path = jsonpath.compile("$.users[?@.score < 100].name");
167+
console.log(path.toString({ form: "canonical" })); $['users'][?@['score'] < 100]['name']
168+
```
169+
150170
## JSON Pointer
151171
152172
Resolve a JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) against some data using `jsonpointer.resolve()`.

0 commit comments

Comments
 (0)