Skip to main content

Encode existing JSON strings within new JSON documents efficiently...ish

Project description

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).

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

lazyjson-0.0.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

lazyjson-0.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file lazyjson-0.0.1.tar.gz.

File metadata

  • Download URL: lazyjson-0.0.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for lazyjson-0.0.1.tar.gz
Algorithm Hash digest
SHA256 19c55355e0c8381c3d80dfba13d8ab9f2991f59540767f9d9d21a81f163d56f2
MD5 9fe461913d3aa3b4af4be75b22fa521f
BLAKE2b-256 406a2645ca845d960dec41b3e717b2acfe38af292c157eee79f702ba89f6ea10

See more details on using hashes here.

File details

Details for the file lazyjson-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: lazyjson-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for lazyjson-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2a88322e419569eea1a6dc67db868b91554d174804c1a1f606103cc6ead4e3d8
MD5 592e5bf5ca3adc2c1f1646eebd385f12
BLAKE2b-256 b071127712328531baccf7671462a72ca6fedc2f8cd9198867a6fa38d73ebc02

See more details on using hashes here.

Supported by

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