//TODO: Refactor Stringify functions to share core logic

        /*
         * I know, I know, this is really bad form.  It turns out that there is a
         * significant amount of garbage created when calling as a coroutine, so this
         * method is duplicated.  Hopefully there won't be too many future changes, but
         * I would still like a more elegant way to optionaly yield
         */
        void Stringify(int depth, StringBuilder builder, bool pretty = false)   //Convert the JSONObject into a string
        //Profiler.BeginSample("JSONprint");
        {
            if (depth++ > MAX_DEPTH)
            {
#if UNITY_2 || UNITY_3 || UNITY_4 || UNITY_5
                Debug.Log
#else
                Debug.WriteLine
#endif
                    ("reached max depth!");

                return;
            }
            switch (type)
            {
            case Type.BAKED:
                builder.Append(str);
                break;

            case Type.STRING:
                builder.AppendFormat("\"{0}\"", str);
                break;

            case Type.NUMBER:
                if (useInt)
                {
                    builder.Append(i.ToString());
                }
                else
                {
#if USEFLOAT
                    if (float.IsInfinity(n))
                    {
                        builder.Append(INFINITY);
                    }
                    else if (float.IsNegativeInfinity(n))
                    {
                        builder.Append(NEGINFINITY);
                    }
                    else if (float.IsNaN(n))
                    {
                        builder.Append(NaN);
                    }
#else
                    if (double.IsInfinity(n))
                    {
                        builder.Append(INFINITY);
                    }
                    else if (double.IsNegativeInfinity(n))
                    {
                        builder.Append(NEGINFINITY);
                    }
                    else if (double.IsNaN(n))
                    {
                        builder.Append(NaN);
                    }
#endif
                    else
                    {
                        builder.Append(n.ToString());
                    }
                }
                break;

            case Type.OBJECT:
                builder.Append("{");
                if (list.Count > 0)
                {
#if (PRETTY)             //for a bit more readability, comment the define above to disable system-wide
                    if (pretty)
                    {
                        builder.Append("\n");
                    }
#endif
                    for (int i = 0; i < list.Count; i++)
                    {
                        string     key = keys[i];
                        JSONObject obj = list[i];
                        if (obj)
                        {
#if (PRETTY)
                            if (pretty)
                            {
                                for (int j = 0; j < depth; j++)
                                {
                                    builder.Append("\t");     //for a bit more readability
                                }
                            }
#endif
                            builder.AppendFormat("\"{0}\":", key);
                            obj.Stringify(depth, builder, pretty);
                            builder.Append(",");
#if (PRETTY)
                            if (pretty)
                            {
                                builder.Append("\n");
                            }
#endif
                        }
                    }
#if (PRETTY)
                    if (pretty)
                    {
                        builder.Length -= 2;
                    }
                    else
#endif
                    builder.Length--;
                }
#if (PRETTY)
                if (pretty && list.Count > 0)
                {
                    builder.Append("\n");
                    for (int j = 0; j < depth - 1; j++)
                    {
                        builder.Append("\t");     //for a bit more readability
                    }
                }
#endif
                builder.Append("}");
                break;

            case Type.ARRAY:
                builder.Append("[");
                if (list.Count > 0)
                {
#if (PRETTY)
                    if (pretty)
                    {
                        builder.Append("\n");     //for a bit more readability
                    }
#endif
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i])
                        {
#if (PRETTY)
                            if (pretty)
                            {
                                for (int j = 0; j < depth; j++)
                                {
                                    builder.Append("\t");     //for a bit more readability
                                }
                            }
#endif
                            list[i].Stringify(depth, builder, pretty);
                            builder.Append(",");
#if (PRETTY)
                            if (pretty)
                            {
                                builder.Append("\n");     //for a bit more readability
                            }
#endif
                        }
                    }
#if (PRETTY)
                    if (pretty)
                    {
                        builder.Length -= 2;
                    }
                    else
#endif
                    builder.Length--;
                }
#if (PRETTY)
                if (pretty && list.Count > 0)
                {
                    builder.Append("\n");
                    for (int j = 0; j < depth - 1; j++)
                    {
                        builder.Append("\t");     //for a bit more readability
                    }
                }
#endif
                builder.Append("]");
                break;

            case Type.BOOL:
                if (b)
                {
                    builder.Append("true");
                }
                else
                {
                    builder.Append("false");
                }
                break;

            case Type.NULL:
                builder.Append("null");
                break;
            }
            //Profiler.EndSample();
        }