Map file-based JSON inputs from the file keys to their locations in the original file
Project description
JSON Mapper
Tooling that can map a given JSON key to either a file offset or a line/column pair
This tooling is still in beta and the API is subject to change.
Purpose
This was designed to support a custom VS code JSON validator in an unrelated product. We needed to be able to know information such as people.0.name
started at line 8 column 2 and ended at line 8 column 12, or that people.0
(the entire sub-object) spanned from line 6 column 1 through line 18 column 1.
Interface
The primary interface to the JSON mapper is json_mapper.mapper.JSONMapper
. This is initialized with a seekable IO object (such as a file or StringIO) and has the following public properties and methods:
JSONMapper.offsets
: A cached property that contains a mapping of all the keys in the object to their relative file positions. The keys are tuples representing the path to get to a given JSON object(('people', 0, 'name')
) in my example above and the offsets are named tuples withstart
andend
parameters. The offsets are inclusive at the start and non-inclusive at the end - the same as Python slice mechanics.JSONMapper.get_position(key: Tuple)
: Get the start and end line/column offsets for a given JSON key. Ajson_mapper.mapper.Position
object is returned which hasstart_line
,start_col
,end_line
, andend_col
stored values, and aneditor
calculated property, which returns ajson_mapper.mapper.EditorPosition
JSONMapper.get_position(key: Tuple).editor
: Get thejson_mapper.mapper.EditorPosition
object for a given JSON key. This is very similar to the non-editor based object, except that it uses inclusive starts and ends and all values are one based. This is what you will want to use if you are trying to use this library to write a VS Code task that highlights JSON files for problems.
Example
Installation: pip install json-position-mapper
Sample JSON file: example.json
{
"people": [
{
"name": "Adam",
"favoriteFood": "pie",
"attributes": {
"eyeColor": "orange",
"height": "12 foot",
"happy": true
}
},
{
"name": "David",
"favoriteFood": "sushi",
"attributes": {
"eyeColor": "fuschia",
"height": "4 inches",
"happy": null
}
}
]
}
Example Python file: example.py
from io import StringIO
from json_mapper.mapper import JSONMapper, Position, Offset
with open("example.json") as f_in:
in_memory = StringIO(f_in.read())
mapper = JSONMapper(in_memory)
# Get the entire slice offset of the 'people' key
print(mapper.offsets[("people",)])
# Output: Offset(start=16, end=488)
# Get the slice offset for people.0.name
print(mapper.offsets[("people", 0, "name")])
# Output: Offset(start=48, end=54)
# Get the line/column start and end for people.0
# This is what we would use if we were to use Python to split the file on line and then use slice mechanics
print(mapper.get_position(("people", 0)))
# Output: Position(start_line=2, start_col=8, end_line=10, end_col=9)
# This is what we would use to highlight in something like VS Code
print(mapper.get_position(("people", 0)).editor)
# Output: EditorPosition(start_line=3, start_col=9, end_line=11, end_col=9)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file json-position-mapper-0.0.1.tar.gz
.
File metadata
- Download URL: json-position-mapper-0.0.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.9.12 Darwin/21.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 610cbabf1ef4da733d13ade5a3fc62360b5f02056f25d83c943ad567297b9d41 |
|
MD5 | 07e3ff1dfdfcd8a2bda21123b1d01d3a |
|
BLAKE2b-256 | 0dc1d04f747cce06f5299cceeeb6aa22ff3dde3257ddda31adbeb45f0ae246fc |
Provenance
File details
Details for the file json_position_mapper-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: json_position_mapper-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.9.12 Darwin/21.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ef7453928dd5c38c60796adf4d0f6e1fbda0d3167694c003c5dd4b909d52e33 |
|
MD5 | 99d42be9b07374ae4c6e8a18acf85273 |
|
BLAKE2b-256 | b963c5c91a48d1c3d62c56be9dabfec0ab788ba391262f5014f0b09504c1177e |