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: 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.parse_str(filepath: 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
    
    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 _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: 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


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

Uploaded Source

Built Distribution

jsonc_parser-1.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jsonc-parser-1.1.0.tar.gz
  • Upload date:
  • Size: 4.3 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.0.tar.gz
Algorithm Hash digest
SHA256 0a074faaab4c71c1ce1210fd1f651ccc1219f5a7396674aa730520026e70af44
MD5 14da06c9913ad68f4f5d50c7b186469c
BLAKE2b-256 9bf79c9550a5bfffdd00542c042dd151c039d48ec748e190034e64ea9f21270f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jsonc_parser-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56b45aaf22ae3bcec7a9bec0a2232a96035a6046f639a513b86531878af724d2
MD5 a4236960d75a8877500cab5ab4e44227
BLAKE2b-256 91bfcece71ed98e1aac70221538d070b636b8868b6ae255278c180759ec27d3e

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