Skip to content

Repository files navigation

Pickle Parser

NPM VersionUnit TestDemoLicense

A pure TypeScript parser for the Python pickle format. Supports protocol 0 through 5.

Features

  • Pickle protocol version 0~5, all opcodes supported
  • Pure TypeScript, zero dependencies
  • Works in Node.js and browsers
  • Customizable via ParserOptions (name resolution, dict/set types, out-of-band buffers)
  • CLI tool to convert pickle files to JSON

Demo

Online Pickle to JSON Converter

Installation

npm install pickleparser

Usage

Node.js

importfsfrom'node:fs/promises';import{Parser}from'pickleparser';constbuffer=awaitfs.readFile('data.pkl');constobj=newParser().parse(buffer);console.log(obj);

Browser

<scriptsrc="https://unpkg.com/pickleparser/dist/index.js"></script><script>constbuffer=newUint8Array(arrayBuffer);constobj=newpickleparser.Parser().parse(buffer);</script>

CLI

npx pickleparser file.pkl file.json
# or
npm i pickleparser -g
pickletojson file.pkl file.json

API

new Parser(options?)

import{Parser}from'pickleparser';constparser=newParser({unpicklingTypeOfDictionary: 'Map',unpicklingTypeOfSet: 'Set',});constresult=parser.parse<MyType>(buffer);

ParserOptions

OptionTypeDefaultDescription
unpicklingTypeOfDictionary'object' | 'Map''object'Python dict maps to JS plain object or Map
unpicklingTypeOfSet'array' | 'Set''array'Python set maps to JS array or Set
nameResolverNameResolverbuilt-inResolve Python classes/functions by module and name
persistentResolverPersistentResolverthrowsResolve persistent object references
extensionResolverExtensionResolverthrowsResolve extension registry codes
buffersIterator<any>undefinedOut-of-band buffers for protocol 5

NameResolver

Register custom constructors to control how Python classes are instantiated:

import{Parser,NameRegistry}from'pickleparser';classMyClass{x: number=0;y: number=0;}constregistry=newNameRegistry().register('mymodule','MyClass',MyClass);constobj=newParser({nameResolver: registry}).parse<MyClass>(buffer);

Type Mapping

PythonJavaScriptNotes
dict{} or Mapcontrolled by unpicklingTypeOfDictionary
listArray
tupleArray
set, frozensetArray or Setcontrolled by unpicklingTypeOfSet
strstring
bytes, bytearrayBuffer (Node.js)
intnumber
int (> 2^53)number with precision loss, or BigInt via LONG4
floatnumberincluding inf, -inf, nan
boolboolean
Nonenull

Supported Protocols

ProtocolPython VersionPEP
0all
1all
22.3+PEP 307
33.0+
43.4+PEP 3154
53.8+PEP 574

For opcode-level details, see Supported Opcodes.

License

MIT

About

A pure Javascript implemented parser for Python pickle format

Topics

Resources

Stars

24 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages