Thursday, September 08, 2005

Gherkin

From LGT, home of NanoThreads comes Gherkin:

A gherkin is a simple pickle. It is a simple serialization algorithm, written in Python, which serializes simple python types (including longs and sequence types (and nested structures)) into a size and speed efficient binary string. It has the same API as the marshal module.

import gherkin
value = [1,2,3,"Four","Five",["Six",7],{8:9,10:11},12.131415,(16,[17,18])]
serial = gherkin.dumps(value)
new_value = gherkin.loads(serial)
assert value == new_value

It is used to decode serialized strings from unknown / untrusted sources (network connections) in a safe manner, which pickle and marshal are unable to do. <plug>It is faster than JSON and Pickle, and can often beat marshal in the size and speed competition. It can serialize all simple Python types, which JSON cannot do, and which bencode cannot do.</plug>. It also comes with a nice set of unit tests to prove it's safety.