Core functionality for Pydantic validation and serialization
—
High-performance JSON parsing and serialization functions with extensive configuration options for handling edge cases, infinity/NaN values, and custom serialization formats. These functions provide fast JSON operations that significantly outperform Python's standard library.
Fast JSON parsing with support for various parsing options and edge case handling.
def from_json(data, *, allow_inf_nan=True, cache_strings='all', allow_partial='off'):
"""
Parse JSON data into Python objects with high performance.
Args:
data: JSON string, bytes, or bytearray to parse
allow_inf_nan: Whether to allow parsing of Infinity and NaN values
cache_strings: String caching mode ('all', 'keys', 'none')
allow_partial: Partial parsing mode ('off', 'on', 'trailing-strings')
Returns:
Parsed Python object (dict, list, str, int, float, bool, or None)
Raises:
ValueError: If JSON is invalid or parsing fails
"""Fast JSON serialization with extensive formatting and filtering options.
def to_json(obj, *, indent=None, include=None, exclude=None, by_alias=False, exclude_unset=False, exclude_defaults=False, exclude_none=False, round_trip=False, warnings=True, fallback=None, serialize_as_any=False) -> bytes:
"""
Serialize Python objects to JSON bytes with high performance.
Args:
obj: Python object to serialize
indent: JSON indentation (None for compact, int for spaces, str for custom)
include: Set/dict of fields to include (None for all)
exclude: Set/dict of fields to exclude (None for none)
by_alias: Whether to use field aliases during serialization
exclude_unset: Whether to exclude fields that were not explicitly set
exclude_defaults: Whether to exclude fields with default values
exclude_none: Whether to exclude fields with None values
round_trip: Whether to preserve exact types for round-trip compatibility
warnings: Whether to show serialization warnings
fallback: Function to handle non-serializable values
serialize_as_any: Whether to serialize unknown types as Any
Returns:
JSON as bytes
Raises:
PydanticSerializationError: If serialization fails
"""Convert data to JSON-serializable Python objects without actually serializing to JSON.
def to_jsonable_python(obj, *, fallback=None, serialize_as_any=False, when_used='json'):
"""
Convert Python objects to JSON-serializable Python objects.
Args:
obj: Python object to convert
fallback: Function to handle non-serializable values
serialize_as_any: Whether to serialize unknown types as Any
when_used: Context hint for serialization ('json' or 'python')
Returns:
JSON-serializable Python object (dict, list, str, int, float, bool, or None)
Raises:
PydanticSerializationError: If conversion fails
"""Additional utility functions for error handling and build information.
def list_all_errors() -> list[ErrorTypeInfo]:
"""
Get information about all available error types.
Returns:
List of ErrorTypeInfo dictionaries containing error type metadata
"""
# Build and version information
__version__: str # Current version of pydantic-core
build_profile: str # Build profile used (debug/release/etc.)
build_info: str # Build information and compilation details
_recursion_limit: int # Maximum recursion limit for validationfrom pydantic_core import from_json
# Parse simple JSON
json_str = '{"name": "Alice", "age": 30, "active": true}'
result = from_json(json_str)
print(result) # {'name': 'Alice', 'age': 30, 'active': True}
# Parse JSON from bytes
json_bytes = b'[1, 2, 3, "hello", null]'
result = from_json(json_bytes)
print(result) # [1, 2, 3, 'hello', None]
# Parse with special values
json_with_inf = '{"value": Infinity, "nan_value": NaN}'
result = from_json(json_with_inf, allow_inf_nan=True)
print(result) # {'value': inf, 'nan_value': nan}
# Disable special value parsing
try:
from_json(json_with_inf, allow_inf_nan=False)
except ValueError as e:
print(f"Error: {e}") # Error: Invalid JSONfrom pydantic_core import from_json
# Different string caching modes
large_json = '{"key1": "value1", "key2": "value2", "key1": "value3"}'
# Cache all strings (default, fastest for repeated strings)
result1 = from_json(large_json, cache_strings='all')
# Cache only keys (good balance for objects with unique values)
result2 = from_json(large_json, cache_strings='keys')
# No caching (lowest memory usage)
result3 = from_json(large_json, cache_strings='none')from pydantic_core import to_json
# Serialize simple data
data = {'name': 'Bob', 'age': 25, 'scores': [95, 87, 92]}
json_bytes = to_json(data)
print(json_bytes.decode()) # {"name":"Bob","age":25,"scores":[95,87,92]}
# Serialize with formatting
formatted = to_json(data, indent=2)
print(formatted.decode())
# {
# "name": "Bob",
# "age": 25,
# "scores": [
# 95,
# 87,
# 92
# ]
# }
# Serialize with custom indentation
custom_indent = to_json(data, indent=' ') # 4 spaces
print(custom_indent.decode())from pydantic_core import to_json
data = {
'id': 123,
'name': 'Charlie',
'email': 'charlie@example.com',
'password': 'secret123',
'active': True,
'metadata': None
}
# Include only specific fields
public_json = to_json(data, include={'id', 'name', 'active'})
print(public_json.decode()) # {"id":123,"name":"Charlie","active":true}
# Exclude sensitive fields
safe_json = to_json(data, exclude={'password'})
print(safe_json.decode()) # {"id":123,"name":"Charlie","email":"charlie@example.com","active":true,"metadata":null}
# Exclude None values
no_nulls = to_json(data, exclude_none=True)
print(no_nulls.decode()) # {"id":123,"name":"Charlie","email":"charlie@example.com","password":"secret123","active":true}from pydantic_core import to_json
from datetime import datetime, date
import uuid
# Data with non-JSON-native types
data = {
'timestamp': datetime.now(),
'date': date.today(),
'id': uuid.uuid4(),
'custom_object': object()
}
# Define fallback function
def serialize_fallback(obj):
if isinstance(obj, datetime):
return obj.isoformat()
elif isinstance(obj, date):
return obj.isoformat()
elif isinstance(obj, uuid.UUID):
return str(obj)
else:
return f"<{type(obj).__name__} object>"
# Serialize with fallback
result = to_json(data, fallback=serialize_fallback)
print(result.decode())
# {"timestamp":"2023-10-15T10:30:45.123456","date":"2023-10-15","id":"123e4567-e89b-12d3-a456-426614174000","custom_object":"<object object>"}
# Without fallback, non-serializable values cause errors
try:
to_json(data)
except Exception as e:
print(f"Serialization error: {e}")from pydantic_core import to_jsonable_python
from datetime import datetime
from decimal import Decimal
# Convert complex data to JSON-serializable Python objects
data = {
'name': 'Dave',
'balance': Decimal('123.45'),
'created': datetime(2023, 10, 15, 10, 30, 45),
'settings': {'theme': 'dark', 'notifications': True}
}
# Convert to JSON-serializable objects
json_safe = to_jsonable_python(data)
print(json_safe)
# Output will convert Decimal and datetime to appropriate JSON-safe types
# Use with standard json module
import json
json_str = json.dumps(json_safe)
print(json_str)from pydantic_core import to_json, from_json
from datetime import datetime
# Data that needs to preserve exact types
original_data = {
'timestamp': datetime(2023, 10, 15, 10, 30, 45),
'precision_number': 123.456789012345
}
# Serialize with round-trip preservation
json_bytes = to_json(original_data, round_trip=True)
# Parse back - types should be preserved
restored_data = from_json(json_bytes)
# Verify round-trip integrity
print(f"Original: {original_data}")
print(f"Restored: {restored_data}")
print(f"Types match: {type(original_data['timestamp']) == type(restored_data['timestamp'])}")import json
from pydantic_core import from_json, to_json
import time
# Large data for performance testing
large_data = {
'users': [
{'id': i, 'name': f'User{i}', 'email': f'user{i}@example.com'}
for i in range(10000)
]
}
# Serialize with pydantic-core
start = time.time()
pydantic_json = to_json(large_data)
pydantic_serialize_time = time.time() - start
# Serialize with standard library
start = time.time()
stdlib_json = json.dumps(large_data).encode()
stdlib_serialize_time = time.time() - start
print(f"pydantic-core serialization: {pydantic_serialize_time:.4f}s")
print(f"stdlib serialization: {stdlib_serialize_time:.4f}s")
print(f"pydantic-core is {stdlib_serialize_time/pydantic_serialize_time:.1f}x faster")
# Parse with pydantic-core
start = time.time()
pydantic_parsed = from_json(pydantic_json)
pydantic_parse_time = time.time() - start
# Parse with standard library
start = time.time()
stdlib_parsed = json.loads(stdlib_json.decode())
stdlib_parse_time = time.time() - start
print(f"pydantic-core parsing: {pydantic_parse_time:.4f}s")
print(f"stdlib parsing: {stdlib_parse_time:.4f}s")
print(f"pydantic-core is {stdlib_parse_time/pydantic_parse_time:.1f}x faster")Install with Tessl CLI
npx tessl i tessl/pypi-pydantic-core