From af46cf885d6f9f86bc144ef72235aa4dc87df8de Mon Sep 17 00:00:00 2001 From: Roderick Bovee Date: Wed, 21 Sep 2016 10:58:27 -0700 Subject: [PATCH] Make PotionJSONEncoder serialize self-references (e.g. to re-serialize PotionJSONSchemaDecoder output) --- potion_client/converter.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/potion_client/converter.py b/potion_client/converter.py index d528219..6f98537 100644 --- a/potion_client/converter.py +++ b/potion_client/converter.py @@ -32,6 +32,7 @@ def dst(self, dt): class PotionJSONEncoder(JSONEncoder): def encode(self, o): + root_id = id(o) if self.check_circular: markers = {} else: @@ -42,6 +43,8 @@ def _encode(o): if markers is not None: marker_id = id(o) if marker_id in markers: + if marker_id == root_id: + return {"$ref": "#"} raise ValueError("Circular reference detected") markers[marker_id] = o try: