or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/pypi-types-ujson

Type stubs for ujson - Ultra fast JSON encoder and decoder for Python

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/types-ujson@4.2.x

To install, run

npx @tessl/cli install tessl/pypi-types-ujson@4.2.0

index.mddocs/

Types-ujson

Type stubs for ujson - an ultra-fast JSON encoder and decoder for Python. This package provides type annotations that enable static type checking and IDE support when working with ujson's high-performance JSON serialization and deserialization functions.

Package Information

  • Package Name: types-ujson
  • Package Type: pypi (Python type stubs)
  • Language: Python
  • Installation: pip install types-ujson

Core Imports

import ujson

Import specific functions:

from ujson import encode, decode, dumps, loads, dump, load, __version__

Basic Usage

import ujson

# Basic encoding (Python object to JSON string)
data = {"name": "Alice", "age": 30, "city": "New York"}
json_string = ujson.encode(data)
print(json_string)  # {"name":"Alice","age":30,"city":"New York"}

# Basic decoding (JSON string to Python object)
json_text = '{"name":"Bob","age":25}'
python_obj = ujson.decode(json_text)
print(python_obj)  # {'name': 'Bob', 'age': 25}

# Using common aliases
json_string = ujson.dumps(data)  # Same as encode
python_obj = ujson.loads(json_text)  # Same as decode

# File operations
with open('data.json', 'w') as f:
    ujson.dump(data, f, indent=2)

with open('data.json', 'r') as f:
    loaded_data = ujson.load(f)

Capabilities

JSON Encoding Functions

Convert Python objects to JSON strings with various formatting options.

def encode(
    obj: Any,
    ensure_ascii: bool = ...,
    double_precision: int = ...,
    encode_html_chars: bool = ...,
    escape_forward_slashes: bool = ...,
    sort_keys: bool = ...,
    indent: int = ...
) -> str:
    """
    Encode Python object to JSON string.
    
    Args:
        obj: Python object to encode (dict, list, str, int, float, bool, None)
        ensure_ascii: If True, escape non-ASCII characters (default: True)
        double_precision: Number of decimal places for floats (default: 9)
        encode_html_chars: If True, encode HTML characters like <>&" (default: False)
        escape_forward_slashes: If True, escape forward slashes (default: True)  
        sort_keys: If True, sort dictionary keys (default: False)
        indent: Indentation level for pretty printing (default: 0)
        
    Returns:
        JSON string representation of the object
    """

def dumps(
    obj: Any,
    ensure_ascii: bool = ...,
    double_precision: int = ...,
    encode_html_chars: bool = ...,
    escape_forward_slashes: bool = ...,
    sort_keys: bool = ...,
    indent: int = ...
) -> str:
    """
    Serialize Python object to JSON string. Alias for encode().
    
    Args:
        obj: Python object to serialize
        ensure_ascii: If True, escape non-ASCII characters
        double_precision: Number of decimal places for floats
        encode_html_chars: If True, encode HTML characters like <>&"
        escape_forward_slashes: If True, escape forward slashes
        sort_keys: If True, sort dictionary keys
        indent: Indentation level for pretty printing
        
    Returns:
        JSON string representation of the object
    """

def dump(
    obj: Any,
    fp: IO[str],
    ensure_ascii: bool = ...,
    double_precision: int = ...,
    encode_html_chars: bool = ...,
    escape_forward_slashes: bool = ...,
    sort_keys: bool = ...,
    indent: int = ...
) -> None:
    """
    Serialize Python object to JSON and write to file-like object.
    
    Args:
        obj: Python object to serialize
        fp: File-like object to write JSON to (must have write() method)
        ensure_ascii: If True, escape non-ASCII characters
        double_precision: Number of decimal places for floats
        encode_html_chars: If True, encode HTML characters like <>&"
        escape_forward_slashes: If True, escape forward slashes
        sort_keys: If True, sort dictionary keys
        indent: Indentation level for pretty printing
        
    Returns:
        None (writes to file)
    """

JSON Decoding Functions

Parse JSON strings into Python objects with precision control.

def decode(s: AnyStr, precise_float: bool = ...) -> Any:
    """
    Decode JSON string to Python object.
    
    Args:
        s: JSON string or bytes to decode
        precise_float: If True, use precise float parsing (default: False)
        
    Returns:
        Python object (dict, list, str, int, float, bool, or None)
    """

def loads(s: AnyStr, precise_float: bool = ...) -> Any:
    """
    Parse JSON string to Python object. Alias for decode().
    
    Args:
        s: JSON string or bytes to parse
        precise_float: If True, use precise float parsing
        
    Returns:
        Python object (dict, list, str, int, float, bool, or None)
    """

def load(fp: IO[AnyStr], precise_float: bool = ...) -> Any:
    """
    Deserialize JSON from file-like object to Python object.
    
    Args:
        fp: File-like object to read JSON from (must have read() method)
        precise_float: If True, use precise float parsing
        
    Returns:
        Python object (dict, list, str, int, float, bool, or None)
    """

Version Information

Access the ujson library version.

__version__: str
    """
    Version string of the ujson library.
    """

Types

from typing import IO, Any, AnyStr

# AnyStr represents either str or bytes
# Any represents any Python object type
# IO[str] represents file-like objects that work with strings
# IO[AnyStr] represents file-like objects that work with strings or bytes

Usage Examples

Encoding with Options

import ujson

data = {
    "user": "Alice",
    "score": 95.7,
    "tags": ["python", "json", "fast"]
}

# Pretty-printed JSON with sorted keys
formatted = ujson.encode(data, indent=2, sort_keys=True)
print(formatted)

# Ensure ASCII encoding for non-ASCII characters
non_ascii_data = {"name": "José", "city": "São Paulo"}
ascii_json = ujson.encode(non_ascii_data, ensure_ascii=True)
print(ascii_json)  # {"name":"Jos\\u00e9","city":"S\\u00e3o Paulo"}

# Control float precision
float_data = {"pi": 3.141592653589793}
precise = ujson.encode(float_data, double_precision=15)
print(precise)  # {"pi":3.141592653589793}

File Operations

import ujson

# Write JSON to file
data = {"users": [{"name": "Alice", "id": 1}, {"name": "Bob", "id": 2}]}

with open("users.json", "w") as f:
    ujson.dump(data, f, indent=2)

# Read JSON from file  
with open("users.json", "r") as f:
    loaded_data = ujson.load(f)
    
print(loaded_data["users"][0]["name"])  # Alice

Precise Float Handling

import ujson

# Standard parsing
json_with_floats = '{"value": 0.1234567890123456789}'
standard = ujson.loads(json_with_floats)
print(standard["value"])  # 0.12345678901234568

# Precise float parsing
precise = ujson.loads(json_with_floats, precise_float=True)
print(precise["value"])  # 0.1234567890123456789