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

Handling JSON Module Compatibility in Transcrypt #868

Open
fdsgdshn opened this issue Mar 27, 2024 · 2 comments
Open

Handling JSON Module Compatibility in Transcrypt #868

fdsgdshn opened this issue Mar 27, 2024 · 2 comments

Comments

@fdsgdshn
Copy link

Hi,

I've recently started working with Transcrypt, and I'd like to transpile my Python modules to JavaScript using this tool. However, I have some concerns that I'd like to address. I would greatly appreciate your assistance.

Firstly, as far as I've observed, it seems that the json module is not supported by Transcrypt. One solution that comes to mind is creating my own json module and adding it to the source modules, but this appears to be quite laborious. My Python code contains a json file, but since the open command is also not supported by Transcrypt, I've assigned the content of this file directly to a data_json variable.

However, I'm unsure about the approach I should take. For example, how can I convert it to a dictionary data type and access its key-value pairs? I've noticed that JSON.parse command is supported by Transcrypt, but it's not a valid command in Python. I want both my Python and JavaScript code to function correctly. What should I do in this regard? Could you provide some guidance?

@JennaSys
Copy link
Collaborator

You are correct in that the Python json library has not been ported to Transcrypt. However, for basic encoding and decoding, the built-in JavaScript JSON object can be used instead. So json.dumps() gets replaced by JSON.stringify() and json.loads() becomes JSON.parse()

I'll also usually add a stub class to my code that keeps Python linters from complaining when using the JavaScript version, similar to this:

# __pragma__ ('skip')
class JSON:
    parse = None
    stringify = None
# __pragma__ ('noskip')

The skip/noskip compiler directives tells the Transcrypt compiler to ignore the stub code since it is not needed once it gets converted to JavaScript.

To treat the json object as a Python dictionary, I pass it into the python dict() constructor, which adds the Python dictionary methods to the JavaScript object.

If you need your code to run both as Python and as JavaScript, it gets a little bit trickier. You can use the skip/noskip and ecom/noecom directives to control individual parts of code execution in the different environments when necessary. Another possibility in this case, is that you can try mapping the JavaScript JSON methods to the corresponding Python json methods similar to that of the stub code above:

# __pragma__ ('skip')
import json

class JSON:
    @staticmethod
    def parse(json_string):
        return json.loads(json_string)
    
    @staticmethod
    def stringify(json_data):
        return json.dumps(json_data)

# __pragma__ ('noskip')

With this, you can use the JavaScript syntax in your Python code. If running in Python, it will use the mapped methods, and when running in JavaScript it will use the built-in JavaScript methods. (Note: I haven't actually used this particular mapping myself, but I think it should work)

@fdsgdshn
Copy link
Author

fdsgdshn commented Apr 2, 2024

Thank you for your help and support, this information has been very beneficial.

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