diff --git a/src/py-opentimelineio/opentimelineio/core/_core_utils.py b/src/py-opentimelineio/opentimelineio/core/_core_utils.py index d58791a67..2692bfab7 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) # noqa: F811 +def to_dict(self): # noqa: F811 + """ + 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) # noqa: F811 +def to_list(self): # noqa: F811 + """ + 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) # noqa: F811 +def to_dict(self): # noqa: F811 + """ + 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) # noqa: F811 +def to_dict(self): # noqa: F811 + """ + 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) # noqa: F811 +def to_dict(self): # noqa: F811 + """ + 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) # noqa: F811 +def to_dict(self): # noqa: F811 + """ + 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)) diff --git a/tests/test_core_utils.py b/tests/test_core_utils.py index 3c93330fb..18bfc3bd6 100644 --- a/tests/test_core_utils.py +++ b/tests/test_core_utils.py @@ -2,10 +2,13 @@ # Copyright Contributors to the OpenTimelineIO project import copy +import json import unittest import opentimelineio._otio import opentimelineio.core._core_utils +import opentimelineio.core +import opentimelineio.opentime class AnyDictionaryTests(unittest.TestCase): @@ -242,3 +245,52 @@ def test_copy(self): deepcopied = copy.deepcopy(v) self.assertIsNot(v, deepcopied) self.assertIsNot(v[2], deepcopied[2]) + + +class ConvertToPython(unittest.TestCase): + def test_SerializableObject(self): + so = opentimelineio.core.SerializableObjectWithMetadata(name="asd") + so.metadata["key1"] = opentimelineio.core.Composition() + + converted = so.to_dict() + self.assertTrue(isinstance(converted, dict)) + json.dumps(converted) + + def test_AnyDictionary(self): + ad = opentimelineio._otio.AnyDictionary() + ad["my key"] = opentimelineio.core.Composable() + + converted = ad.to_dict() + self.assertTrue(isinstance(converted, dict)) + json.dumps(converted) + + def test_AnyVector(self): + av = opentimelineio._otio.AnyVector() + av.append(1) + av.append(opentimelineio._otio.AnyDictionary()) + + converted = av.to_list() + self.assertTrue(isinstance(converted, list)) + self.assertEqual(converted, [1, {}]) + json.dumps(converted) + + def test_RationalTime(self): + rt = opentimelineio.opentime.RationalTime() + + converted = rt.to_dict() + self.assertTrue(isinstance(converted, dict)) + json.dumps(converted) + + def test_TimeRange(self): + tr = opentimelineio.opentime.TimeRange() + + converted = tr.to_dict() + self.assertTrue(isinstance(converted, dict)) + json.dumps(converted) + + def test_TimeTransform(self): + tt = opentimelineio.opentime.TimeTransform() + + converted = tt.to_dict() + self.assertTrue(isinstance(converted, dict)) + json.dumps(converted)