Skip to content

Commit b80a871

Browse files
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 <[email protected]>
1 parent 4c17494 commit b80a871

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/py-opentimelineio/opentimelineio/core/_core_utils.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import types
55
import collections.abc
66
import copy
7+
import json
78

89
from .. import (
910
_otio,
@@ -14,6 +15,11 @@
1415
AnyVector,
1516
PyAny
1617
)
18+
from .. _opentime import (
19+
RationalTime,
20+
TimeRange,
21+
TimeTransform
22+
)
1723

1824

1925
SUPPORTED_VALUE_TYPES = (
@@ -388,3 +394,57 @@ def __deepcopy__(self, *args, **kwargs):
388394
@add_method(SerializableObject)
389395
def __copy__(self, *args, **kwargs):
390396
raise ValueError("SerializableObjects may not be shallow copied.")
397+
398+
399+
@add_method(AnyDictionary)
400+
def to_dict(self):
401+
"""
402+
Convert to a built-in dict. It will recursively convert all values
403+
to their corresponding python built-in types.
404+
"""
405+
return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0))
406+
407+
408+
@add_method(AnyVector)
409+
def to_list(self):
410+
"""
411+
Convert to a built-in list. It will recursively convert all values
412+
to their corresponding python built-in types.
413+
"""
414+
return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0))
415+
416+
417+
@add_method(SerializableObject)
418+
def to_dict(self):
419+
"""
420+
Convert to a built-in dict. It will recursively convert all values
421+
to their corresponding python built-in types.
422+
"""
423+
return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0))
424+
425+
426+
@add_method(RationalTime)
427+
def to_dict(self):
428+
"""
429+
Convert to a built-in dict. It will recursively convert all values
430+
to their corresponding python built-in types.
431+
"""
432+
return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0))
433+
434+
435+
@add_method(TimeRange)
436+
def to_dict(self):
437+
"""
438+
Convert to a built-in dict. It will recursively convert all values
439+
to their corresponding python built-in types.
440+
"""
441+
return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0))
442+
443+
444+
@add_method(TimeTransform)
445+
def to_dict(self):
446+
"""
447+
Convert to a built-in dict. It will recursively convert all values
448+
to their corresponding python built-in types.
449+
"""
450+
return json.loads(_otio._serialize_json_to_string(_value_to_any(self), {}, 0))

0 commit comments

Comments
 (0)