-
Notifications
You must be signed in to change notification settings - Fork 48
JSON Regular Expressions
Since PFA is JSON, it is easier to inspect, build, or edit if you have good tools for manipulating JSON. JSON is represented in Python as None, True, False, integers, floating-point numbers, strings, Python lists of the above, and Python dictionaries of the above. Titus's "JSON regular expressions" provide a declarative language for manipulating these structures.
"JSON regular expressions" work like regular expressions for text, except that they apply to tree structures, rather than substrings of text. The titus.producer.tools library provides classes for defining patterns, performing JSON-tree searches using those patterns, and using pattern-matches for extractions or modifications.
Keep in mind that, despite the name, these "regular expressions" are not matching the string representation of the JSON, but the Python structure (effectively a DOM).
When you do from titus.producer.tools import *, you get a Python-based DSL for pattern matching. The pfainspector goes one step further and provides a shorter, regex-inspired syntax for the same pattern matching functions. The table below shows all of the patterns and their pfainspector equivalents.
| Python class | Example | pfainspector syntax | Matches |
|---|---|---|---|
| NoneType | None |
null |
null and only null
|
| bool |
True, False
|
true, false
|
only true and false
|
| int | 123 |
123 |
exact number in JSON |
| float | 3.14 |
3.14 |
exact number in JSON |
| str | "hello" |
"hello" |
exact string in JSON |
| list | [1, True, "hello"] |
[1, true, "hello"] |
exact array in JSON, may contain non-trivial patterns |
| dict | {"one": 1, "two": None} |
{one: 1, "two": null} |
exact object in JSON, may contain non-trivial patterns in the values (not keys) |
| Any | Any() |
_ |
anything: any structure or leaf node |
Any(int, str) |
(no equivalent) | specified Python classes |
Return to the Hadrian wiki table of contents.
Licensed under the Hadrian Personal Use and Evaluation License (PUEL).