Skip to content

Commit

Permalink
Fixes RobotWebTools#313 by fixing the has_binary check
Browse files Browse the repository at this point in the history
has_binary now checks for lists of binary objects
  • Loading branch information
Mohamed Behery committed Jan 9, 2018
1 parent 1865ca4 commit a4a4c58
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions rosbridge_library/src/rosbridge_library/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,26 @@ def is_number(s):
return True
except ValueError:
return False

def has_binary(d):
if type(d)==bson.Binary:


def has_binary(obj):
""" Returns True if obj is a binary or contains a binary attribute
"""

if type(obj) is bson.binary.Binary:
return True
if type(d)==dict:
for k,v in d.iteritems():

if type(obj) is list:
for item in obj:
if has_binary(item):
return True

if type(obj) is dict:
for k, v in obj.iteritems():
if has_binary(v):
return True
return False
return False


class Protocol:
""" The interface for a single client to interact with ROS.
Expand Down Expand Up @@ -284,7 +295,7 @@ def serialize(self, msg, cid=None):
try:
if has_binary(msg) or self.bson_only_mode:
return bson.BSON.encode(msg)
else:
else:
return json.dumps(msg)
except:
if cid is not None:
Expand Down

0 comments on commit a4a4c58

Please sign in to comment.