Fixed broken `yaml` serialization with `benedict` attributes. #89
This commit is contained in:
parent
95a0ca196c
commit
74db9d7b7c
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from benedict.serializers.abstract import AbstractSerializer
|
from benedict.serializers.abstract import AbstractSerializer
|
||||||
|
from benedict.serializers.json import JSONSerializer
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
@ -12,11 +13,13 @@ class YAMLSerializer(AbstractSerializer):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(YAMLSerializer, self).__init__()
|
super(YAMLSerializer, self).__init__()
|
||||||
|
self._json_serializer = JSONSerializer()
|
||||||
|
|
||||||
def decode(self, s, **kwargs):
|
def decode(self, s, **kwargs):
|
||||||
data = yaml.safe_load(s, **kwargs)
|
data = yaml.safe_load(s, **kwargs)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def encode(self, d, **kwargs):
|
def encode(self, d, **kwargs):
|
||||||
data = yaml.dump(dict(d.items()), **kwargs)
|
d = self._json_serializer.decode(self._json_serializer.encode(d))
|
||||||
|
data = yaml.dump(d, **kwargs)
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from benedict import benedict
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class github_issue_0089_test_case(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
This class describes a github issue 0089 test case.
|
||||||
|
https://github.com/fabiocaccamo/python-benedict/issues/89
|
||||||
|
|
||||||
|
To run this specific test:
|
||||||
|
- Run python -m unittest tests.github.test_issue_0089
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_broken_serialization_with_benedict_attributes(self):
|
||||||
|
d1 = benedict()
|
||||||
|
d1["a"] = benedict({"b": 2})
|
||||||
|
yaml_str = d1.to_yaml()
|
||||||
|
# print(yaml_str)
|
||||||
|
self.assertEqual(yaml_str, "a:\n b: 2\n")
|
||||||
|
d2 = benedict.from_yaml(yaml_str)
|
||||||
|
self.assertEqual(d1, d2)
|
Loading…
Reference in New Issue