fork download
  1. import json
  2.  
  3. try: long = long
  4. except NameError:
  5. long = int
  6.  
  7. class hexint(int):
  8. def __str__(self):
  9. return hex(self)
  10.  
  11. class hexlong(long):
  12. def __str__(self):
  13. return hex(self)
  14.  
  15. class Hexifyint(object):
  16. def __call__(self, jsonobj):
  17. h = getattr(self, 'hexify_'+jsonobj.__class__.__name__, lambda x, y: x)
  18. return h(jsonobj, jsonobj.__class__)
  19.  
  20. def hexify_list(self, lst, cls):
  21. return cls(self(item) for item in lst)
  22. hexify_tuple = hexify_list
  23. def hexify_dict(self, dct, cls):
  24. return cls((self(k), self(v)) for k, v in dct.items())
  25. hexify_OrderedDict = hexify_dict
  26. def hexify_int(self, i, cls):
  27. return hexint(i)
  28. def hexify_long(self, i, cls):
  29. return hexlong(i)
  30.  
  31. data = {'test': 33, 'this': 99, 'something bigger':[1,2,3, {'a':44}]}
  32. print(json.dumps(Hexifyint()(data)))
Success #stdin #stdout 0.04s 6728KB
stdin
Standard input is empty
stdout
{"test": 0x21, "this": 0x63, "something bigger": [0x1, 0x2, 0x3, {"a": 0x2c}]}