Skip to main content

tests python

Encode existing JSON strings within new JSON documents efficiently…ish

Sometimes you have some existing JSON snippets which you would like to include in a new JSON document or api response. This can be achieved cleanly but inefficiently by decoding the JSON first.

st = os.stat('tests/my.json')
meta = {
    "size": st.st_size,
    "mtime": st.st_mtime,
    "content": json.load(open('tests/my.json'))
}
return json.dumps(meta)

The goal of this library is to provide an API which allows you to avoid ever decoding the snippet that’s being included here.

JSONPassthroughEncoder

This is a subclass of the standard lib’s json.JSONEncoder and performs the magic enabling SafeJSON to work.

WARNING: Blindly wrapping untrusted JSON strings could result in JSON injection attacks. This library is not intended for everyday uses, but rather for complex proxy/file serving use cases where upstream is data you can trust. Please be careful, and only use when you’re certain this is the correct tool for the job, and you trust any data you’re embedding.

WARNING: This class comes with massive performance penalties for encoding complex JSON structures from primitives. The way in which this class is forced to work by the standard lib’s json API is makes normal encoding inneficient, but the concept is otherwise simple and could be added to an existing C implementation trivially with no performance penalty.

If the performance of this module is preventing you from using it but you have a valid use case please get in contact with me to collaborate on a C implementation.

SafeJSON

A wrapper for json-encoded strings which allows them to be output within a JSON document by the JSONPassthroughEncoder without decoding first.

This is a subclass of LazyJSON.

st = os.stat('my.json')
doc = {
    "size": st.st_size,
    "mtime": st.st_mtime,
    "content": SafeJSON(open('my.json').read()),
}
return JSONPassthroughEncoder().encode(doc)

SafeJSONFile

Like SafeJSON but for file-like objects instead of strings. This can be used for streaming extremely large embedded json documents without ever loading them fully into memory.

st = os.stat('my.json')
doc = {
    "size": st.st_size,
    "mtime": st.st_mtime,
    "content": SafeJSONFile(open('my.json'), chunk_size=5000),
}
for t in JSONPassthroughEncoder().iterencode(doc):
    socket.write(t)

LazyJSON

Like SafeJSON but will not be passed-through by JSONPassthroughEncoder classes.

This class can be used for untrusted JSON data for passing around your application and retaining the fact it’s JSON-encoded, if you might only want to decode it sometimes.

Use the my_lazy_json.parse_json() method to parse the JSON.

After validation (usually of the parsed data) you can construct a SafeJSON object from this object to avoid re-encoding SafeJSON(my_lazy_json).

Metadata

Release files for lazyjson 0.0.1

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

Source distribution (sdist)

Source distribution for lazyjson 0.0.1
File Size Uploaded
lazyjson-0.0.1.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lazyjson 0.0.1
File Interpreter ABI Platform
lazyjson-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.8 kB

Release files / lazyjson-0.0.1.tar.gz

Download URL lazyjson-0.0.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
19c55355e0c8381c3d80dfba13d8ab9f2991f59540767f9d9d21a81f163d56f2
BLAKE2b-256 checksum
How to use checksums
406a2645ca845d960dec41b3e717b2acfe38af292c157eee79f702ba89f6ea10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release files / lazyjson-0.0.1-py3-none-any.whl

Download URL lazyjson-0.0.1-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a88322e419569eea1a6dc67db868b91554d174804c1a1f606103cc6ead4e3d8
BLAKE2b-256 checksum
How to use checksums
b071127712328531baccf7671462a72ca6fedc2f8cd9198867a6fa38d73ebc02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

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