Author steven.daprano
Recipients Daniel Ward, Ollie Ford, abarry, steven.daprano
Date 2016-06-21.14:43:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1466520191.29.0.181236194348.issue27362@psf.upfronthosting.co.za>
In-reply-to
Content
For starters, dunder names like __json__ are reserved for Python's own use, so you would have to get the core developers to officially bless this use.


But... I'm not really sure that "the responsibility of determining how an object should be represented in JSON objects firmly within the object itself" is a good idea. For a general purpose protocol, I don't think you can trust any object to return valid JSON. What if my object.__json__ returned "}key='c" or some other invalid string? Whose responsibility is it to check that __json__ returns valid JSON?

I don't think there is any need to make this an official protocol. You know your own objects, you know if you can trust them, and you can call any method you like. So your example becomes:

    my_json_string = json.dumps(
        {'success': True, 'counter': object_counter.to_json()})

which is okay because that's clearly *your* responsibility to make sure that your object's to_json method returns a valid string. If you make it an official language wide protocol, it's unclear whose responsibility it is: the object (dangerous!), the caller (difficult), the Python interpreter (unlikely), json.dumps (unlikely).
History
Date User Action Args
2016-06-21 14:43:11steven.dapranosetrecipients: + steven.daprano, abarry, Daniel Ward, Ollie Ford
2016-06-21 14:43:11steven.dapranosetmessageid: <1466520191.29.0.181236194348.issue27362@psf.upfronthosting.co.za>
2016-06-21 14:43:11steven.dapranolinkissue27362 messages
2016-06-21 14:43:11steven.dapranocreate