Skip to main content

Fast iterable JSON parser.

Project description

jiter

CI pypi versions license

This is a standalone version of the JSON parser used in pydantic-core. The recommendation is to only use this package directly if you do not use pydantic.

The API is extremely minimal:

def from_json(
    json_data: bytes,
    /,
    *,
    allow_inf_nan: bool = True,
    cache_mode: Literal[True, False, 'all', 'keys', 'none'] = 'all',
    partial_mode: Literal[True, False, 'off', 'on', 'trailing-strings'] = False,
    catch_duplicate_keys: bool = False,
    float_mode: Literal['float', 'decimal', 'lossless-float'] = 'float',
) -> Any:
    """
    Parse input bytes into a JSON object.

    Arguments:
        json_data: The JSON data to parse
        allow_inf_nan: Whether to allow infinity (`Infinity` an `-Infinity`) and `NaN` values to float fields.
            Defaults to True.
        cache_mode: cache Python strings to improve performance at the cost of some memory usage
            - True / 'all' - cache all strings
            - 'keys' - cache only object keys
            - False / 'none' - cache nothing
        partial_mode: How to handle incomplete strings:
            - False / 'off' - raise an exception if the input is incomplete
            - True / 'on' - allow incomplete JSON but discard the last string if it is incomplete
            - 'trailing-strings' - allow incomplete JSON, and include the last incomplete string in the output
        catch_duplicate_keys: if True, raise an exception if objects contain the same key multiple times
        float_mode: How to return floats: as a `float`, `Decimal` or `LosslessFloat`

    Returns:
        Python object built from the JSON input.
    """


def cache_clear() -> None:
    """
    Reset the string cache.
    """


def cache_usage() -> int:
    """
    get the size of the string cache.

    Returns:
        Size of the string cache in bytes.
    """

Examples

The main function provided by Jiter is from_json(), which accepts a bytes object containing JSON and returns a Python dictionary, list or other value.

import jiter

json_data = b'{"name": "John", "age": 30}'
parsed_data = jiter.from_json(json_data)
print(parsed_data)  # Output: {'name': 'John', 'age': 30}

Handling Partial JSON

Incomplete JSON objects can be parsed using the partial_mode= parameter.

import jiter

partial_json = b'{"name": "John", "age": 30, "city": "New Yor'

# Raise error on incomplete JSON
try:
    jiter.from_json(partial_json, partial_mode=False)
except ValueError as e:
    print(f'Error: {e}')

# Parse incomplete JSON, discarding incomplete last field
result = jiter.from_json(partial_json, partial_mode=True)
print(result)  # Output: {'name': 'John', 'age': 30}

# Parse incomplete JSON, including incomplete last field
result = jiter.from_json(partial_json, partial_mode='trailing-strings')
print(result)  # Output: {'name': 'John', 'age': 30, 'city': 'New Yor'}

Catching Duplicate Keys

The catch_duplicate_keys=True option can be used to raise a ValueError if an object contains duplicate keys.

import jiter

json_with_dupes = b'{"foo": 1, "foo": 2}'

# Default behavior (last value wins)
result = jiter.from_json(json_with_dupes)
print(result)  # Output: {'foo': 2}

# Catch duplicate keys
try:
    jiter.from_json(json_with_dupes, catch_duplicate_keys=True)
except ValueError as e:
    print(f'Error: {e}')

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

jiter_android-0.16.0.tar.gz (176.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jiter_android-0.16.0-cp313-cp313-android_31_arm64_v8a.whl (369.6 kB view details)

Uploaded Android API level 31+ ARM64 v8aCPython 3.13

File details

Details for the file jiter_android-0.16.0.tar.gz.

File metadata

  • Download URL: jiter_android-0.16.0.tar.gz
  • Upload date:
  • Size: 176.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for jiter_android-0.16.0.tar.gz
Algorithm Hash digest
SHA256 2270b37a85ced32030aa36d6b227108d07e5d3680c00a3c0bc4361ed8a1292fa
MD5 52ab5321c9891d7fd6d17445138436ae
BLAKE2b-256 c86e0abae80032de2ba923881e4060a32d2b0adf139130b03440676c85a3dc14

See more details on using hashes here.

File details

Details for the file jiter_android-0.16.0-cp313-cp313-android_31_arm64_v8a.whl.

File metadata

File hashes

Hashes for jiter_android-0.16.0-cp313-cp313-android_31_arm64_v8a.whl
Algorithm Hash digest
SHA256 bed9f707ccbf474082d7fce355804cbc17d426a1ab860a6e0b1d0a2a21bb1a47
MD5 5c6fa9a601a26505367110693ac6240b
BLAKE2b-256 283fcce9e154b527ca61b6ec83d8405ade891ca45756c83169150c22019589b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page