Reputation: 12459
I want to be able to decode a format that looks like JSON, but can handle hexadecimal content. Let's call it JSHON. Example content:
{
"nine": 9,
"ten": 0xA,
"eleven": 11
}
Is it easy to coax the stdlib json module to provide such an ability, or should I use something else.
Upvotes: 1
Views: 109
Reputation: 12459
According to its documentation, demjson supports this (in its non-strict mode). It has no support for Python 3 yet, but it will do for now.
Upvotes: 0
Reputation: 26397
Simple fix would be to use ast.literal_eval
,
>>> data = '''
... {
... "nine": 9,
... "ten": 0xA,
... "eleven": 11
... }
... '''
>>> import ast
>>> ast.literal_eval(data)
{'eleven': 11, 'nine': 9, 'ten': 10}
Upvotes: 3