Convert context to dict containing only builtin types.

        Args:
            fields (list of str): If present, only write these fields into the
                dict. This can be used to avoid constructing expensive fields
                (such as 'graph') for some cases.

        Returns:
            dict: Dictified context.

def to_dict(self, fields=None):
        """Convert context to dict containing only builtin types.

        Args:
            fields (list of str): If present, only write these fields into the
                dict. This can be used to avoid constructing expensive fields
                (such as 'graph') for some cases.

        Returns:
            dict: Dictified context.
        """
        data = {}

        def _add(field):
            return (fields is None or field in fields)

        if _add("resolved_packages"):
            resolved_packages = []
            for pkg in (self._resolved_packages or []):
                resolved_packages.append(pkg.handle.to_dict())
            data["resolved_packages"] = resolved_packages

        if _add("serialize_version"):
            data["serialize_version"] = \
                '.'.join(map(str, ResolvedContext.serialize_version))

        if _add("patch_locks"):
            data["patch_locks"] = dict((k, v.name) for k, v in self.patch_locks)

        if _add("package_orderers"):
            package_orderers = [package_order.to_pod(x)
                                for x in (self.package_orderers or [])]
            data["package_orderers"] = package_orderers or None

        if _add("package_filter"):
            data["package_filter"] = self.package_filter.to_pod()

        if _add("graph"):
            if self.graph_string and self.graph_string.startswith('{'):
                graph_str = self.graph_string  # already in compact format
            else:
                g = self.graph()
                graph_str = write_compacted(g)

            data["graph"] = graph_str

        data.update(dict(
            timestamp=self.timestamp,
            requested_timestamp=self.requested_timestamp,
            building=self.building,
            caching=self.caching,
            implicit_packages=map(str, self.implicit_packages),
            package_requests=map(str, self._package_requests),
            package_paths=self.package_paths,

            default_patch_lock=self.default_patch_lock.name,

            rez_version=self.rez_version,
            rez_path=self.rez_path,
            user=self.user,
            host=self.host,
            platform=self.platform,
            arch=self.arch,
            os=self.os,
            created=self.created,

            parent_suite_path=self.parent_suite_path,
            suite_context_name=self.suite_context_name,

            status=self.status_.name,
            failure_description=self.failure_description,

            from_cache=self.from_cache,
            solve_time=self.solve_time,
            load_time=self.load_time,
            num_loaded_packages=self.num_loaded_packages
        ))

        if fields:
            data = dict((k, v) for k, v in data.iteritems() if k in fields)

        return data