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.dev2.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: textx_lang_json-0.0.dev2.tar.gz
  • Upload date:
  • Size: 11.6 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.dev2.tar.gz
Algorithm Hash digest
SHA256 c8f29fd590b6aeb01ff185059ea63a7e590ca95643ad71060c5a72311eb2acce
MD5 1d94a99a59082dbc2d4abae538533c55
BLAKE2b-256 84ef92a78ef12fd294334258ac70e4a536b35f54d4e76ac1a87b4badc982b5c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for textx_lang_json-0.0.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 ffecbea53e1843f0b66ef44c379d7a125ac74d1edaa0de35836a30ddc8a7e7be
MD5 fe2c21bdc6c9a7fab49626d460cfd395
BLAKE2b-256 a3e94f5aac85cf2e10c9d860a9fd6d3d21c0fd7c134e1b321364fa9e76337fb2

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