|
| 1 | +# Python RSON |
| 2 | + |
| 3 | + |
| 4 | +RSON is a superset of the JSON format. It extends JSON with the primary goal of allowing |
| 5 | +references in the JSON itself. |
| 6 | + |
| 7 | +In addition to that, RSON also allows: |
| 8 | +- [x] Line comments |
| 9 | +- [x] Block comments |
| 10 | +- [x] Trailing commas |
| 11 | + |
| 12 | +> These are just some nicities are not the primary goal of the RSON format. |
| 13 | +
|
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +The RSON format allows references by adding 2 types of tokens: |
| 18 | +1. DEF node - defines a `ref-name` for the value |
| 19 | +2. REF node - references the given `ref-name` |
| 20 | + |
| 21 | +The `DEF` node can be used on any JSON value. This node defines a name for the value which can then used |
| 22 | +to refer to the same value. |
| 23 | +The `REF` node can only appear as an *object value* or an *array member*, and is used to refer to a |
| 24 | +defined name. |
| 25 | + |
| 26 | +Here is an example of how this would work: |
| 27 | +```json |
| 28 | +// team.rson |
| 29 | + |
| 30 | +[ |
| 31 | + { |
| 32 | + "members": [ |
| 33 | + { |
| 34 | + "name": "Alice", |
| 35 | + "role": $ROLE_DEVELOPER |
| 36 | + }, |
| 37 | + { |
| 38 | + "name": "Bob", |
| 39 | + "role": $ROLE_MANAGER |
| 40 | + }, |
| 41 | + { |
| 42 | + "name": "Charlie", |
| 43 | + "role": $ROLE_DESIGNER |
| 44 | + } |
| 45 | + ], |
| 46 | + "roles": [ |
| 47 | + { |
| 48 | + "name": "Developer" |
| 49 | + }(ROLE_DEVELOPER), |
| 50 | + { |
| 51 | + "name": "Designer" |
| 52 | + }(ROLE_DESIGNER), |
| 53 | + { |
| 54 | + "name": "Manager" |
| 55 | + }(ROLE_MANAGER) |
| 56 | + ] |
| 57 | + } |
| 58 | +] |
| 59 | +``` |
| 60 | + |
| 61 | +We can load the `teams.rson` file with the following code: |
| 62 | +```py |
| 63 | +from rson import load |
| 64 | + |
| 65 | +with open("teams.rson") as f: |
| 66 | + team = load(f)[0] |
| 67 | + |
| 68 | + target_role_name = input("Which role to look for? ") |
| 69 | + |
| 70 | + for role in team["roles"]: |
| 71 | + if role["name"].lower() == target_role_name.lower(): |
| 72 | + target_role = role |
| 73 | + break |
| 74 | + else: |
| 75 | + print("Could not find role " + target_role_name) |
| 76 | + exit() |
| 77 | + |
| 78 | + members_with_role = [ |
| 79 | + member for member in team["members"] |
| 80 | + if member["role"] is target_role # <- note the identity check here |
| 81 | + ] |
| 82 | + |
| 83 | + print(f"Found {len(members_with_role)} members with role {role['name']}:") |
| 84 | + for i, member in enumerate(members_with_role): |
| 85 | + print(f"\t{i}. {member['name']}") |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +and a sample execution: |
| 90 | +``` |
| 91 | +Which role to look for? developer |
| 92 | +Found 2 members with role Developer: |
| 93 | + 0. Alice |
| 94 | + 1. David |
| 95 | +``` |
| 96 | + |
| 97 | +## Specification |
| 98 | + |
| 99 | +The `ref-name` must conform to the following regex: |
| 100 | +```re |
| 101 | +[A-Za-z_][A-Za-z_0-9]* |
| 102 | +``` |
| 103 | + |
| 104 | +Or, in plain english: |
| 105 | +> The first character must be a valid english letter or an underscore, and the |
| 106 | +> following letters must be either an english letter, an underscore or a digit. |
| 107 | +> A `ref-name` must have at least 1 character (the first character). |
| 108 | +
|
| 109 | + |
| 110 | +A `DEF` node can be used for every JSON value, so this makes the following |
| 111 | +valid as well: |
| 112 | +```json |
| 113 | +{ |
| 114 | + "organization": { |
| 115 | + "name": "Xpo Development"(ORG_NAME) |
| 116 | + }, |
| 117 | + "title": $ORG_NAME |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | +## Contributing |
| 123 | + |
| 124 | +Feel free to open an issue or a PR. |
| 125 | + |
| 126 | +## ToDo |
| 127 | + |
| 128 | +- [x] Implement the `load` function |
| 129 | +- [x] Implement the `loads` function |
| 130 | +- [ ] Implement the `dump` function |
| 131 | +- [ ] Implement the `dumps` function |
0 commit comments