1

I need to serialize a tuple that contains a raw python datatype or in other words a built in class eg. int/str. But the json library throws an error like TypeError: Object of type type is not JSON serializable

Full traceback:

Traceback (most recent call last):
  File "C:\Users\ns877v\git\analytics-kronos-worker\useful_snippets\2.py", line 2, in <module>
    json.dumps(int)
  File "C:\Users\ns877v\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:\Users\ns877v\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Users\ns877v\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Users\ns877v\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type type is not JSON serializable
[Finished in 0.4s]

Run this to replicate:

import json
json.dumps(int)
  • Does this answer your question? [How to make a class JSON serializable](https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable) – Brian61354270 Apr 10 '20 at 20:07
  • 1
    I don't agree with the "question already has answer". If the user try to use type as JSON value then it's a new question. Btw this is not possible according to [the section 3 of RFC7159 ](https://tools.ietf.org/html/rfc7159#section-3). However if the user want to automatically convert the `type` in string. It might ask a new answer since it's not like serializing a Python object to JSON. Introspection is needed here. – Raphael Medaer Apr 10 '20 at 20:14
  • @RaphaelMedaer that's probably answers my question - but is there any workaround to serialize native python data classes of meta class `type` eg- int, str – Nishith Shetty Apr 10 '20 at 21:05
  • @martineau fyi, i don't think that question answers mine - this question is about serializing a python built in class - can you re-open my question so that people are able post possible answers? – Nishith Shetty Apr 10 '20 at 21:06
  • 1
    As soon the question is open again, I answer your question with pleasure. You could also precise a little bit your question with your "real" needs. I have to admit that it was not really clear. ;-) – Raphael Medaer Apr 10 '20 at 21:12
  • Nishith Shetty: OK, but please [edit] your question and be more specific about what you're asking as you said in that comment. – martineau Apr 10 '20 at 22:10

1 Answers1

3

There is no way to serialize a JSON or Python type as JSON value. As described in RFC7159 Section 3 the only available values are:

false / null / true / object / array / number / string

However you could serialize a Python type as a JSON string. For instance, a Python int would become JSON string value "int".

Since Python keyword int is object of type type. You can use __name__ to get its string name. For instance: print(int.__name__).

To automatically encode it, I let you check this answer which use a custom JSONEncoder.

Community
  • 1
  • 1
Raphael Medaer
  • 2,528
  • 12
  • 18
  • yeah I ended up doing the `int.__name__` thing too and then use `eval()` to deserialize - wished json library was able to handle that itself Thank you for your repsonse, Raphael! – Nishith Shetty Apr 10 '20 at 22:35
  • You're welcome. Don't forget to accept/vote the answer to close the question. – Raphael Medaer Apr 10 '20 at 22:37
  • @NishithShetty I read (again) your previous comment. Pay attention when you use `eval()`. It sounds dangerous to me! Btw, I would appreciate that you edit a bit your question for something more clear. And if answer is OK for you, please accept it! ;-) – Raphael Medaer Apr 11 '20 at 14:15