Skip to main content

Python implementation of RSON (Reference JSON)

Project description

Python RSON

RSON is a superset of the JSON format. It extends JSON with the primary goal of allowing references in the JSON itself.

In addition to that, RSON also allows:

  • Line comments
  • Block comments
  • Trailing commas

These are just some nicities are not the primary goal of the RSON format.

Usage

The RSON format allows references by adding 2 types of tokens:

  1. DEF node - defines a ref-name for the value
  2. REF node - references the given ref-name

The DEF node can be used on any JSON value. This node defines a name for the value which can then used to refer to the same value. The REF node can only appear as an object value or an array member, and is used to refer to a defined name.

Here is an example of how this would work:

// team.rson

[
    {
        "members": [
            {
                "name": "Alice",
                "role": $ROLE_DEVELOPER
            },
            {
                "name": "Bob",
                "role": $ROLE_MANAGER
            },
            {
                "name": "Charlie",
                "role": $ROLE_DESIGNER
            }
        ],
        "roles": [
            {
                "name": "Developer"
            }(ROLE_DEVELOPER),
            {
                "name": "Designer"
            }(ROLE_DESIGNER),
            {
                "name": "Manager"
            }(ROLE_MANAGER)
        ]
    }
]

We can load the teams.rson file with the following code:

from rson import load

with open("teams.rson") as f:
    team = load(f)[0]

    target_role_name = input("Which role to look for? ")

    for role in team["roles"]:
        if role["name"].lower() == target_role_name.lower():
            target_role = role
            break
    else:
        print("Could not find role " + target_role_name)
        exit()

    members_with_role = [
        member for member in team["members"]
        if member["role"] is target_role # <- note the identity check here
    ]

    print(f"Found {len(members_with_role)} members with role {role['name']}:")
    for i, member in enumerate(members_with_role):
        print(f"\t{i}. {member['name']}")

and a sample execution:

Which role to look for? developer
Found 2 members with role Developer:
        0. Alice
        1. David

Specification

The ref-name must conform to the following regex:

[A-Za-z_][A-Za-z_0-9]*

Or, in plain english:

The first character must be a valid english letter or an underscore, and the following letters must be either an english letter, an underscore or a digit. A ref-name must have at least 1 character (the first character).

A DEF node can be used for every JSON value, so this makes the following valid as well:

{
    "organization": {
        "name": "Xpo Development"(ORG_NAME)
    },
    "title": $ORG_NAME
}

Contributing

Feel free to open an issue or a PR.

ToDo

  • Implement the load function
  • Implement the loads function
  • Implement the dump function
  • Implement the dumps function

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_rson-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_rson-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file python_rson-0.1.0.tar.gz.

File metadata

  • Download URL: python_rson-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for python_rson-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3882c06f9c696ba251e4af1abd48afd31739599fb75ed4b9006eb30fc7619c99
MD5 6c8299763e016571b65358c7a1466afd
BLAKE2b-256 b512dd2568286e5e1c882de1cc3229b1dc7183ecb49d43a4e05b1933de4b47bf

See more details on using hashes here.

File details

Details for the file python_rson-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_rson-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for python_rson-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45fed38d524d88cfd1335eb95ff159ec89df5227c2595fc8cf38ce9d6a25bf88
MD5 3e7d5f916343301f3ad3e5377bc29882
BLAKE2b-256 b90416da2b7ab5a47a1c872797f6567dd3a7cf712c97e1dc577f8d78bed1cbe6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page