@@ -157,53 +157,51 @@ the correct escaping of keys and values, preventing syntax errors::
157
157
The :class: `Symfony\\ Component\\ JsonPath\\ JsonPath ` class provides several methods to build your query:
158
158
159
159
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::key `
160
- Adds a key selector. The key name will be properly escaped::
160
+ Adds a key selector. The key name will be properly escaped::
161
161
162
- // Creates the path '$["key\"with\"quotes"]'
163
- $path = (new JsonPath())->key('key"with"quotes');
162
+ // Creates the path '$["key\"with\"quotes"]'
163
+ $path = (new JsonPath())->key('key"with"quotes');
164
164
165
165
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::deepScan `
166
- Adds the descendant operator ``.. `` to perform a recursive search from the
167
- current point in the path::
166
+ Adds the descendant operator ``.. `` to perform a recursive search from the current point in the path::
168
167
169
- // Get all prices in the store: '$["store"]..["price"]'
170
- $path = (new JsonPath())->key('store')->deepScan()->key('price');
168
+ // Get all prices in the store: '$["store"]..["price"]'
169
+ $path = (new JsonPath())->key('store')->deepScan()->key('price');
171
170
172
171
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::all `
173
- Adds the wildcard operator ``[*] `` to select all items in an array or object::
172
+ Adds the wildcard operator ``[*] `` to select all items in an array or object::
174
173
175
- // Creates the path '$["store"]["book"][*]'
176
- $path = (new JsonPath())->key('store')->key('book')->all();
174
+ // Creates the path '$["store"]["book"][*]'
175
+ $path = (new JsonPath())->key('store')->key('book')->all();
177
176
178
177
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::index `
179
- Adds an array index selector.
178
+ Adds an array index selector.
180
179
181
180
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::first ` / :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::last `
182
- Shortcuts for ``index(0) `` and ``index(-1) `` respectively::
181
+ Shortcuts for ``index(0) `` and ``index(-1) `` respectively::
183
182
184
- // Get the last book: '$["store"]["book"][-1]'
185
- $path = (new JsonPath())->key('store')->key('book')->last();
183
+ // Get the last book: '$["store"]["book"][-1]'
184
+ $path = (new JsonPath())->key('store')->key('book')->last();
186
185
187
186
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::slice `
188
- Adds an array slice selector ``[start:end:step] ``::
187
+ Adds an array slice selector ``[start:end:step] ``::
189
188
190
- // Get books from index 1 up to (but not including) index 3
191
- // Creates the path '$["store"]["book"][1:3]'
192
- $path = (new JsonPath())->key('store')->key('book')->slice(1, 3);
189
+ // Get books from index 1 up to (but not including) index 3
190
+ // Creates the path '$["store"]["book"][1:3]'
191
+ $path = (new JsonPath())->key('store')->key('book')->slice(1, 3);
193
192
194
- // Get every second book from the first four books
195
- // Creates the path '$["store"]["book"][0:4:2]'
196
- $path = (new JsonPath())->key('store')->key('book')->slice(0, 4, 2);
193
+ // Get every second book from the first four books
194
+ // Creates the path '$["store"]["book"][0:4:2]'
195
+ $path = (new JsonPath())->key('store')->key('book')->slice(0, 4, 2);
197
196
198
197
* :method: `Symfony\\ Component\\ JsonPath\\ JsonPath::filter `
199
- Adds a filter expression. The expression string is the part that goes inside
200
- the ``?() `` syntax::
198
+ Adds a filter expression. The expression string is the part that goes inside the ``?() `` syntax::
201
199
202
- // Get expensive books: '$["store"]["book"][?(@.price > 20)]'
203
- $path = (new JsonPath())
204
- ->key('store')
205
- ->key('book')
206
- ->filter('@.price > 20');
200
+ // Get expensive books: '$["store"]["book"][?(@.price > 20)]'
201
+ $path = (new JsonPath())
202
+ ->key('store')
203
+ ->key('book')
204
+ ->filter('@.price > 20');
207
205
208
206
Advanced Querying
209
207
-----------------
@@ -216,7 +214,7 @@ appropriate (e.g., inside a ``filter()`` expression).
216
214
Testing with JSON Assertions
217
215
----------------------------
218
216
219
- The component provides a set of PHPUnit assertions to make testing JSON data more convenient. To use them, include the
217
+ The component provides a set of PHPUnit assertions to make testing JSON data more convenient. Use the
220
218
:class: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait ` in your test class::
221
219
222
220
use PHPUnit\Framework\TestCase;
@@ -237,52 +235,52 @@ The component provides a set of PHPUnit assertions to make testing JSON data mor
237
235
The trait provides the following assertion methods:
238
236
239
237
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathCount `
240
- Asserts that the number of elements found by the JSONPath expression matches an expected count::
238
+ Asserts that the number of elements found by the JSONPath expression matches an expected count::
241
239
242
- $json = '{"a": [1, 2, 3]}';
243
- self::assertJsonPathCount(3, '$.a[*]', $json);
240
+ $json = '{"a": [1, 2, 3]}';
241
+ self::assertJsonPathCount(3, '$.a[*]', $json);
244
242
245
243
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathEquals `
246
- Asserts that the result of a JSONPath expression is equal (`` == ``) to an expected value. This assertion uses type coercion::
244
+ Asserts that the result of a JSONPath expression is equal to an expected value.The comparison uses `` == `` ( type coercion) instead of `` === `` ::
247
245
248
- $json = '{"a": [1, 2, 3]}';
246
+ $json = '{"a": [1, 2, 3]}';
249
247
250
- // passes because "1" == 1
251
- self::assertJsonPathEquals(['1'], '$.a[0]', $json);
248
+ // passes because "1" == 1
249
+ self::assertJsonPathEquals(['1'], '$.a[0]', $json);
252
250
253
251
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotEquals `
254
- Asserts that the result of a JSONPath expression is not equal ( ``!= ``) to an expected value ::
252
+ Asserts that the result of a JSONPath expression is not equal to an expected value.The comparison uses ``!= `` (type coercion) instead of `` !== `` ::
255
253
256
- $json = '{"a": [1, 2, 3]}';
257
- self::assertJsonPathNotEquals([42], '$.a[0]', $json);
254
+ $json = '{"a": [1, 2, 3]}';
255
+ self::assertJsonPathNotEquals([42], '$.a[0]', $json);
258
256
259
257
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathSame `
260
- Asserts that the result of a JSONPath expression is identical (``=== ``) to an expected value. This is a strict comparison and does not perform type coercion::
258
+ Asserts that the result of a JSONPath expression is identical (``=== ``) to an expected value. This is a strict comparison and does not perform type coercion::
261
259
262
- $json = '{"a": [1, 2, 3]}';
260
+ $json = '{"a": [1, 2, 3]}';
263
261
264
- // fails because "1" !== 1
265
- // self::assertJsonPathSame(['1'], '$.a[0]', $json);
262
+ // fails because "1" !== 1
263
+ // self::assertJsonPathSame(['1'], '$.a[0]', $json);
266
264
267
- self::assertJsonPathSame([1], '$.a[0]', $json);
265
+ self::assertJsonPathSame([1], '$.a[0]', $json);
268
266
269
267
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotSame `
270
- Asserts that the result of a JSONPath expression is not identical (``!== ``) to an expected value::
268
+ Asserts that the result of a JSONPath expression is not identical (``!== ``) to an expected value::
271
269
272
- $json = '{"a": [1, 2, 3]}';
273
- self::assertJsonPathNotSame(['1'], '$.a[0]', $json);
270
+ $json = '{"a": [1, 2, 3]}';
271
+ self::assertJsonPathNotSame(['1'], '$.a[0]', $json);
274
272
275
273
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathContains `
276
- Asserts that a given value is found within the array of results from the JSONPath expression::
274
+ Asserts that a given value is found within the array of results from the JSONPath expression::
277
275
278
- $json = '{"tags": ["php", "symfony", "json"]}';
279
- self::assertJsonPathContains('symfony', '$.tags[*]', $json);
276
+ $json = '{"tags": ["php", "symfony", "json"]}';
277
+ self::assertJsonPathContains('symfony', '$.tags[*]', $json);
280
278
281
279
* :method: `Symfony\\ Component\\ JsonPath\\ Test\\ JsonPathAssertionsTrait::assertJsonPathNotContains `
282
- Asserts that a given value is NOT found within the array of results from the JSONPath expression::
280
+ Asserts that a given value is NOT found within the array of results from the JSONPath expression::
283
281
284
- $json = '{"tags": ["php", "symfony", "json"]}';
285
- self::assertJsonPathNotContains('java', '$.tags[*]', $json);
282
+ $json = '{"tags": ["php", "symfony", "json"]}';
283
+ self::assertJsonPathNotContains('java', '$.tags[*]', $json);
286
284
287
285
Error Handling
288
286
--------------
0 commit comments