Skip to content

Instantly share code, notes, and snippets.

@ZdenekM
Created March 18, 2020 07:42
Show Gist options
  • Save ZdenekM/b5c1f065846ece2ed016c76b764a91d1 to your computer and use it in GitHub Desktop.
Save ZdenekM/b5c1f065846ece2ed016c76b764a91d1 to your computer and use it in GitHub Desktop.
json patch test
>>> import jsonpatch
>>>
>>> p1='{"id": "bbb", "scene_id": "bbb", "objects": [{"id": "box", "action_points": [{"id": "box-AP0", "position": {"x": -0.2453652, "y": 0.0, "z": 0.1269144}, "orientations": [{"id": "default", "orientation": {"x": 1.0, "y": 0.0, "z": 0.0, "w": 0.0}}], "robot_joints": [{"id": "default", "robot_id": "aubo", "joints": [{"name": "joint1", "value": 0}, {"name": "joint2", "value": 0}, {"name": "joint3", "value": 0}], "is_valid": true}], "actions": [{"id": "set_joints9763", "type": "RestRobotService/set_joints", "parameters": [{"id": "robot_id", "value": "\"aubo\"", "type": "string"}, {"id": "joints", "value": "\"box.box-AP0.default\"", "type": "joints"}, {"id": "move_type", "value": "\"Simple\"", "type": "string_enum"}, {"id": "speed", "value": "0.5", "type": "double"}], "inputs": [{"default": ""}], "outputs": [{"default": ""}], "uuid": "b8b5c936-718b-4f4c-9541-0940d5480332"}], "uuid": "b8e3e6bc-f614-423a-b9d2-c4dd0cdb4a77"}, {"id": "box-AP1", "position": {"x": 0.2003918, "y": 0.0, "z": 0.1591703}, "orientations": [{"id": "default", "orientation": {"x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0}}], "robot_joints": [{"id": "default", "robot_id": "aubo", "joints": [], "is_valid": false}], "actions": [{"id": "move8923", "type": "RestRobotService/move", "parameters": [{"id": "robot_id", "value": "\"aubo\"", "type": "string"}, {"id": "end_effector_id", "value": "\"gripper\"", "type": "string"}, {"id": "pose", "value": "\"box.box-AP0.default\"", "type": "pose"}, {"id": "move_type", "value": "\"Simple\"", "type": "string_enum"}, {"id": "speed", "value": "0.5", "type": "double"}], "inputs": [{"default": ""}], "outputs": [{"default": ""}], "uuid": "eec3b247-1d74-4fcb-bafe-593f94f3b620"}], "uuid": "3b2ac6b1-4a0f-4670-8b93-d9780b6f3538"}], "uuid": "4c36d4cc-b448-4d4f-99ef-13a7ac3e0de6"}], "desc": "", "has_logic": true}'
>>> p2='{"id": "bbb", "scene_id": "bbb", "objects": [{"id": "box", "action_points": [{"id": "box-AP0", "position": {"x": -0.2453651, "y": 0.0, "z": 0.1269144}, "orientations": [{"id": "default", "orientation": {"x": 1.0, "y": 0.0, "z": 0.0, "w": 0.0}}], "robot_joints": [{"id": "default", "robot_id": "aubo", "joints": [{"name": "joint1", "value": 0}, {"name": "joint2", "value": 0}, {"name": "joint3", "value": 0}], "is_valid": true}], "actions": [{"id": "set_joints9763", "type": "RestRobotService/set_joints", "parameters": [{"id": "robot_id", "value": "\"aubo\"", "type": "string"}, {"id": "joints", "value": "\"box.box-AP0.default\"", "type": "joints"}, {"id": "move_type", "value": "\"Simple\"", "type": "string_enum"}, {"id": "speed", "value": "0.5", "type": "double"}], "inputs": [{"default": ""}], "outputs": [{"default": ""}], "uuid": "b8b5c936-718b-4f4c-9541-0940d5480332"}], "uuid": "b8e3e6bc-f614-423a-b9d2-c4dd0cdb4a77"}, {"id": "box-AP1", "position": {"x": 0.2003918, "y": 0.0, "z": 0.1591703}, "orientations": [{"id": "default", "orientation": {"x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0}}], "robot_joints": [{"id": "default", "robot_id": "aubo", "joints": [], "is_valid": false}], "actions": [{"id": "move8923", "type": "RestRobotService/move", "parameters": [{"id": "robot_id", "value": "\"aubo\"", "type": "string"}, {"id": "end_effector_id", "value": "\"gripper\"", "type": "string"}, {"id": "pose", "value": "\"box.box-AP0.default\"", "type": "pose"}, {"id": "move_type", "value": "\"Simple\"", "type": "string_enum"}, {"id": "speed", "value": "0.6", "type": "double"}], "inputs": [{"default": ""}], "outputs": [{"default": ""}], "uuid": "eec3b247-1d74-4fcb-bafe-593f94f3b620"}], "uuid": "3b2ac6b1-4a0f-4670-8b93-d9780b6f3538"}], "uuid": "4c36d4cc-b448-4d4f-99ef-13a7ac3e0de6"}], "desc": "", "has_logic": true}'
>>>
>>> patch = jsonpatch.make_patch(p1,p2)
>>> str(patch)
'[{"op": "replace", "path": "", "value": "{\\"id\\": \\"bbb\\", \\"scene_id\\": \\"bbb\\", \\"objects\\": [{\\"id\\": \\"box\\", \\"action_points\\": [{\\"id\\": \\"box-AP0\\", \\"position\\": {\\"x\\": -0.2453651, \\"y\\": 0.0, \\"z\\": 0.1269144}, \\"orientations\\": [{\\"id\\": \\"default\\", \\"orientation\\": {\\"x\\": 1.0, \\"y\\": 0.0, \\"z\\": 0.0, \\"w\\": 0.0}}], \\"robot_joints\\": [{\\"id\\": \\"default\\", \\"robot_id\\": \\"aubo\\", \\"joints\\": [{\\"name\\": \\"joint1\\", \\"value\\": 0}, {\\"name\\": \\"joint2\\", \\"value\\": 0}, {\\"name\\": \\"joint3\\", \\"value\\": 0}], \\"is_valid\\": true}], \\"actions\\": [{\\"id\\": \\"set_joints9763\\", \\"type\\": \\"RestRobotService/set_joints\\", \\"parameters\\": [{\\"id\\": \\"robot_id\\", \\"value\\": \\"\\"aubo\\"\\", \\"type\\": \\"string\\"}, {\\"id\\": \\"joints\\", \\"value\\": \\"\\"box.box-AP0.default\\"\\", \\"type\\": \\"joints\\"}, {\\"id\\": \\"move_type\\", \\"value\\": \\"\\"Simple\\"\\", \\"type\\": \\"string_enum\\"}, {\\"id\\": \\"speed\\", \\"value\\": \\"0.5\\", \\"type\\": \\"double\\"}], \\"inputs\\": [{\\"default\\": \\"\\"}], \\"outputs\\": [{\\"default\\": \\"\\"}], \\"uuid\\": \\"b8b5c936-718b-4f4c-9541-0940d5480332\\"}], \\"uuid\\": \\"b8e3e6bc-f614-423a-b9d2-c4dd0cdb4a77\\"}, {\\"id\\": \\"box-AP1\\", \\"position\\": {\\"x\\": 0.2003918, \\"y\\": 0.0, \\"z\\": 0.1591703}, \\"orientations\\": [{\\"id\\": \\"default\\", \\"orientation\\": {\\"x\\": 0.0, \\"y\\": 0.0, \\"z\\": 0.0, \\"w\\": 1.0}}], \\"robot_joints\\": [{\\"id\\": \\"default\\", \\"robot_id\\": \\"aubo\\", \\"joints\\": [], \\"is_valid\\": false}], \\"actions\\": [{\\"id\\": \\"move8923\\", \\"type\\": \\"RestRobotService/move\\", \\"parameters\\": [{\\"id\\": \\"robot_id\\", \\"value\\": \\"\\"aubo\\"\\", \\"type\\": \\"string\\"}, {\\"id\\": \\"end_effector_id\\", \\"value\\": \\"\\"gripper\\"\\", \\"type\\": \\"string\\"}, {\\"id\\": \\"pose\\", \\"value\\": \\"\\"box.box-AP0.default\\"\\", \\"type\\": \\"pose\\"}, {\\"id\\": \\"move_type\\", \\"value\\": \\"\\"Simple\\"\\", \\"type\\": \\"string_enum\\"}, {\\"id\\": \\"speed\\", \\"value\\": \\"0.6\\", \\"type\\": \\"double\\"}], \\"inputs\\": [{\\"default\\": \\"\\"}], \\"outputs\\": [{\\"default\\": \\"\\"}], \\"uuid\\": \\"eec3b247-1d74-4fcb-bafe-593f94f3b620\\"}], \\"uuid\\": \\"3b2ac6b1-4a0f-4670-8b93-d9780b6f3538\\"}], \\"uuid\\": \\"4c36d4cc-b448-4d4f-99ef-13a7ac3e0de6\\"}], \\"desc\\": \\"\\", \\"has_logic\\": true}"}]'
>>> len(str(patch))
2163
>>>
>>> res=patch.apply(p1)
>>> res==p1
False
>>> res==p2
True
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment