Skip to main content

yjson

PyPI Python versions CI

A fast JSON library for CPython, compatible with orjson. Its serializer and parser are written in Mojo.

  • About 1.3× faster than orjson at serializing a corpus of real-world JSON documents, and about 1.2× faster at parsing them.
  • loads(data, type=...) parses straight into dataclasses and validates them, without building dicts first.
  • A drop-in replacement for orjson, with the same API, options and output, verified against orjson's own test suite.
  • Native support for dataclasses, datetime, UUID, Enum, and NumPy arrays and scalars.
  • One deliberate difference in output: NaN and Infinity are written the way Python's json module writes them, not as null.

Installation

pip install yjson

Wheels are available for CPython 3.11–3.15 on x86-64 Linux with glibc 2.35 or newer (Ubuntu 22.04 or later), and require a CPU with AVX2. They bundle the Mojo runtime and have no dependencies. Releases before 0.2.0 installed the module as mojson.

Quickstart

import yjson

yjson.dumps({"id": 1, "tags": ["a", "b"], "score": float("nan")})
# b'{"id":1,"tags":["a","b"],"score":NaN}'

yjson.dumps({"b": 1, "a": 2}, option=yjson.OPT_SORT_KEYS | yjson.OPT_INDENT_2).decode()
# '{\n  "a": 2,\n  "b": 1\n}'

yjson.loads(b'{"id": 1}')
# {'id': 1}

Code written for orjson can usually switch by changing the import (import yjson as orjson); see Differences from orjson below. More examples are in examples/usage.py.

Serialization

dumps(obj, /, default=None, option=None) -> bytes

dumps returns compact UTF-8 JSON as bytes; call .decode() if you need a str. These types are serialized natively:

Type Output
str, int, float, bool, None JSON string, number, true/false, null
dict, list, tuple JSON object or array
Subclasses of str, int, dict, list As their base type
Dataclass instances Object of their fields, skipping names that start with _
datetime.datetime, date, time RFC 3339 string, e.g. "2024-01-02T03:04:05+01:00"
uuid.UUID String, e.g. "00000000-0000-0000-0000-000000000001"
enum.Enum The member's value
NumPy arrays and scalars See NumPy below
yjson.Fragment Its contents, inserted verbatim

Integers must fit in 64 bits (-2**63 to 2**64 - 1), and dict keys must be strings unless OPT_NON_STR_KEYS is set. Aware datetimes work with datetime.timezone, zoneinfo, dateutil, pendulum and pytz.

Any other object is passed to default, and its return value is serialized in its place:

import decimal

yjson.dumps({"amount": decimal.Decimal("12.50")}, default=str)
# b'{"amount":"12.50"}'

dumps raises JSONEncodeError, an alias of TypeError, for unsupported types, invalid keys, out-of-range integers, strings that are not valid UTF-8, and circular or too deeply nested data. If default raises, its exception is attached as __cause__.

Options

Options have the same names and values as orjson's, and are combined with |:

Option Effect
OPT_APPEND_NEWLINE Append \n to the output.
OPT_INDENT_2 Pretty-print with two-space indentation.
OPT_NAIVE_UTC Serialize naive datetime objects as UTC.
OPT_NON_STR_KEYS Allow int, float, bool, None, datetime, date, time, UUID and Enum keys.
OPT_OMIT_MICROSECONDS Drop microseconds from datetime and time values.
OPT_PASSTHROUGH_DATACLASS Pass dataclass instances to default.
OPT_PASSTHROUGH_DATETIME Pass datetime, date and time objects to default.
OPT_PASSTHROUGH_SUBCLASS Pass subclasses of str, int, dict and list to default.
OPT_SERIALIZE_NUMPY Accepted for compatibility; NumPy values are always serialized.
OPT_SORT_KEYS Sort dict keys.
OPT_STRICT_INTEGER Reject integers outside -(2**53 - 1) to 2**53 - 1, JavaScript's safe integer range.
OPT_UTC_Z Write a UTC offset as Z instead of +00:00.

OPT_SERIALIZE_DATACLASS and OPT_SERIALIZE_UUID are deprecated and equal to 0, as in orjson. OPT_STRICT_INTEGER and OPT_PASSTHROUGH_SUBCLASS use a slower, generic code path; all other options run on the same optimized writers as the default.

Fragments

Fragment embeds JSON that is already serialized, such as a cached value:

yjson.dumps({"cached": yjson.Fragment(b'{"a":1}')})
# b'{"cached":{"a":1}}'

The contents are inserted as they are, without validation.

Deserialization

loads(obj, /, *, type=None) -> Any

loads parses str, bytes, bytearray or memoryview input with orjson's rules. It rejects invalid UTF-8, lone surrogates, NaN and Infinity, a byte order mark, trailing commas and nesting deeper than 1,024 levels, and returns integers outside [-2⁶³, 2⁶⁴) as floats. Invalid input raises JSONDecodeError, a subclass of json.JSONDecodeError and ValueError, whose pos, lineno and colno give the position where parsing stopped.

Decoding into dataclasses

With type, loads builds the objects an annotation describes instead of dicts and lists, and checks the document against the annotation as it parses:

import dataclasses
from typing import Optional

@dataclasses.dataclass(slots=True)
class Address:
    city: str
    zip: str = ""

@dataclasses.dataclass(slots=True)
class User:
    id: int
    name: str
    tags: list[str]
    address: Address
    note: Optional[str] = None

user_json = b'{"id": 7, "name": "Ada", "tags": ["x"], "address": {"city": "Lahore"}}'
yjson.loads(user_json, type=User)
# User(id=7, name='Ada', tags=['x'], address=Address(city='Lahore', zip=''), note=None)

users_json = b'[{"id": 1, "name": "Grace", "tags": [], "address": {"city": "Oslo"}}]'
yjson.loads(users_json, type=list[User])
# [User(id=1, name='Grace', tags=[], address=Address(city='Oslo', zip=''), note=None)]

yjson.loads(b'{"id": "7"}', type=User)
# JSONDecodeError: expected int, got str: line 1 column 8 (char 7)

Supported annotations are Any, int, float, str, bool, None, Optional[X] (or X | None), list[X], dict[str, X], and dataclasses whose fields use these, nested to any depth and recursively. Anything else, including other unions, tuples, sets, Enum, datetime and InitVar fields, raises TypeError.

For dataclasses:

  • JSON keys match field names. Keys the dataclass does not declare are parsed and ignored.
  • A missing field takes its default or default_factory. A missing field without one, or a value of the wrong type, raises JSONDecodeError with its position.
  • int fields accept only integers; float fields also accept integers and convert them.
  • Instances are created without calling __new__ or __init__: fields are stored directly, then __post_init__ runs if the class defines it. Frozen dataclasses work too.
  • slots=True dataclasses decode fastest, since each field is a direct store.
  • Each annotation is compiled once and cached for the life of the process, which keeps its classes alive.

NumPy

Arrays and scalars of type bool, int8–int64, uint8–uint64, float16, float32, float64 and datetime64 are serialized directly from memory, without .tolist() or OPT_SERIALIZE_NUMPY:

import numpy as np

yjson.dumps({"matrix": np.array([[1.5, np.nan], [3.0, -np.inf]]), "count": np.int64(4)})
# b'{"matrix":[[1.5,NaN],[3.0,-Infinity]],"count":4}'
  • float16 and float32 values are written with their own shortest round-trip digits, so np.float32(0.1) is written as 0.1.
  • datetime64 values are written as YYYY-MM-DDTHH:MM:SS[.ffffff], and honor the datetime options.
  • Arrays that are not C-contiguous, are zero-dimensional or have another dtype are passed to default, or raise JSONEncodeError without one. Arrays in non-native byte order always raise. These rules match orjson's.

dumps_socket

dumps_socket(obj, /, default=None, classify=None) -> bytes

A second encoder, built for Reflex's Socket.IO messages. It follows the semantics of Python's json module rather than orjson's: integers of any size, NaN and Infinity tokens, and lone surrogates escaped instead of rejected. It writes compact UTF-8 and takes no options. Objects beyond plain JSON, such as datetime, dataclass and UUID instances, go to default unless classify handles them.

classify(type) lets a framework serialize its own types without a Python call per object. yjson calls it once for each type that would otherwise reach default, and caches the answer for the rest of the call:

  • A tuple of attribute names writes the object as {name: getattr(obj, name), ...}.
  • A callable is called with the object, and its result is written. When the callable is str and the object is a naive date, time or datetime, yjson formats the value itself, matching str().
  • None passes the object to default.
import dataclasses

@dataclasses.dataclass
class Row:
    id: int
    name: str

def classify(cls):
    if dataclasses.is_dataclass(cls):
        return tuple(field.name for field in dataclasses.fields(cls))
    return None

yjson.dumps_socket([Row(1, "a"), Row(2, "b")], classify=classify)
# b'[{"id":1,"name":"a"},{"id":2,"name":"b"}]'

Differences from orjson

dumps produces the same bytes as orjson, loads returns the same values, and CI runs orjson 3.12.0's test suite against yjson. The deliberate differences are:

  • NaN, Infinity and -Infinity are written as Python's json module writes them, where orjson writes null. Strict parsers, including yjson.loads, orjson and JavaScript's JSON.parse, reject these tokens; Python's json.loads accepts them.
  • NumPy values are serialized without OPT_SERIALIZE_NUMPY.
  • Wheels are available only for x86-64 Linux with AVX2.
  • loads(type=...) and dumps_socket have no orjson equivalent.

Performance

Serialization

Speedup over orjson 3.12.0 when serializing documents from the simdjson-data corpus (higher is better):

Document 3.11 3.12 3.13 3.14 3.15
twitter.json 1.38× 1.39× 1.37× 1.36× 1.41×
github_events.json 1.44× 1.32× 1.38× 1.34× 1.35×
citm_catalog.json 1.21× 1.26× 1.24× 1.28× 1.26×
canada.json 1.07× 1.12× 1.04× 1.05× 1.09×
All 14 documents (geometric mean) 1.28× 1.30× 1.29× 1.31× 1.31×

Measured with bench/bench_paired.py, which checks that both libraries produce identical output, then times them in 40 alternating pairs per document on one pinned core of an Intel Xeon at 2.80 GHz (AVX2). Of the other libraries measured (msgspec, ujson, python-rapidjson, simplejson and json), orjson was the fastest.

Results depend on the data. yjson can be slower than orjson on some shapes, such as lists of short strings or of mixed True, False and None, and the two perform about the same when most of the time goes to Python default functions. The full results cover every document, per-shape measurements, other libraries and the Reflex benchmarks; CONTRIBUTING.md explains how to measure your own payloads.

Parsing

Parsing the same corpus, loads is 1.20× faster than orjson.loads (geometric mean over the 14 documents on CPython 3.13), from 0.98× on numbers.json to 2.04× on gsoc-2018.json; json.loads is 2–5× slower than either. Measured with bench/bench_loads.py in 20 alternating pairs per document on a shared cloud machine, so differences under about 5% are noise.

Decoding 1,000 records of nine fields with a nested object, measured with bench/bench_typed.py on the same kind of machine (fastest first):

Decoder Result Per call
yjson.loads(type=list[Record]), slots=True slotted dataclasses 0.47 ms
msgspec with a Struct Structs 0.50 ms
yjson.loads(type=list[Record]) dataclasses 0.62 ms
yjson.loads dicts 0.71 ms
msgspec, untyped dicts 0.80 ms
msgspec with list[Record] dataclasses 0.82 ms
orjson.loads dicts 0.85 ms
orjson.loads, then dataclasses built by hand dataclasses 2.72 ms

Across the 14 corpus documents, with a schema inferred from each, slotted dataclasses decode 1.11× faster than msgspec's Structs (geometric mean), from 0.97× on marine_ik.json to 1.34× on twitterescaped.json.

How it works

yjson is a CPython extension module. The serializer is written in Mojo, with a C shim for the Python C API. Instead of calling the C API for each value, its writers read CPython's object layouts directly: list and tuple items, compact integers, float bits, string data and dict key tables. The field offsets are probed from the target interpreter's headers at build time and checked against live objects at import, so an unsupported interpreter fails with an ImportError rather than misreading memory. Floats are formatted with the Żmij shortest round-trip algorithm, integers with an itoap-style writer in 4-wide SIMD batches, and strings are escaped with a 64-byte SIMD scan. Output is written straight into the resulting bytes object.

loads is a separate recursive-descent parser in Mojo that shares no code with the serializer. It scans strings and whitespace 16 bytes at a time, builds str objects straight from the input, and caches object keys, so repeated keys share one str and its hash. Floats are parsed with the Eisel-Lemire algorithm as implemented in fast_float, falling back to CPython's strtod beyond 19 significant digits. With type, each annotation is compiled once into a plan that builds the target objects directly.

Limitations

  • x86-64 Linux only. The extension targets x86-64-v3 and requires AVX2; there is no fallback for older CPUs.
  • CPython 3.11–3.15 with the GIL. Free-threaded builds are not supported.
  • The string scan can read up to 31 bytes past the end of a string, within the same memory page. This is safe, but Valgrind and AddressSanitizer report it.
  • Once the output buffer of a dumps call reaches 1 MB, the result keeps that buffer, as orjson's does, so it can occupy up to twice its length until it is freed. Shrinking the buffer made glibc map fresh pages on every call.
  • On import, yjson points the bundled Mojo runtime at the running interpreter. If MOJO_PYTHON_LIBRARY is already set, it takes precedence and must refer to that interpreter.

Contributing

See CONTRIBUTING.md for building from source, running the tests and benchmarks, and making a release.

Acknowledgements

  • Float formatting and its power-of-ten table are ported from Żmij by Victor Zverovich.
  • The integer writer is ported from itoap.
  • The float parser and its power-of-five table follow fast_float by Daniel Lemire and contributors.
  • The API and the test suite in tests/suite/ come from orjson, whose source also informed the design.

Their licenses are reproduced in THIRD_PARTY_NOTICES.md.

Metadata

Release files for yjson 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yjson 0.3.2
File Size Uploaded Provenance Actions
yjson-0.3.2.tar.gz 209.6 kB Without provenance Details for yjson-0.3.2.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for yjson 0.3.2
File Actions
yjson-0.3.2-cp315-cp315-manylinux_2_35_x86_64.whl CPython 3.15 CPython 3.15 Linux glibc 2.35+ x86-64 Details for yjson-0.3.2-cp315-cp315-manylinux_2_35_x86_64.whl
yjson-0.3.2-cp314-cp314-manylinux_2_35_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.35+ x86-64 Details for yjson-0.3.2-cp314-cp314-manylinux_2_35_x86_64.whl
yjson-0.3.2-cp313-cp313-manylinux_2_35_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.35+ x86-64 Details for yjson-0.3.2-cp313-cp313-manylinux_2_35_x86_64.whl
yjson-0.3.2-cp312-cp312-manylinux_2_35_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.35+ x86-64 Details for yjson-0.3.2-cp312-cp312-manylinux_2_35_x86_64.whl
yjson-0.3.2-cp311-cp311-manylinux_2_35_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.35+ x86-64 Details for yjson-0.3.2-cp311-cp311-manylinux_2_35_x86_64.whl

Total release size: 8.2 MB

Release files / yjson-0.3.2.tar.gz

Download URL yjson-0.3.2.tar.gz
Size 209.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cb7fb157b27b8f3b0048bf8b3a6adbf71b19611d29d05f53e799fd7e1bd3223c
BLAKE2b-256 checksum
How to use checksums
3c120b5ecc7eec3c876c12c9c9eaa9c3a78426dce133b6baecdc1257213dcf8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yjson-0.3.2-cp315-cp315-manylinux_2_35_x86_64.whl

Download URL yjson-0.3.2-cp315-cp315-manylinux_2_35_x86_64.whl
Size 1.6 MB
Tags CPython 3.15 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
f6acdd2de36ff4785c28437f707a09590ebec607f4ecfab2088cc10e51cabc50
BLAKE2b-256 checksum
How to use checksums
9926a8d92d9c4a849311e859d404f7e8bd1cb292a1514ca560c9680ce82b51f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yjson-0.3.2-cp314-cp314-manylinux_2_35_x86_64.whl

Download URL yjson-0.3.2-cp314-cp314-manylinux_2_35_x86_64.whl
Size 1.6 MB
Tags CPython 3.14 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
b2c65eb1d178b5ed4b3adaa6066fa1484b5a22aed29ecd3894a1b72d68f89f1e
BLAKE2b-256 checksum
How to use checksums
6e87c09aefffa4ea85d5da3aa8589fadeb1ace46ded53a46673d303acff17feb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yjson-0.3.2-cp313-cp313-manylinux_2_35_x86_64.whl

Download URL yjson-0.3.2-cp313-cp313-manylinux_2_35_x86_64.whl
Size 1.6 MB
Tags CPython 3.13 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
528f3d3e2372fec578d285e0de68423676ef5b9518fc1e131569e19877c82994
BLAKE2b-256 checksum
How to use checksums
9a47da2181aaaf5f0db916d06be868f42edcf3c01eb6b1a0d3b625a8bf6f3596
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yjson-0.3.2-cp312-cp312-manylinux_2_35_x86_64.whl

Download URL yjson-0.3.2-cp312-cp312-manylinux_2_35_x86_64.whl
Size 1.6 MB
Tags CPython 3.12 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
3508e08d25eb49d78df0a1c620960d7b1a2a1911cab78a1d2cc8cfc1c3aa1162
BLAKE2b-256 checksum
How to use checksums
0ea304f9196c51ed153bbfaf4110e9de0b9b7d8d388119f34a90b0d622f26578
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / yjson-0.3.2-cp311-cp311-manylinux_2_35_x86_64.whl

Download URL yjson-0.3.2-cp311-cp311-manylinux_2_35_x86_64.whl
Size 1.6 MB
Tags CPython 3.11 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
0d5fd0d1c27a06a7e6e215e7a71daba3bb087cca43fa74dc784dddf6d561675a
BLAKE2b-256 checksum
How to use checksums
a9630c5c5998480389fa741e4e053ceffd29ca5d80870fe5483dc5f66b4510c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for yjson 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page