-
Notifications
You must be signed in to change notification settings - Fork 216
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
Comments
You are correct in that the Python 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 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) |
Thank you for your help and support, this information has been very beneficial. |
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?
The text was updated successfully, but these errors were encountered: