'Convert Pickle Object into JS-readable Code?
I'm pretty new to the Pickle Library and JS. I am writing code in JS to interact with a Python server; when I make a POST to the Python server, I am returned a pickled float. I want to convert the float into a JS-readable object.
The server returns something like pickle.dumps(3.14159,0)
. When I print this value in Python, I get b'F3.14159\n.'
However, the library I'm working with, JPickle, cannot interpret the 'b' in the return (or at least, that's what I'm guessing...when I try to unpickle my response using console.log(jpickle.loads(response))
, I get a "unhandled opcode" error). Separate from the POST request, jpickle.loads('F3.14159\n.')
gives me 3.14159, but jpickle.loads(b'F3.14159\n.')
fails to compile. I'm not sure how to change the response I get from the POST because it is unreadable in JS, so I'm unsure how to go about solving this problem.
I'm open to any solutions that don't involve changing the server-side code (I don't have control over it), including using another library. I've tried JsonPickle with no luck.
Solution 1:[1]
Use just JSON, no pickle library required; Python comes with the json
module and browser JS can, out of the box, parse json into values.
Serialize your data via something like json.dump
and use JSON.parse
in the browser to deserialize it.
Solution 2:[2]
A little late, but the answers in attached might help:: the code allows Javascript on a browser to traverse and decode compressed pickled objects coming from Python via Ajax.
Make sure that the mime type is allowing you to pass binary numbers.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ThisIsNoZaku |
Solution 2 | Tunneller |