From b80a871120c1c2babba61c4cb5fdcb985e1ea431 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Morin Date: Wed, 28 Feb 2024 21:54:26 -0500 Subject: [PATCH] Add to_dict method to all classes that can be converted to dict and to_list to AnyVector. Useful to pass to json.dumps. Signed-off-by: Jean-Christophe Morin --- .../opentimelineio/core/_core_utils.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/py-opentimelineio/opentimelineio/core/_core_utils.py b/src/py-opentimelineio/opentimelineio/core/_core_utils.py index d58791a67b..548a33fac9 100644 --- a/src/py-opentimelineio/opentimelineio/core/_core_utils.py +++ b/src/py-opentimelineio/opentimelineio/core/_core_utils.py @@ -4,6 +4,7 @@ import types import collections.abc import copy +import json from .. import ( _otio, @@ -14,6 +15,11 @@ AnyVector, PyAny ) +from .. _opentime import ( + RationalTime, + TimeRange, + TimeTransform +) SUPPORTED_VALUE_TYPES = ( @@ -388,3 +394,57 @@ def __deepcopy__(self, *args, **kwargs): @add_method(SerializableObject) def __copy__(self, *args, **kwargs): raise ValueError("SerializableObjects may not be shallow copied.") + + +@add_method(AnyDictionary) +def to_dict(self): + """ + Convert to a built-in dict. It will recursively convert all values + to their corresponding python built-in types. + """ + return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0)) + + +@add_method(AnyVector) +def to_list(self): + """ + Convert to a built-in list. It will recursively convert all values + to their corresponding python built-in types. + """ + return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0)) + + +@add_method(SerializableObject) +def to_dict(self): + """ + Convert to a built-in dict. It will recursively convert all values + to their corresponding python built-in types. + """ + return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0)) + + +@add_method(RationalTime) +def to_dict(self): + """ + Convert to a built-in dict. It will recursively convert all values + to their corresponding python built-in types. + """ + return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0)) + + +@add_method(TimeRange) +def to_dict(self): + """ + Convert to a built-in dict. It will recursively convert all values + to their corresponding python built-in types. + """ + return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0)) + + +@add_method(TimeTransform) +def to_dict(self): + """ + Convert to a built-in dict. It will recursively convert all values + to their corresponding python built-in types. + """ + return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0))