private void traceReferences(Object root)
    {
        LinkedList<Object> stack = new LinkedList<Object>();
        stack.addFirst(root);
        final Map<Object, Long> visited = _objVisited;
        final Map<Object, Long> referenced = _objsReferenced;

        while (!stack.isEmpty())
        {
            Object obj = stack.removeFirst();
            if (obj == null)
            {
                continue;
            }

            if (!JsonReader.isPrimitive(obj.getClass()))
            {
                Long id = visited.get(obj);
                if (id != null)
                {   // Only write an object once.
                    referenced.put(obj, id);
                    continue;
                }
                visited.put(obj, _identity++);
            }

            final Class clazz = obj.getClass();

            if (clazz.isArray())
            {
                Class compType = clazz.getComponentType();
                if (!JsonReader.isPrimitive(compType))
                {   // Speed up: do not traceReferences of primitives, they cannot reference anything
                    final int len = Array.getLength(obj);

                    for (int i = 0; i < len; i++)
                    {
                        Object o = Array.get(obj, i);
                        if (o != null)
                        {   // Slight perf gain (null is legal)
                            stack.addFirst(o);
                        }
                    }
                }
            }
            else
            {
                traceFields(stack, obj);
            }
        }
    }