Skip to main content

A package to parse out C/JS style block and single line comments from JSON files.

Project description

jsonjsc

CI - Test CI - Build PyPI - Version PyPI - Python Version

A Python library for parsing out C/Javascript style comments in JSON files.

Install

pip install jsonjsc

Features

  • Simple and easy to use library with no other dependencies.
  • Supports C/JS block (/* */) and single line (//) comments.
  • Retains the line number and character column of JSON content after parsing, letting syntax error positions get properly reported by the normal Python JSON decoder.
  • Is easily dropped into existing JSON library usage as a decoder class.
  • Test backed via unittest.

Python 2

jsonjsc v1.1.2 is the last version to support Python 2.7.

pip install jsonjsc==1.1.2

Example

import json
import jsonjsc

TEST_JSON_DECODER = r'''{
    /*
    This is a test of the JSON decoder in full
    */
    "test1": "message1", // this comment should parse out.

    // "junk1": "message",

    /*
    "junk2": "another message",
    */

    "test2": "message2"
}'''

test = json.loads(TEST_JSON_DECODER, cls=jsonjsc.JSONCommentDecoder)

print(test["test1"])

if "junk1" not in test:
    print("I guess junk1 was commented out?")

if "junk2" not in test:
    print("I guess junk2 was commented out too!")

print(test["test2"])

Development

jsonjsc uses Hatchling as a build backend and flake8 as a style guide.

$ pip install -e .

Hatch is the primary project manager of choice, but any project adhering to PEP 621 (pyproject.toml specification) can be used.

$ hatch shell

Tests

Tests can be ran with pytest. Hatch scripts are included for linting and testing.

# Lint
$ hatch run lint:all

# Test with current Python version
$ hatch run full
# Test with all Python versions
$ hatch run test:full

TODOs

Implementation could probably be sped up significantly as it uses character by character searches to test if comments are in string values or not. No performance metrics have been taken.

License

Licensed under the MIT License. See LICENSE for more information.

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

jsonjsc-1.2.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

jsonjsc-1.2.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file jsonjsc-1.2.0.tar.gz.

File metadata

  • Download URL: jsonjsc-1.2.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for jsonjsc-1.2.0.tar.gz
Algorithm Hash digest
SHA256 6f20e705fa00da3be000c9b1f2d028f4ffbf55cfb35df0bac3679ec4bf048e8f
MD5 832c96726258d8ed88d15b878febdd1a
BLAKE2b-256 c936584439e6bc0102d526eec74827523d74087b64def96be16fd249b8a273fc

See more details on using hashes here.

File details

Details for the file jsonjsc-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: jsonjsc-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for jsonjsc-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 88f12bfffceeccfe28e91b176734e13a7f1ca15d648bc06acc7f95f998031b55
MD5 7c7da1006d49d66318d420573a7560b2
BLAKE2b-256 90df36eff9801ce9e8fdf1dd025b0ee14931da1d0cb7897dcc0d695fecd8f6cf

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