0

Ujson (and probably other json libraries) for python will reject some objects. A good example is Ulid; you can't put a Ulid into a dict and then expect uJson to correctly dump it. However if I make an empty class, uJson will happily encode it. I'd like to write a unit test that shows that we correctly handle objects that will cause ujson to choke, but I'd rather not import a library just for that purpose. What's a minimal python object that can't be ujson'd?

Eamonn M.R.
  • 1,917
  • 2
  • 17
  • 23
  • Hm. `ujson` seems to try very hard to produce *something* for many values, whether or not that makes sense. `...` and `NotImplemented` both are serialized as the empty object `{}`; a socket produces an object that describes it somewhat, `ujson.dumps(socket.socket()) == '{"family":2,"proto":0,"timeout":null,"type":1}'`, etc. – chepner Mar 23 '20 at 15:55
  • I'm not sure there *is* such a value; if all else fails, it seems an empty `{}` is returned. – chepner Mar 23 '20 at 16:00
  • As this question points out: https://stackoverflow.com/questions/36588126/uuid-is-not-json-serializable there are some classes that cannot be serialized. – Eamonn M.R. Mar 23 '20 at 16:16
  • That question doesn't mention `ujson` at all. Ideally, JSON encoding should not be lossy, or at least minimally lossy: for instance, usually you don't care *too* much if both tuples and lists are encoded as JSON arrays. But `ujson` seems to be very liberal in how it encodes seeming unencodable values: it just gives you an empty object, and in many cases the object you get back can't be decoded into anything like the Python value that produced it. – chepner Mar 23 '20 at 16:26
  • Consider the output of `ujson.dumps(int)`, the encoding of the type `int`, not an instance of `int`: `'{"denominator":{},"imag":{},"numerator":{},"real":{}}'`. There's no particular reason why a type should be JSON-encodable, but there it is. – chepner Mar 23 '20 at 16:29
  • I update the question's title to be ujson specific, though other json libraries appear to have similar issues. I'm not trying to fix the problem, I'm trying to make write a test that exercises the problem without having to import Ulid or the like. – Eamonn M.R. Mar 23 '20 at 18:23
  • My point is, `uJSON` appears to go out of its way to *avoid* being unable to encode an object. Can you post an example of `ujson.dumps` failing to encode an object, even if you do import your ulid module? – chepner Mar 23 '20 at 18:27
  • Using ulid-py 0.0.12: ```ujson.dumps(ulid.new())``` will raise `OverflowError: Unterminated UTF-8 sequence when encoding string` – Eamonn M.R. Mar 23 '20 at 19:15

0 Answers0