Skip to main content

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: PathLike) -> 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 path-like object (str, bytes object representing a path, or os.PathLike compliant) 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.parse_str(_string: str) -> dict

    This function parses string, specified in _string parameter, and deserializes it into a valid Python object (dictionary), removing any comment in the process.

    from jsonc-parser.parser import JsoncParser
    
    json_string = """{"version": "1.0.0" /*This is my project's version*/}"""
    
    data = JsoncParser.parse_str(json_string)
    
    print(data)
    # Output: {'version': '1.0.0'}
    

    This function can raise FunctionParameterError if _string parameter is not a string or is empty. Also this function will raise a ParserError exception if file cannot be parsed/contains invalid JSON data.

    JsoncParser.convert_to_json(filepath: PathLike, 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 path-like object or is empty or if remove_file parameter is not a boolean.

  • JsoncParser.convert_to_jsonc(filepath: PathLike, 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 path-like object 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 import them from 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 erroneous in some other way.
  • ParserError

    This exception indicates that file cannot be parsed. It can have wrong extension, invalid data, etc.

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

jsonc-parser-1.1.5.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

jsonc_parser-1.1.5-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file jsonc-parser-1.1.5.tar.gz.

File metadata

  • Download URL: jsonc-parser-1.1.5.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.2

File hashes

Hashes for jsonc-parser-1.1.5.tar.gz
Algorithm Hash digest
SHA256 7126d17725b0413cd40af4297d9f6412c4181a62135e4c41cdf8f6a82c5936e6
MD5 d8f470e86504e7f8dc7da3fa3271e131
BLAKE2b-256 3585bec7d88eca76c6d014d8efc17b0a9babb0810213e974bff3b7928158216b

See more details on using hashes here.

Provenance

File details

Details for the file jsonc_parser-1.1.5-py3-none-any.whl.

File metadata

  • Download URL: jsonc_parser-1.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.2

File hashes

Hashes for jsonc_parser-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 abd1db76a4c6d1733ec7bb5340a89c49cbc878a181a1e7947ee6719eedf2c6cc
MD5 08a2b22ac2c74562b3003b0f004dd2cf
BLAKE2b-256 62be7d27877578c43decdc38a5fac0c7aecde2e0d2f7b50869131ef9bfd1d36e

See more details on using hashes here.

Provenance

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