1- jsonpickleJS
2- ============
1+ # jsonpickleJS
32
43Javascript reinterpretation of Python jsonpickle to allow reading and (to a lesser extent)
54writing JSON objects
65
7- Copyright (c) 2014 Michael Scott Cuthbert and cuthbertLab .
6+ Copyright © 2014-25 Michael Scott Asato Cuthbert .
87Released under the BSD (3-clause) license. See LICENSE.
98
10- Python to Javascript and Back
11- ==============================
9+ # Pre-class-based system (Closing down)
10+
11+ Note for 2025+: This system depends on legacy style functions that behave like objects.
12+ To use with modern Javascript you will need to assign all classes to globalThis (window, global).
13+ For this reason, v1.2 will be the last version of jsonpickleJS. Using custom decoders in JSON.parse
14+ is probably the best solution going forward in the modern world.
15+
16+ # Python to Javascript and Back
1217Python has a remarkable number of ways (for a language that believes there's only one way to do it)
1318to transfer data between itself and Javascript, most obviously with the
1419`` json.dump() `` /`` json.dumps() `` calls, which work well on specifically created data, especially
@@ -31,22 +36,21 @@ namespace, such as ``window``, in the Javascript. For instance, if you have a c
3136`` window.myobject.Thing `` in Javascript. The object, and any subobjects, will be created as closely
3237as possible in Javascript.
3338
34- The reverse is also possible, with some caveats. Since Javascript doesn 't (until ECMAScript 6) have
35- the concept of named classes, each object will need to have a marker somewhere on it saying what
39+ The reverse is also possible, with some caveats. Since Javascript didn 't (until ECMAScript 6) have
40+ the concept of named classes, each object needed to have a marker somewhere on it saying what
3641Python object it should convert back to. The marker is
3742`` o[jsonpickle.tags.PY_CLASS] = 'fully.qualified.ClassName' `` .
3843It may be possible in the future to use `` instanceof ``
3944through the entire Global namespace to figure out what something is, but that seems rather dangerous
4045and inefficient (A project for later).
4146
42- Limitations
43- ===========
47+ # Limitations
4448Remember that Javascript does not have tuples, so all tuple objects are changed to lists.
49+
4550Namedtuples behave the same way, I believe. Dicts and Objects are identical in Javascript (both
4651a blessing and a curse).
4752
48- Security
49- ========
53+ # Security
5054Pickle, jsonpickle, and jsonpickleJS all raise important security considerations you must be
5155aware of. You will be loading data directly into Python or Javascript with no checks on what the
5256data contains. Only load data you have personally produced if you want to be safe. In Javascript,
@@ -60,15 +64,68 @@ data, but anyone with JS programming experience can inject other data into your
6064Be safe: be cautious going from Python to Javascript and NEVER accept Javascript-produced
6165jsonpickle data from the net into your Python program in any sort of open environment.
6266
63- Usage
64- =====
65- See the source code of: testUnpickle.html to see how to use it. JsonpickleJS follows the AMD
66- moduleloader standard, so set the `` src="" `` attribute in the `` <script> `` to an AMD loader
67- such as `` //cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js ``
68- (or the included local version in `` jsonpickleJS/ext/require/require.js `` ) and
69- the `` data-main `` attribute to `` jsonpickleJS/main `` (no `` .js `` ). Then call
70- `` var o = jsonpickle.decode(jsonStr) `` to get the Python object back as a JS object named `` o `` .
71-
72- See the cuthbertLab/music21 and cuthbertLab/music21j projects and especially the `` .show('vexflow') ``
73- component for an example of how jsonpickleJS can be extremely useful for projects that have
74- parallel data structures between Python and Javascript.
67+ # Usage
68+
69+ You can use ` jsonpickleJS ` in modern JavaScript environments with either
70+ ES Modules or traditional ` <script> ` tags.
71+
72+ ---
73+
74+ ### ES Module (Modern usage)
75+
76+ ``` js
77+ // be sure jsonpickle is in your package.json and installed.
78+
79+ // # in python:
80+ // import jsonpickle
81+ // jsonStr = jsonpickle.encode(my_object)
82+
83+ import jsonpickle from ' jsonpickle' ;
84+
85+ const obj = jsonpickle .decode (jsonStr);
86+ // `obj` is now a JavaScript version of the original Python object
87+ // with proper classes.
88+ ```
89+
90+ ---
91+
92+ ### ` <script> ` Tag (Legacy/global usage)
93+
94+ ``` html
95+ <script src =" build/jsonpickle.min.js" ></script >
96+ <script >
97+ const obj = jsonpickle .decode (jsonStr);
98+ console .log (obj);
99+ </script >
100+ ```
101+
102+ This works without any build system. The ` jsonpickle ` global will be available automatically.
103+
104+
105+ ### Example
106+
107+ You can find a working example in [ ` testUnpickle.html ` ] ( ./testUnpickle.html ) .
108+
109+
110+ ### Background
111+
112+ ` jsonpickleJS ` allows you to ** decode Python-style JSON strings** into rich JavaScript objects.
113+ It’s especially useful for projects that mirror data structures across Python and JavaScript.
114+
115+ One real-world use case:
116+ Older versions of [ ` music21 ` ] ( https://github.com/cuthbertLab/music21 ) and
117+ [ ` music21j ` ] ( https://github.com/cuthbertLab/music21j ) use ` jsonpickleJS ` to render complex
118+ musical objects in the browser — such as using ` .show('vexflow') ` in music21 to visualize
119+ music sent from Python into JavaScript.
120+
121+ # Building
122+ Run once:
123+ ```
124+ % npm install
125+ ```
126+
127+ Then:
128+
129+ ```
130+ % npm run build
131+ ```
0 commit comments