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

TO DO when registered on pip.

Testing

You can test that everything behaves correctly...

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: textx_lang_json-0.0.dev1.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.dev1.tar.gz
Algorithm Hash digest
SHA256 9317457634ee3695596bb1a631e43a34134e7c6b76c921ee40a12953a0ed6e6f
MD5 2da51dcaecc844058367bfe826433310
BLAKE2b-256 ea1dee23456b8e3bcdfeb8742cc9b9b27b182db2272f5f41295dc35af701edae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for textx_lang_json-0.0.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 420b25935fa6fd7f99b5d7a50e463289c01c76a5eb1a1d0ed1cca8b2feb8acdf
MD5 69855def98a3162176cbf2b721941de3
BLAKE2b-256 c65dc8fc88f41605f6fd4c3ce827edaab9abdcdf69d7edd7ce657a33cf3b0353

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