Skip to main content

data serialization using typed netstrings

Project description

This is a data serialization library. It’s a lot like JSON but it uses a new syntax called “typed netstrings” that Zed has proposed for use in the Mongrel2 webserver. It’s designed to be simpler and easier to implement than JSON, with a happy consequence of also being faster in many cases.

An ordinary netstring is a blob of data prefixed with its length and postfixed with a sanity-checking comma. The string “hello world” encodes like this:

11:hello world,

Typed netstrings add other datatypes by replacing the comma with a type tag. Here’s the integer 12345 encoded as a tnetstring:

5:12345#

And here’s the list [12345,True,0] which mixes integers and bools:

19:5:12345#4:true!1:0#]

Simple enough? This module gives you the following functions:

dump:

dump an object as a tnetstring to a file

dumps:

dump an object as a tnetstring to a string

load:

load a tnetstring-encoded object from a file

loads:

load a tnetstring-encoded object from a string

pop:

pop a tnetstring-encoded object from the front of a string

Note that since parsing a tnetstring requires reading all the data into memory at once, there’s no efficiency gain from using the file-based versions of these functions. They’re only here so you can use load() to read precisely one item from a file or socket without consuming any extra data.

By default tnetstrings work only with byte strings, not unicode. If you want unicode strings then pass an optional encoding to the various functions, like so:

>>> print repr(tnetstring.loads("2:\xce\xb1,"))
'\xce\xb1'
>>>
>>> print repr(tnetstring.loads("2:\xce\xb1,","utf8"))
u'\u03b1'

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

tnetstring-0.2.0.tar.gz (15.5 kB view details)

Uploaded Source

File details

Details for the file tnetstring-0.2.0.tar.gz.

File metadata

  • Download URL: tnetstring-0.2.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tnetstring-0.2.0.tar.gz
Algorithm Hash digest
SHA256 13315c4c7d7b373833971770366786d47c2ce6969b487b0de132e44efff6f5d0
MD5 7cc9de42ad5a969630be3cd859282bef
BLAKE2b-256 715198a0b5689c0928dea1c75d5662bc399e45d3e1725764496c1b1e850eee0d

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