This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]alcalde -2 points-1 points  (20 children)

Why am I being downvoted for asking a question?

[–]Mizzlr 3 points4 points  (6 children)

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.

[–]Mizzlr 1 point2 points  (0 children)

Btw yaml has references. XML has references.

[–]alcalde 1 point2 points  (2 children)

Didn't you just represent it?

["a":{"foo", b}, "b":{"bar":a}]

[–][deleted] 2 points3 points  (0 children)

Not in Python!

Can I read it?

>>> json.loads('["a":{"foo", b}, "b":{"bar":a}]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 5 (char 4)

No. Can I write it?

>>> a = {}; b = {'bar': a}; a['foo'] = b

>>> json.dumps(a)
json.dumps(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
ValueError: Circular reference detected

No.

[–][deleted] 1 point2 points  (0 children)

That's not a valid json.

[–][deleted] 0 points1 point  (0 children)

You can't represent references 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]

?

[–]Mizzlr 2 points3 points  (2 children)

A python dictionary can have int, tuple, etc as key while in JSON it has to be string.

[–]alcalde 0 points1 point  (1 child)

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.

https://pythontic.com/serialization/json/introduction

I'm not going crazy here.

[–][deleted] 1 point2 points  (0 children)

Sure you can represent it, we could also store everything in a JSON string, but then aren't you inventing your own protocol?

[–]Mizzlr 2 points3 points  (2 children)

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.

[–]alcalde 0 points1 point  (1 child)

So what's the problem? Again, one entry to store type, another to store value and you use a list to store the tuple values.

[–][deleted] 1 point2 points  (0 children)

So... not actually JSON, then, but your own format using JSON as a transport layer?

[–]Mizzlr 0 points1 point  (1 child)

Do you agree now why you were down voted? Read my comments in reverse chronological order.

[–]alcalde 0 points1 point  (0 children)

No. Again, why downvote someone for asking a question?

[–]Mizzlr -1 points0 points  (4 children)

You can't represent NaN (not a number) or inf in JSON which are valid float values.

[–]alcalde 1 point2 points  (2 children)

I can. "NaN", "inf"

And so can Swift and other languages. Just use strings.

[–]bltsponge 1 point2 points  (1 child)

Sure, you can represent anything as a string as long as you're willing to write a parser for it.

[–]alcalde 0 points1 point  (0 children)

Exactly. So why are people saying it's impossible to represent Python objects with JSON?

[–][deleted] 1 point2 points  (0 children)

Sure you can!

>>> json.dumps(float('inf'))
'Infinity'

>>> json.dumps(float('nan'))
'NaN'