Skip to content

Commit

Permalink
Add to_dict method to all classes that can be converted to dict and t…
Browse files Browse the repository at this point in the history
…o_list to AnyVector. Useful to pass to json.dumps.

Signed-off-by: Jean-Christophe Morin <[email protected]>
  • Loading branch information
JeanChristopheMorinPerso committed Feb 29, 2024
1 parent 4c17494 commit b80a871
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/py-opentimelineio/opentimelineio/core/_core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import types
import collections.abc
import copy
import json

from .. import (
_otio,
Expand All @@ -14,6 +15,11 @@
AnyVector,
PyAny
)
from .. _opentime import (
RationalTime,
TimeRange,
TimeTransform
)


SUPPORTED_VALUE_TYPES = (
Expand Down Expand Up @@ -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))

0 comments on commit b80a871

Please sign in to comment.