Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not being able to use jsonpickleJS in Node.js #6

Open
laike9m opened this issue Aug 2, 2020 · 1 comment
Open

Not being able to use jsonpickleJS in Node.js #6

laike9m opened this issue Aug 2, 2020 · 1 comment

Comments

@laike9m
Copy link

laike9m commented Aug 2, 2020

Sorry for opening several issues. I really want to use jsonpickleJS in Node.js, since I'm already using jsonpickle in Python and got the encoded JSON string, and want to convert them to Js objects

The blockers are:

  • window is not defined in Node.js

  • Arbitrary Python objects can be encoded, and the prerequisite of

    you'll need to have a Prototype constructor function called window.myobject.Thing in Javascript.

    is hard to met

Do you have any suggestions?

@mscuthbert
Copy link
Member

Here is a quick patch that I will get into the main system at some point, but if you could put it in and test it, this would be a big help. The only reference to window is in unpickler.js. The revised code should go:

export function loadclass(module_and_name) {
    const main_check = '__main__.';
    if (module_and_name.indexOf(main_check) === 0) {
        module_and_name = module_and_name.slice(main_check.length);
    }
    let parent;
    if (typeof globalThis !== 'undefined') {
	parent = globalThis;
    } else if (typeof window !== 'undefined') {
	parent = window;
    } else if (typeof global !== 'undefined') {
	parent = global;
    }
    const module_class_split = module_and_name.split('.');

from the const module_class_split onward, it should be the same.

If you have a version of node with globalThis or global it should work. Just make sure that the Prototype constructor function points to global.myobject.Thing instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants