0

I am trying to make a dictionary of Point, in which the key is an id (unique integer number) and value is the point itself. Please see the sample code below:

from geometry_msgs.msg import Point

indices = [1, 2, 3, 4]
p1 = Point(11, 12, 13)
p2 = Point(21, 22, 23)
p3 = Point(31, 32, 33)
p4 = Point(41, 42, 43)
points = [p1, p2, p3, p4]
points_info = {indices[i]:p for i, p in enumerate(points)}

I want to convert it to JSON. So I am using JSON library as below:

import json
print json.dumps(points_info)

However, it throws following error:

TypeError: x: 11
y: 12
z: 13 is not JSON serializable

Any workaround, please? Note that I am using Python 2.7 in ROS Indigo on Ubuntu 14.04 LTS PC.

ravi
  • 6,140
  • 18
  • 77
  • 154
  • You can't convert arbitrary Python classes to JSON no, not without a converter. Your `Point()` class has a confusing `__repr__`, by the way, it's the string `'x: 11\ny: 12\nz: 13'` rather than a more helpful `'Point(11, 12, 13)'`. – Martijn Pieters Nov 16 '17 at 11:39
  • [Point](http://docs.ros.org/api/geometry_msgs/html/msg/Point.html) class is defined inside ROS not by me. Each `Point` has 3 `float64` variables, which defines the location of a point in 3-dimensional space. – ravi Nov 16 '17 at 11:51

0 Answers0