From 4f3e9142f4f4fbc460a3b1459c4182bce7c50734 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Wed, 22 Jun 2016 18:41:11 +0000 Subject: Sets are not JSON serializable Commit 46384b6782f0b73318f764bf02f24cdbcdd059c9 introduced storing the reaction users as a set in the message_json representation. However sets are not JSON serializable. Instead uses a list and checks for membership prior to manipulation. Fixes the traceback: Traceback (most recent call last): File "wee_slack.py", line 2105, in cache_write_cb cache_file.write("{}\n".format(json.dumps(message.message_json))) File "/usr/lib/python2.7/json/__init__.py", line 244, in dumps return _default_encoder.encode(obj) File "/usr/lib/python2.7/json/encoder.py", line 207, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode return _iterencode(o, 0) File "/usr/lib/python2.7/json/encoder.py", line 184, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: set([u'Uxxxxxxxx']) is not JSON serializable --- wee_slack.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/wee_slack.py b/wee_slack.py index cbe98ec..5098748 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -952,18 +952,19 @@ class Message(object): if "reactions" in self.message_json: found = False for r in self.message_json["reactions"]: - if r["name"] == reaction: - r["users"].add(user) + if r["name"] == reaction and user not in r["users"]: + r["users"].append(user) found = True + if not found: - self.message_json["reactions"].append({u"name": reaction, u"users": {user}}) + self.message_json["reactions"].append({u"name": reaction, u"users": [user]}) else: - self.message_json["reactions"] = [{u"name": reaction, u"users": {user}}] + self.message_json["reactions"] = [{u"name": reaction, u"users": [user]}] def remove_reaction(self, reaction, user): if "reactions" in self.message_json: for r in self.message_json["reactions"]: - if r["name"] == reaction: + if r["name"] == reaction and user in r["users"]: r["users"].remove(user) else: pass -- cgit