# Path: riv/utils.py
# def traverse_dict(d, keys, return_parent=False):
#     if return_parent:
#         # Remove the last key element and set return_parent to False
#         # for recursive calls.
#         keys = keys[:-1]
#         return_parent = False
#     if isinstance(d, dict) and len(keys) > 0:
#         if d.has_key(keys[0]):
#             return traverse_dict(d[keys[0]], keys[1:], return_parent)
#         else:
#             raise KeyError("Traversing the dictionary failed on key: %s" % keys[0])
#     elif isinstance(d, list) and len(keys) == 1:
#         try:
#             if isinstance(d[0], dict):
#                 try:
#                     return [i[keys[0]] for i in d]
#                 except KeyError, e:
#                     raise KeyError("Traversing the dictionary failed on key: %s" % keys[0])
#         except IndexError, e:
#             raise KeyError("Traversing the dictionary failed on key: %s" % keys[0])
#     else:
#         # if "keys" is specified the user requested to further traverse
#         # the dictionary. This fails so we return None. In any other
#         # case we arrived and return the value. 
#         if keys:
#             raise KeyError("Traversing the dictionary failed on key: %s" % keys[0])
#         else:
#             return d
# 
# def create_tree_with_val(d, keys, val):
#     if not keys:
#         return
#     if len(keys) == 1:
#         d[keys[0]] = val
#     if not d.has_key(keys[0]):
#         d[keys[0]] = {}
#     create_tree_with_val(d[keys[0]], keys[1:], val)
# 
# def get_url_for_object(api_name, obj, extra_id=None):
#     try:
#         if extra_id:
#             return reverse('object-%s-%s' % (api_name, obj._meta), kwargs={'id': extra_id})
#         else:
#             return reverse('object-%s-%s' % (api_name, obj._meta), kwargs={'id': obj.id})
#     except NoReverseMatch:
#         return obj._get_pk_val()


import json
from StringIO import StringIO
from django.db.models import Model
from django.core.serializers import python, json
from django.core.serializers.base import SerializationError
from django.core.urlresolvers import reverse, NoReverseMatch
from django.utils.encoding import smart_unicode, is_protected_type
from riv.utils import traverse_dict, create_tree_with_val, get_url_for_object
            del self._current[key]
        elif SEPARATOR in key and self._current.has_key(key.split(SEPARATOR)[0]):
            key_list = key.split(SEPARATOR)

            # Skip if the FK or M2M relation is blank.
            if not self._current[key_list[0]]:
                return

            if SEPARATOR in value:
                value_list = value.split(SEPARATOR)
                # Test if the key is valid to generate nice errors.
                try:
                    traverse_dict(self._current, key_list)
                except KeyError, e:
                    try:
                        traverse_dict(self._current, value_list)
                    except KeyError, e:
                        raise SerializationError("Invalid key '%s' in map_fields. Did you add '%s' to the inline fields?" % (key, key_list[0]))

                if key_list[0] == value_list[0]:
                    # If the value is a ForeignKey as well (contains the SEPARATOR)
                    # we don't handle it here. It will be handled during the 
                    # serialization of the inline element.
                    return
                else:
                    # Crossmapping between different ForeignKey objects
                    # is currently not supported.
                    raise SerializationError("Crossmapping between different ForeignKey fields is currently not supported.")

            try:

# Path: riv/utils.py
# def traverse_dict(d, keys, return_parent=False):
#     if return_parent:
#         # Remove the last key element and set return_parent to False
#         # for recursive calls.
#         keys = keys[:-1]
#         return_parent = False
#     if isinstance(d, dict) and len(keys) > 0:
#         if d.has_key(keys[0]):
#             return traverse_dict(d[keys[0]], keys[1:], return_parent)
#         else:
#             raise KeyError("Traversing the dictionary failed on key: %s" % keys[0])
#     elif isinstance(d, list) and len(keys) == 1:
#         try:
#             if isinstance(d[0], dict):
#                 try:
#                     return [i[keys[0]] for i in d]
#                 except KeyError, e:
#                     raise KeyError("Traversing the dictionary failed on key: %s" % keys[0])
#         except IndexError, e:
#             raise KeyError("Traversing the dictionary failed on key: %s" % keys[0])
#     else:
#         # if "keys" is specified the user requested to further traverse
#         # the dictionary. This fails so we return None. In any other
#         # case we arrived and return the value. 
#         if keys:
#             raise KeyError("Traversing the dictionary failed on key: %s" % keys[0])
#         else:
#             return d
# 
# def create_tree_with_val(d, keys, val):
#     if not keys:
#         return
#     if len(keys) == 1:
#         d[keys[0]] = val
#     if not d.has_key(keys[0]):
#         d[keys[0]] = {}
#     create_tree_with_val(d[keys[0]], keys[1:], val)
# 
# def get_url_for_object(api_name, obj, extra_id=None):
#     try:
#         if extra_id:
#             return reverse('object-%s-%s' % (api_name, obj._meta), kwargs={'id': extra_id})
#         else:
#             return reverse('object-%s-%s' % (api_name, obj._meta), kwargs={'id': obj.id})
#     except NoReverseMatch:
#         return obj._get_pk_val()


# Path: riv/serializers/base_serializer.py
import json
from StringIO import StringIO
from django.db.models import Model
from django.core.serializers import python, json
from django.core.serializers.base import SerializationError
from django.core.urlresolvers import reverse, NoReverseMatch
from django.utils.encoding import smart_unicode, is_protected_type
from riv.utils import traverse_dict, create_tree_with_val, get_url_for_object

            del self._current[key]
        elif SEPARATOR in key and self._current.has_key(key.split(SEPARATOR)[0]):
            key_list = key.split(SEPARATOR)

            # Skip if the FK or M2M relation is blank.
            if not self._current[key_list[0]]:
                return

            if SEPARATOR in value:
                value_list = value.split(SEPARATOR)
                # Test if the key is valid to generate nice errors.
                try:
                    traverse_dict(self._current, key_list)
                except KeyError, e:
                    try:
                        traverse_dict(self._current, value_list)
                    except KeyError, e:
                        raise SerializationError("Invalid key '%s' in map_fields. Did you add '%s' to the inline fields?" % (key, key_list[0]))

                if key_list[0] == value_list[0]:
                    # If the value is a ForeignKey as well (contains the SEPARATOR)
                    # we don't handle it here. It will be handled during the 
                    # serialization of the inline element.
                    return
                else:
                    # Crossmapping between different ForeignKey objects
                    # is currently not supported.
                    raise SerializationError("Crossmapping between different ForeignKey fields is currently not supported.")

            try:

                tmp = traverse_dict(self._current, key_list)