Skip to main content

A textX implementation of JSON.

Project description

textx-lang-json

logos

textx-lang-json is a python implementation of the JSON (JavaScript Object Notation) data interchange format RFC8259 using the textX meta-language. Though it is not intended to replace the standard python JSON encoder and decoder Lib/json, which is much faster, it is a good alternative when you want to mix some JSON in your own textX grammar, or a good starting point should you want to develop your own JSON-like grammar.

The textxjson package provides a parser (basically a textX metamodel), able to build a textx model from a JSON file or string. This model can be visualized, for educational purpose, but more importantly can be decoded to obtain the usual (as done by Lib/json) python representation of the JSON document.

textx-lang-json has been created by Jean-François Baget at the Boreal team (Inria and LIRMM). It is part of the textx-lang-dlgpe project.

Walkthrough

The following code demonstrates, in python, how to build a parser, generate a model from a python string respecting the JSON standard, and decode the model to obtain the usual python representation of the python string (in that case a dictionary). It also shows that parser.model_from_str(data).decode() returns the same python object as the standard json.loads(data).

from textx import metamodel_for_language

parser = metamodel_for_language('textxjson') # building the parser

data = '{"Hello": "World"}' # data is a python string respecting the JSON format
model = parser.model_from_str(data) # model is a JsonText object
textxresult = model.decode() # textxresult is a python dictionary

test1 = textxresult == {'Hello' : 'World'} # test1 is True

import json

jsonresult = json.loads(data) # using the standard python function to decode data

test2 = textxresult == jsonresult # test2 is True

Note that a parser can also read a JSON file:

model = parser.model_from_file("./path/to/data.json")

Installation

pip install textx-lang-json

Testing

You can test that everything behaves correctly (but first you have to clone the whole repository).

git clone https://github.com/Jean-Francois-Baget/textx-lang-json.git
cd textx-lang-json
python -m unittest
..............
----------------------------------------------------------------------
Ran 14 tests in 11.538s

OK

Thanks to ArenaNet whose GW2 API provided some data used in our testbed.

Usage

Building the parser

The first thing to do is to build the Json parser. This can be done with the following code.

from textx import metamodel_for_language

parser = metamodel_for_language('textxjson')

Visualizing the grammar

This parser can be used to obtain a graphical representation of the grammar json.tx. For more details on textx visualization, see https://textx.github.io/textX/visualization.html.

from textx.export import metamodel_export

metamodel_export(parser, 'json.dot')

This codes generates a file json.dot that can be visualized with Graphviz, as shown below.

parser

Parsing JSON

Most importantly, the parser can be used to generate a model from a python string encoding some JSON data, or directly from a JSON file.

Parsing a python string

The parsing below is demonstrated using a python string.

some_json = r'''
{
    "name" : "textx-lang-json",
    "authors" : [
        "Jean-François Baget"
    ],
    "year" : 2024,
    "tested" : true
}
'''

model = parser.model_from_str(some_json)

Parsing a JSON file

If we have the following JSON file data.json...

{
    "name" : "textx-lang-json",
    "authors" : [
        "Jean-François Baget"
    ],
    "year" : 2024,
    "tested" : true
}

... the parser can build the model directly from the file:

model = parser.model_from_file("data.json")

Visualizing the model

As for the parser, the model can be visualized.

from textx.export import model_export

model_export(model, 'model.dot')

This file model.dot can also be visualized with Graphviz.

model

Decoding the model

The method decode() is called on a model to obtain the usual python representation of JSON strings. The test shows the interest of this representation.

result = model.decode()

test = result['authors'][0] == 'Jean-François Baget' # test is True

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

textx_lang_json-0.0.dev3.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

textx_lang_json-0.0.dev3-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file textx_lang_json-0.0.dev3.tar.gz.

File metadata

  • Download URL: textx_lang_json-0.0.dev3.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for textx_lang_json-0.0.dev3.tar.gz
Algorithm Hash digest
SHA256 aaa8f22059739d409af39f8206327453da13775d8abf739a5a03b2764f63d032
MD5 ddc2727aac0693632f478537506f37be
BLAKE2b-256 bbaf2150023edae8c87070fd386dbff86a6036876160b79f1f82ca7cad0a7a93

See more details on using hashes here.

File details

Details for the file textx_lang_json-0.0.dev3-py3-none-any.whl.

File metadata

File hashes

Hashes for textx_lang_json-0.0.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 a5ee316f214aeb464a03d769c81c3f9f03fdbe8192a00e63e0ee8c59aac96e30
MD5 a54bfb0e77b4e1744011ccef86cdb829
BLAKE2b-256 26c9444757fef115ee1d8b2c18d2939264f26a1fedd9cf111c35f92cd3a9c07c

See more details on using hashes here.

Supported by

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