# Path: ingenico/connect/sdk/defaultimpl/default_marshaller.py
# class DefaultMarshaller(Marshaller):
#     """
#     Marshaller implementation based on json.
#     """
# 
#     @staticmethod
#     def INSTANCE():
#         return _DEFAULT_MARSHALLER_INSTANCE
# 
#     def marshal(self, request_object):
#         if isinstance(request_object, DataObject):
#             dictionary = request_object.to_dictionary()
#             return dumps(dictionary, default=lambda o: o.to_dictionary(),
#                          indent=4)
#         else:
#             return dumps(request_object, default=lambda o: o.__dict__, indent=4)
# 
#     def unmarshal(self, response_json, type_class):
#         if not response_json:
#             return None
#         if issubclass(type_class, DataObject):
#             try:
#                 return type_class().from_dictionary(loads(response_json))
#             except ValueError as e:
#                 raise MarshallerSyntaxException(e)
# 
#         class Object(object):
#             pass
# 
#         def convert_to_object(d):
#             try:
#                 d = dict(d)
#             except (TypeError, ValueError):
#                 return d
#             o = Object()
#             for key, value in d.items():
#                 o.__dict__[key] = convert_to_object(value)
#             return o
# 
#         try:
#             dictionary = loads(response_json)
#             converted_object = convert_to_object(dictionary)
#             return converted_object
#         except ValueError as e:
#             raise MarshallerSyntaxException(e)


import unittest
from ingenico.connect.sdk.data_object import DataObject
from ingenico.connect.sdk.domain.definitions.address import Address
from ingenico.connect.sdk.domain.token.definitions.customer_token import CustomerToken
from ingenico.connect.sdk.domain.token.definitions.token_card import TokenCard
from ingenico.connect.sdk.domain.token.definitions.token_card_data import TokenCardData
from ingenico.connect.sdk.domain.token.token_response import TokenResponse
from ingenico.connect.sdk.defaultimpl.default_marshaller import DefaultMarshaller
        dummy_object.foo = mini_dummy
        dummy_object.bar = True
        dummy_object.boo = 0o1
        dummy_object.far = "close"
        dummy_object.extra_field = "something else"
        marshaller = DefaultMarshaller.INSTANCE()

        json = marshaller.marshal(dummy_object)
        unmarshalled = marshaller.unmarshal(json, JsonDummy)

        self.assertEqual(True, unmarshalled.bar)
        self.assertEqual(0o1, unmarshalled.boo)
        self.assertEqual("close", unmarshalled.far)
        self.assertEqual("hiddenfoo", unmarshalled.foo.foo.foo)

    def test_unmarshal_extended_token_response(self):
        """Tests if the marshaller is able to marshal an object that inherits from token_response
        in such a way that it can be interpreted as a token_response
        """
        token_response = TokenResponseWithExtraField()
        billing_address = Address()
        billing_address.country_code = "NL"
        customer = CustomerToken()
        customer.billing_address = billing_address
        card = TokenCard()
        card_data = TokenCardData()
        card.customer = customer
        card.data = card_data
        token_response.card = card
        token_response.extraField = "a random string"

# Path: ingenico/connect/sdk/defaultimpl/default_marshaller.py
# class DefaultMarshaller(Marshaller):
#     """
#     Marshaller implementation based on json.
#     """
# 
#     @staticmethod
#     def INSTANCE():
#         return _DEFAULT_MARSHALLER_INSTANCE
# 
#     def marshal(self, request_object):
#         if isinstance(request_object, DataObject):
#             dictionary = request_object.to_dictionary()
#             return dumps(dictionary, default=lambda o: o.to_dictionary(),
#                          indent=4)
#         else:
#             return dumps(request_object, default=lambda o: o.__dict__, indent=4)
# 
#     def unmarshal(self, response_json, type_class):
#         if not response_json:
#             return None
#         if issubclass(type_class, DataObject):
#             try:
#                 return type_class().from_dictionary(loads(response_json))
#             except ValueError as e:
#                 raise MarshallerSyntaxException(e)
# 
#         class Object(object):
#             pass
# 
#         def convert_to_object(d):
#             try:
#                 d = dict(d)
#             except (TypeError, ValueError):
#                 return d
#             o = Object()
#             for key, value in d.items():
#                 o.__dict__[key] = convert_to_object(value)
#             return o
# 
#         try:
#             dictionary = loads(response_json)
#             converted_object = convert_to_object(dictionary)
#             return converted_object
#         except ValueError as e:
#             raise MarshallerSyntaxException(e)


# Path: tests/unit/test_default_marshaller.py
import unittest
from ingenico.connect.sdk.data_object import DataObject
from ingenico.connect.sdk.domain.definitions.address import Address
from ingenico.connect.sdk.domain.token.definitions.customer_token import CustomerToken
from ingenico.connect.sdk.domain.token.definitions.token_card import TokenCard
from ingenico.connect.sdk.domain.token.definitions.token_card_data import TokenCardData
from ingenico.connect.sdk.domain.token.token_response import TokenResponse
from ingenico.connect.sdk.defaultimpl.default_marshaller import DefaultMarshaller

        dummy_object.foo = mini_dummy
        dummy_object.bar = True
        dummy_object.boo = 0o1
        dummy_object.far = "close"
        dummy_object.extra_field = "something else"
        marshaller = DefaultMarshaller.INSTANCE()

        json = marshaller.marshal(dummy_object)
        unmarshalled = marshaller.unmarshal(json, JsonDummy)

        self.assertEqual(True, unmarshalled.bar)
        self.assertEqual(0o1, unmarshalled.boo)
        self.assertEqual("close", unmarshalled.far)
        self.assertEqual("hiddenfoo", unmarshalled.foo.foo.foo)

    def test_unmarshal_extended_token_response(self):
        """Tests if the marshaller is able to marshal an object that inherits from token_response
        in such a way that it can be interpreted as a token_response
        """
        token_response = TokenResponseWithExtraField()
        billing_address = Address()
        billing_address.country_code = "NL"
        customer = CustomerToken()
        customer.billing_address = billing_address
        card = TokenCard()
        card_data = TokenCardData()
        card.customer = customer
        card.data = card_data
        token_response.card = card
        token_response.extraField = "a random string"

        marshaller = DefaultMarshaller.INSTANCE()