1

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.

2 Answers 2

3

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}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that's a lot easier than I hoped :)
Be careful though, as values like null (which the JSON decoder would interpret as None) will cause it to fail...
0

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.

1 Comment

Thanks to Jon Clements for pointing this to me: chat.stackoverflow.com/transcript/6?m=11075790#11075790.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.