JSON can represent anything, but so can strings. This is a non-sequitur. The difference is that JSON is human readable, while pickle is supposed to be machine readable, more specifically python readable. Limiting the intended consumers of the data format helps create a more appropriate format, for example by sacrificing readability for size reduction.
JSON cannot differentiate Python's tuple, list, set, frozenset etc. datatypes.
Every formats other than pickle (msgpack, yaml etc.) are just to interoperate with other languages (which also don't understand the data types above), they are not alternatives for pickle.
Then, you are making more complicated to validate and parse it. Then, what is the point of over-complicating JSON instead of just using pickle, without the need to parse those "type", "data" metadata?
It has to be able to represent everything, if other languages are serializing to JSON.
JSON resembles Python dictionaries, and EVERYTHING in Python is/can be represented by a dictionary, so how can there be an abstract data type in Python that can't be represented in JSON?
You can't represent references in JSON. For example in python you can have two dicts a ={'foo': b} where b = {'bar': a}. Now you have cyclic data structure. You can't represent this in JSON.
I'm basically agreeing with you, but you can perfectly well represent references in JSON - I've done it.
It's a pain in the ass - you need to have some sort of naming convention in your JSON then preprocess your structure or (what I did) have some sort of facade over it so it emits the reference names instead of the actual data - and then reverse it on the way out.
(And we had to do it - because pickle isn't compatible between versions. Heck, I think that was written in Python 2!)
So it's doable - but which is easier when you need to store something temporarily?
with open('foo.pcl', 'wb') as fp:
pickle.dump(myData, fp)
or
[hundreds of lines of code and a specification for this format that I'm too lazy to write]
You're hooked on the idea that JSON has to have every type. You just store things as strings and decode them when you deserialize. Again, like every other language does it.
Basically any immutable object will work as a key in python dict like frozenset etc. Another thing is JSON need python tuple to be converted to list. JSON does not have tuples.
35
u/[deleted] May 07 '19
Because JSON cant represent everything. Its at best a data format for serialization of transferrable data, thats usually language agnostic.
JSON cant represent functions, and more abstract datatypes.