A lightweight, native tool for parsing .jsonc files
Project description
jsonc-parser
This package is a lightweight, zero-dependency module for parsing files with .jsonc extension. (a.k.a. JSON with comments)
Installation
To install this package, simply download it from PyPI:
pip install jsonc-parser
Also you can build it yourself from source code available on GitHub
Usage
You need to just import JsoncParser class from this package:
from jsonc_parser.parser import JsoncParser
This class requires no instance to function (i.e. it is fully static)
Functions
These are all methods that JsoncParser class provides for working with .jsonc files:
-
JsoncParser.parse_file(filepath: str) -> dict
This function parses file, specified in filepath parameter, and deserializes it into a valid Python object (dictionary), removing any comment in the process. No alterations are made it the file itself. filepath parameter specifies path to .jsonc file.
from jsonc-parser.parser import JsoncParser file_path = "./data.jsonc" # Content from 'data.jsonc' -> {"version": "1.0.0", /*This is my project's version*/} data = JsoncParser.parse_file(file_path) print(data) # Output: {'version': '1.0.0'}
This function can raise FunctionParameterError if filepath parameter is not a string or is empty. Also this function will raise FileError exception if file's format is unsupported and a ParserError exception if file cannot be parsed/contains invalid JSON data.
-
JsoncParser.convert_to_json(filepath: str, remove_file: bool = False) -> None
This function converts file from .jsonc to .json format, removing any comments in the process. filepath parameter specifies path to file and remove_file parameter specifies if .jsonc file will be removed (deleted from hard drive) after conversion. If set to True, this function will delete .jsonc file leaving only .json file. Otherwise, both files are not deleted. This function can raise FunctionParameterError if filepath parameter is not a string or is empty or if remove_file parameter is not a boolean.
-
JsoncParser.convert_to_jsonc(filepath: str, remove_file: bool = False) -> None
This function converts file from .json to .jsonc format, enabling comment support. filepath parameter specifies path to file and remove_file parameter specifies if .jsonc file will be removed (deleted from hard drive) after conversion. If set to True, this function will delete .jsonc file leaving only .json file. Otherwise, both files are not deleted. This function can raise FunctionParameterError if filepath parameter is not a string or is empty or if remove_file parameter is not a boolean.
Exceptions
There are a total of 3 custom exceptions that jsonc-parser can raise during its runtime. To access the in your script, simply imprt thef from class='exc-code'>jsonc_parser.errors module:
from jsonc_parser.errors import FileError, IncorrectParameterError, ParserError
Exceptions:
-
FileError
This exception indicates that there is a problem with selected file. -
FunctionParameterError
This exception indicates that some of function's parameters are invalid. They may have wrong type, have invalid values or be errorous in some other way. -
ParserError
This exception indicates that file cannot be parsed. It can have wrong extension, invalid data, etc
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 jsonc-parser-1.0.0.tar.gz
.
File metadata
- Download URL: jsonc-parser-1.0.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c930ca94dafabfea0a711fd7ea58c734ba79417c515a91364d5aa46edde0ec7b |
|
MD5 | 0723a010334f02b855b34e60021efe06 |
|
BLAKE2b-256 | 5ccc80d99dd43d4400d7bcb6dac147f3e69efa267cb90402fb7962390e06f362 |
File details
Details for the file jsonc_parser-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: jsonc_parser-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6ebec2d9fd43a91841f3810d44d72342f0af28fc2a2a5e942a4ee223f1a4f8b |
|
MD5 | b94af5e4b989b06444d1251234bad32a |
|
BLAKE2b-256 | 76a6d54b689ab768afcc24fa5a0a8fc8513a314e9964d17ef853ad3b322a8b23 |