added support for ruamel types
This commit is contained in:
1 parent
381835c642
commit
8adb86b2ec
2 files changed
+40
No files matched your search
@@ -1,5 +1,10 @@
|
|||||||
import orjson
|
import orjson
|
||||||
from typing import Any, Union
|
from typing import Any, Union
|
||||||
|
from ruamel.yaml.scalarstring import ScalarString
|
||||||
|
from ruamel.yaml.scalarint import ScalarInt
|
||||||
|
from ruamel.yaml.scalarbool import ScalarBoolean
|
||||||
|
from ruamel.yaml.scalarfloat import ScalarFloat
|
||||||
|
from src.yaml_serializer.types import SecretValue
|
||||||
from src.yaml_serializer.types import SecretValue
|
from src.yaml_serializer.types import SecretValue
|
||||||
|
|
||||||
# TODO: add support for these
|
# TODO: add support for these
|
||||||
@@ -9,6 +14,14 @@ NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED"
|
|||||||
def __default(obj: Any) -> str:
|
def __default(obj: Any) -> str:
|
||||||
if isinstance(obj, SecretValue):
|
if isinstance(obj, SecretValue):
|
||||||
return NOT_IMPLEMENTED_SV_MSG
|
return NOT_IMPLEMENTED_SV_MSG
|
||||||
|
if isinstance(obj, ScalarString):
|
||||||
|
return str(obj)
|
||||||
|
if isinstance(obj, ScalarBoolean):
|
||||||
|
return bool(obj)
|
||||||
|
if isinstance(obj, ScalarFloat):
|
||||||
|
return float(obj)
|
||||||
|
if isinstance(obj, ScalarInt):
|
||||||
|
return int(obj)
|
||||||
raise TypeError
|
raise TypeError
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import json
|
import json
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
from ruamel.yaml.scalarstring import ScalarString
|
||||||
|
from ruamel.yaml.scalarint import ScalarInt
|
||||||
|
from ruamel.yaml.scalarbool import ScalarBoolean
|
||||||
|
from ruamel.yaml.scalarfloat import ScalarFloat
|
||||||
from src.yaml_serializer.types import SecretValue
|
from src.yaml_serializer.types import SecretValue
|
||||||
from src.json_serializer import json_dumps, NOT_IMPLEMENTED_SV_MSG
|
from src.json_serializer import json_dumps, NOT_IMPLEMENTED_SV_MSG
|
||||||
|
|
||||||
@@ -29,3 +33,26 @@ class json_serializer_tests(TestCase):
|
|||||||
separators=(",", ":"),
|
separators=(",", ":"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_convert_ruamel_types(self):
|
||||||
|
self.assertEqual(
|
||||||
|
json_dumps(
|
||||||
|
{
|
||||||
|
"int": ScalarInt(1),
|
||||||
|
"float": ScalarFloat(1.3),
|
||||||
|
"bool": ScalarBoolean(False),
|
||||||
|
"list": [1, ScalarString("w")],
|
||||||
|
"tup": (1, ScalarString("w")),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"int": 1,
|
||||||
|
"float": 1.3,
|
||||||
|
"bool": 0,
|
||||||
|
"list": [1, "w"],
|
||||||
|
"tup": (1, "w"),
|
||||||
|
},
|
||||||
|
separators=(",", ":"),
|
||||||
|
),
|
||||||
|
)
|
||||||
Reference in new issue
Block a user