Reputation: 7951
Can someone suggest a way to serialise a Python dictionary to JavaScript source code describing an equivalent object?
Note that this is different from JSON serialisation. In particular, the following example is important to me:
x = { True: 1, False: 0 }
Serialising this with json.dumps(x)
results in:
{ "true": 1, "false": 0 }
What I want is:
{ true: 1, false: 0 }
This is a valid object declaration in JavaScript but is not valid JSON. Similar problems occur when using any non-string type as a dictionary key.
Upvotes: 1
Views: 104
Reputation: 896
In javascript object keys can only be strings or Symbols
You can access the object properties with true/false because javascript convert them to strings, you can see that they are actually string with:
var x = { true: 1, false: 0 }
console.log(Object.keys(x)) // ["true", "false"]
Upvotes: 1