# Path: typepy/error.py
# class TypeConversionError(TypeError):
#     """
#     Exception raised when failed to convert data.
#     """
# 
# Path: typepy/converter/_interface.py
# class AbstractValueConverter(ValueConverterInterface):
#     __slots__ = "_value"
# 
#     def __init__(self, value, params):
#         self._value = value
#         self._params = params
# 
#     def __repr__(self):
#         try:
#             string = str(self.force_convert())
#         except TypeConversionError:
#             string = "[ValueConverter ERROR] failed to force_convert"
# 
#         return string


import json
from ..error import TypeConversionError
from ._interface import AbstractValueConverter
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""




class DictionaryConverter(AbstractValueConverter):
    def force_convert(self):
        try:
            return dict(self._value)
        except (TypeError, ValueError):
            pass

        if isinstance(self._value, str):
            try:
                return json.loads(self._value)
            except json.JSONDecodeError:
                pass


# Path: typepy/error.py
# class TypeConversionError(TypeError):
#     """
#     Exception raised when failed to convert data.
#     """
# 
# Path: typepy/converter/_interface.py
# class AbstractValueConverter(ValueConverterInterface):
#     __slots__ = "_value"
# 
#     def __init__(self, value, params):
#         self._value = value
#         self._params = params
# 
#     def __repr__(self):
#         try:
#             string = str(self.force_convert())
#         except TypeConversionError:
#             string = "[ValueConverter ERROR] failed to force_convert"
# 
#         return string


# Path: typepy/converter/_dictionary.py
import json
from ..error import TypeConversionError
from ._interface import AbstractValueConverter

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""




class DictionaryConverter(AbstractValueConverter):
    def force_convert(self):
        try:
            return dict(self._value)
        except (TypeError, ValueError):
            pass

        if isinstance(self._value, str):
            try:
                return json.loads(self._value)
            except json.JSONDecodeError:
                pass


        raise TypeConversionError(