| 1 |
jsonpickleJS |
| 2 |
============ |
| 3 |
|
| 4 |
Javascript reinterpretation of Python jsonpickle to allow reading and (to a lesser extent) |
| 5 |
writing JSON objects |
| 6 |
|
| 7 |
Copyright (c) 2014 Michael Scott Cuthbert and cuthbertLab. |
| 8 |
Released under the BSD (3-clause) license. See LICENSE. |
| 9 |
|
| 10 |
Python to Javascript and Back |
| 11 |
============================== |
| 12 |
Python has a remarkable number of ways (for a language that believes there's only one way to do it) |
| 13 |
to transfer data between itself and Javascript, most obviously with the |
| 14 |
``json.dump()``/``json.dumps()`` calls, which work well on specifically created data, especially |
| 15 |
of primitive objects. It also has a good number of ways to store the contents of an object so that |
| 16 |
it could be reloaded later into the same Python interpreter/session. The most well known of these |
| 17 |
is the ``pickle`` module, which stores the Python data as binary data. |
| 18 |
|
| 19 |
The external ``jsonpickle`` module, which you'll need to have installed to make this module have |
| 20 |
any sense, combines the features of ``json`` and ``pickle``, storing Python data in a JSON |
| 21 |
(Javascript Object Notation) string, via ``jsonpickle.encode()`` that can be parsed back by |
| 22 |
``jsonpickle.decode()`` to get nearly all data types restored to their previous state. (Highly |
| 23 |
recommended: jsonpickle v1.7 or higher. Older versions will not serialize recursive |
| 24 |
data structures as necessary for a lot of applications.) |
| 25 |
|
| 26 |
Since JSON is one of the most easily parsed formats in Javascript (the name should be a giveaway), |
| 27 |
this project, jsonpickleJS, exists to parse the output of Python's jsonpickle into objects in |
| 28 |
Javascript. The constructors of the objects need to have the same names and exist in the global |
| 29 |
namespace, such as ``window``, in the Javascript. For instance, if you have a class called |
| 30 |
``myobject.Thing`` in Python, you'll need to have a Prototype constructor function called |
| 31 |
``window.myobject.Thing`` in Javascript. The object, and any subobjects, will be created as closely |
| 32 |
as possible in Javascript. |
| 33 |
|
| 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 |
| 36 |
Python object it should convert back to. The marker is |
| 37 |
``o[jsonpickle.tags.PY_CLASS] = 'fully.qualified.ClassName'``. |
| 38 |
It may be possible in the future to use ``instanceof`` |
| 39 |
through the entire Global namespace to figure out what something is, but that seems rather dangerous |
| 40 |
and inefficient (A project for later). |
| 41 |
|
| 42 |
Limitations |
| 43 |
=========== |
| 44 |
Remember that Javascript does not have tuples, so all tuple objects are changed to lists. |
| 45 |
Namedtuples behave the same way, I believe. Dicts and Objects are identical in Javascript (both |
| 46 |
a blessing and a curse). |
| 47 |
|
| 48 |
Security |
| 49 |
======== |
| 50 |
Pickle, jsonpickle, and jsonpickleJS all raise important security considerations you must be |
| 51 |
aware of. You will be loading data directly into Python or Javascript with no checks on what the |
| 52 |
data contains. Only load data you have personally produced if you want to be safe. In Javascript, |
| 53 |
malicious data may compromise your browser, browser history, functioning of the current web page, |
| 54 |
etc. That's pretty bad, but nothing compared to what can happen if you load jsonpickle data |
| 55 |
from Javascript into Python: a maliciously written set of jsonpickle data may be able to send the |
| 56 |
contents of any file back to the net or manipulate or delete the hard drive of the server. It may |
| 57 |
be that the parsed Python object have to be called in some way, but it may even be possible to have |
| 58 |
malicious code executed just on parsing; assume the worst. Your .html/.js may only produce safe |
| 59 |
data, but anyone with JS programming experience can inject other data into your server scripts. |
| 60 |
Be safe: be cautious going from Python to Javascript and NEVER accept Javascript-produced |
| 61 |
jsonpickle data from the net into your Python program in any sort of open environment. |
| 62 |
|
| 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. |