# Buggy code
def get_host_info_dict_from_instance(self, instance):
    instance_vars = {}
    for key in vars(instance):
        value = getattr(instance, key)
        key = self.to_safe("ec2_" + key)

        # Handle complex types
        # state/previous_state changed to properties in boto in https://github.com/boto/boto/commit/a23c379837f698212252720d2af8dec0325c9518
        if key == "ec2__state":
            instance_vars["ec2_state"] = instance.state or ""
            instance_vars["ec2_state_code"] = instance.state_code
        elif key == "ec2__previous_state":
            instance_vars["ec2_previous_state"] = instance.previous_state or ""
            instance_vars["ec2_previous_state_code"] = instance.previous_state_code
        elif type(value) in [int, bool]:
            instance_vars[key] = value
        elif isinstance(value, six.string_types):
            instance_vars[key] = value.strip()
        elif type(value) == type(None):
            instance_vars[key] = ""
        elif key == "ec2_region":
            instance_vars[key] = value.name
        elif key == "ec2__placement":
            instance_vars["ec2_placement"] = value.zone
        elif key == "ec2_tags":
            for k, v in value.items():
                if self.expand_csv_tags and "," in v:
                    v = map(lambda x: x.strip(), v.split(","))
                key = self.to_safe("ec2_tag_" + k)
                instance_vars[key] = v
        elif key == "ec2_groups":
            group_ids = []
            group_names = []
            for group in value:
                group_ids.append(group.id)
                group_names.append(group.name)
            instance_vars["ec2_security_group_ids"] = ",".join(
                [str(i) for i in group_ids]
            )
            instance_vars["ec2_security_group_names"] = ",".join(
                [str(i) for i in group_names]
            )
        elif key == "ec2_block_device_mapping":
            instance_vars["ec2_block_devices"] = {}
            for k, v in value.items():
                instance_vars["ec2_block_devices"][os.path.basename(k)] = v.volume_id
        else:
            pass
            # TODO Product codes if someone finds them useful
            # print key
            # print type(value)
            # print value

    return instance_vars


# Traceback (TypeError)
$  python ec2.py.2 --list
Traceback (most recent call last):
File "ec2.py.2", line 1510, in <module>
Ec2Inventory()
File "ec2.py.2", line 188, in __init__
self.do_api_calls_update_cache()
File "ec2.py.2", line 496, in do_api_calls_update_cache
self.write_to_cache(self.inventory, self.cache_path_cache)
File "ec2.py.2", line 1483, in write_to_cache
json_data = self.json_format_dict(data, True)
File "ec2.py.2", line 1504, in json_format_dict
return json.dumps(data, sort_keys=True, indent=2)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 200, in encode
chunks = list(chunks)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 429, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 403, in _iterencode_dict
yield from chunks
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 403, in _iterencode_dict
yield from chunks
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 403, in _iterencode_dict
yield from chunks
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 403, in _iterencode_dict
yield from chunks
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 436, in _iterencode
o = _default(o)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <map object at 0x10a433438> is not JSON serializable

# Fixed code
def get_host_info_dict_from_instance(self, instance):
    instance_vars = {}
    for key in vars(instance):
        value = getattr(instance, key)
        key = self.to_safe("ec2_" + key)

        # Handle complex types
        # state/previous_state changed to properties in boto in https://github.com/boto/boto/commit/a23c379837f698212252720d2af8dec0325c9518
        if key == "ec2__state":
            instance_vars["ec2_state"] = instance.state or ""
            instance_vars["ec2_state_code"] = instance.state_code
        elif key == "ec2__previous_state":
            instance_vars["ec2_previous_state"] = instance.previous_state or ""
            instance_vars["ec2_previous_state_code"] = instance.previous_state_code
        elif type(value) in [int, bool]:
            instance_vars[key] = value
        elif isinstance(value, six.string_types):
            instance_vars[key] = value.strip()
        elif type(value) == type(None):
            instance_vars[key] = ""
        elif key == "ec2_region":
            instance_vars[key] = value.name
        elif key == "ec2__placement":
            instance_vars["ec2_placement"] = value.zone
        elif key == "ec2_tags":
            for k, v in value.items():
                if self.expand_csv_tags and "," in v:
                    v = list(map(lambda x: x.strip(), v.split(",")))
                key = self.to_safe("ec2_tag_" + k)
                instance_vars[key] = v
        elif key == "ec2_groups":
            group_ids = []
            group_names = []
            for group in value:
                group_ids.append(group.id)
                group_names.append(group.name)
            instance_vars["ec2_security_group_ids"] = ",".join(
                [str(i) for i in group_ids]
            )
            instance_vars["ec2_security_group_names"] = ",".join(
                [str(i) for i in group_names]
            )
        elif key == "ec2_block_device_mapping":
            instance_vars["ec2_block_devices"] = {}
            for k, v in value.items():
                instance_vars["ec2_block_devices"][os.path.basename(k)] = v.volume_id
        else:
            pass
            # TODO Product codes if someone finds them useful
            # print key
            # print type(value)
            # print value

    return instance_vars