11from configparser import DEFAULTSECT as default_section
22from configparser import RawConfigParser
33from io import StringIO
4+ from json .decoder import JSONDecodeError
45
56from benedict .serializers .abstract import AbstractSerializer
7+ from benedict .serializers .json import JSONSerializer
68from benedict .utils import type_util
79
810
@@ -17,6 +19,7 @@ def __init__(self):
1719 "ini" ,
1820 ],
1921 )
22+ self ._json = JSONSerializer ()
2023
2124 @staticmethod
2225 def _get_parser (options ):
@@ -49,9 +52,14 @@ def decode(self, s, **kwargs):
4952 for section in parser .sections ():
5053 data [section ] = {}
5154 for option , _ in parser .items (section ):
52- data [section ][option ] = self ._get_section_option_value (
53- parser , section , option
54- )
55+ value = self ._get_section_option_value (parser , section , option )
56+ if type_util .is_string (value ):
57+ try :
58+ value_decoded = self ._json .decode (value )
59+ value = value_decoded
60+ except JSONDecodeError :
61+ pass
62+ data [section ][option ] = value
5563 return data
5664
5765 def encode (self , d , ** kwargs ):
@@ -63,7 +71,10 @@ def encode(self, d, **kwargs):
6371 section = key
6472 parser .add_section (section )
6573 for option_key , option_value in value .items ():
66- parser .set (section , option_key , f"{ option_value } " )
74+ if type_util .is_collection (option_value ):
75+ parser .set (section , option_key , self ._json .encode (option_value ))
76+ else :
77+ parser .set (section , option_key , option_value )
6778 str_data = StringIO ()
6879 parser .write (str_data )
6980 return str_data .getvalue ()
0 commit comments