|
| 1 | +============ |
| 2 | +jmespath.php |
| 3 | +============ |
| 4 | + |
| 5 | +JMESPath (pronounced "jaymz path") allows you to declaratively specify how to |
| 6 | +extract elements from a JSON document. *jmespath.php* allows you to use |
| 7 | +JMESPath in PHP applications with PHP data structures. It requires PHP 5.4 or |
| 8 | +greater and can be installed through `Composer <http://getcomposer.org/doc/00-intro.md>`_ |
| 9 | +using the ``mtdowling/jmespath.php`` package. |
| 10 | + |
| 11 | +.. code-block:: php |
| 12 | +
|
| 13 | + require 'vendor/autoload.php'; |
| 14 | +
|
| 15 | + $expression = 'foo.*.baz'; |
| 16 | +
|
| 17 | + $data = [ |
| 18 | + 'foo' => [ |
| 19 | + 'bar' => ['baz' => 1], |
| 20 | + 'bam' => ['baz' => 2], |
| 21 | + 'boo' => ['baz' => 3] |
| 22 | + ] |
| 23 | + ]; |
| 24 | +
|
| 25 | + JmesPath\search($expression, $data); |
| 26 | + // Returns: [1, 2, 3] |
| 27 | +
|
| 28 | +- `JMESPath Tutorial <http://jmespath.org/tutorial.html>`_ |
| 29 | +- `JMESPath Grammar <http://jmespath.org/specification.html#grammar>`_ |
| 30 | +- `JMESPath Python library <https://github.com/jmespath/jmespath.py>`_ |
| 31 | + |
| 32 | +PHP Usage |
| 33 | +========= |
| 34 | + |
| 35 | +The ``JmesPath\search`` function can be used in most cases when using the |
| 36 | +library. This function utilizes a JMESPath runtime based on your environment. |
| 37 | +The runtime utilized can be configured using environment variables and may at |
| 38 | +some point in the future automatically utilize a C extension if available. |
| 39 | + |
| 40 | +.. code-block:: php |
| 41 | +
|
| 42 | + $result = JmesPath\search($expression, $data); |
| 43 | +
|
| 44 | + // or, if you require PSR-4 compliance. |
| 45 | + $result = JmesPath\Env::search($expression, $data); |
| 46 | +
|
| 47 | +Runtimes |
| 48 | +-------- |
| 49 | + |
| 50 | +jmespath.php utilizes *runtimes*. There are currently two runtimes: |
| 51 | +AstRuntime and CompilerRuntime. |
| 52 | + |
| 53 | +AstRuntime is utilized by ``JmesPath\search()`` and ``JmesPath\Env::search()`` |
| 54 | +by default. |
| 55 | + |
| 56 | +AstRuntime |
| 57 | +~~~~~~~~~~ |
| 58 | + |
| 59 | +The AstRuntime will parse an expression, cache the resulting AST in memory, |
| 60 | +and interpret the AST using an external tree visitor. AstRuntime provides a |
| 61 | +good general approach for interpreting JMESPath expressions that have a low to |
| 62 | +moderate level of reuse. |
| 63 | + |
| 64 | +.. code-block:: php |
| 65 | +
|
| 66 | + $runtime = new JmesPath\AstRuntime(); |
| 67 | + $runtime('foo.bar', ['foo' => ['bar' => 'baz']]); |
| 68 | + // > 'baz' |
| 69 | +
|
| 70 | +CompilerRuntime |
| 71 | +~~~~~~~~~~~~~~~ |
| 72 | + |
| 73 | +``JmesPath\CompilerRuntime`` provides the most performance for |
| 74 | +applications that have a moderate to high level of reuse of JMESPath |
| 75 | +expressions. The CompilerRuntime will walk a JMESPath AST and emit PHP source |
| 76 | +code, resulting in anywhere from 7x to 60x speed improvements. |
| 77 | + |
| 78 | +Compiling JMESPath expressions to source code is a slower process than just |
| 79 | +walking and interpreting a JMESPath AST (via the AstRuntime). However, |
| 80 | +running the compiled JMESPath code results in much better performance than |
| 81 | +walking an AST. This essentially means that there is a warm-up period when |
| 82 | +using the ``CompilerRuntime``, but after the warm-up period, it will provide |
| 83 | +much better performance. |
| 84 | + |
| 85 | +Use the CompilerRuntime if you know that you will be executing JMESPath |
| 86 | +expressions more than once or if you can pre-compile JMESPath expressions |
| 87 | +before executing them (for example, server-side applications). |
| 88 | + |
| 89 | +.. code-block:: php |
| 90 | +
|
| 91 | + // Note: The cache directory argument is optional. |
| 92 | + $runtime = new JmesPath\CompilerRuntime('/path/to/compile/folder'); |
| 93 | + $runtime('foo.bar', ['foo' => ['bar' => 'baz']]); |
| 94 | + // > 'baz' |
| 95 | +
|
| 96 | +Environment Variables |
| 97 | +^^^^^^^^^^^^^^^^^^^^^ |
| 98 | + |
| 99 | +You can utilize the CompilerRuntime in ``JmesPath\search()`` by setting |
| 100 | +the ``JP_PHP_COMPILE`` environment variable to "on" or to a directory |
| 101 | +on disk used to store cached expressions. |
| 102 | + |
| 103 | +Testing |
| 104 | +======= |
| 105 | + |
| 106 | +A comprehensive list of test cases can be found at |
| 107 | +https://github.com/jmespath/jmespath.php/tree/master/tests/compliance. |
| 108 | +These compliance tests are utilized by jmespath.php to ensure consistency with |
| 109 | +other implementations, and can serve as examples of the language. |
| 110 | + |
| 111 | +jmespath.php is tested using PHPUnit. In order to run the tests, you need to |
| 112 | +first install the dependencies using Composer as described in the *Installation* |
| 113 | +section. Next you just need to run the tests via make: |
| 114 | + |
| 115 | +.. code-block:: bash |
| 116 | +
|
| 117 | + make test |
| 118 | +
|
| 119 | +You can run a suite of performance tests as well: |
| 120 | + |
| 121 | +.. code-block:: bash |
| 122 | +
|
| 123 | + make perf |
0 commit comments