StringifyAsync() private method

private StringifyAsync ( int depth, StringBuilder builder, bool pretty = false ) : IEnumerable
depth int
builder StringBuilder
pretty bool
return IEnumerable
コード例 #1
0
    IEnumerable StringifyAsync(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!");

            yield break;
        }
        if (printWatch.Elapsed.TotalSeconds > maxFrameTime)
        {
            printWatch.Reset();
            yield return(null);

            printWatch.Start();
        }
        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(NEWLINE);
                }
#endif
                for (int i = 0; i < list.Count; i++)
                {
                    string     key = keys[i];
                    JSONObject obj = list[i];
                    if (obj as JSONObject != null)
                    {
#if (PRETTY)
                        if (pretty)
                        {
                            for (int j = 0; j < depth; j++)
                            {
                                builder.Append("\t");     //for a bit more readability
                            }
                        }
#endif
                        builder.AppendFormat("\"{0}\":", key);
                        foreach (IEnumerable e in obj.StringifyAsync(depth, builder, pretty))
                        {
                            yield return(e);
                        }
                        builder.Append(",");
#if (PRETTY)
                        if (pretty)
                        {
                            builder.Append(NEWLINE);
                        }
#endif
                    }
                }
#if (PRETTY)
                if (pretty)
                {
                    builder.Length -= 2;
                }
                else
#endif
                builder.Length--;
            }
#if (PRETTY)
            if (pretty && list.Count > 0)
            {
                builder.Append(NEWLINE);
                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(NEWLINE);     //for a bit more readability
                }
#endif
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i] as JSONObject != null)
                    {
#if (PRETTY)
                        if (pretty)
                        {
                            for (int j = 0; j < depth; j++)
                            {
                                builder.Append("\t");     //for a bit more readability
                            }
                        }
#endif
                        foreach (IEnumerable e in list[i].StringifyAsync(depth, builder, pretty))
                        {
                            yield return(e);
                        }
                        builder.Append(",");
#if (PRETTY)
                        if (pretty)
                        {
                            builder.Append(NEWLINE);     //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(NEWLINE);
                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();
    }
コード例 #2
0
ファイル: JSONObject.cs プロジェクト: woniupapa/lockstep.io
    IEnumerable StringifyAsync(int depth, StringBuilder builder, bool pretty)
    {
        if (depth++ > MaxDepth)
        {
            Debug.Log("reached max depth!");
            yield break;
        }
        switch (type)
        {
        case JSONType.BAKED:
            builder.Append(str);
            break;

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

        case JSONType.NUMBER:
            if (double.IsInfinity(n))
            {
                builder.Append(JSONInfinity);
            }
            else if (double.IsNegativeInfinity(n))
            {
                builder.Append(JSONNegativeInfinity);
            }
            else if (double.IsNaN(n))
            {
                builder.Append(JSONNotANumber);
            }
            else
            {
                builder.Append(n.ToString());
            }
            break;

        case JSONType.OBJECT:
            builder.Append("{");
            if (list.Count > 0)
            {
                if (pretty)
                {
                    builder.Append("\n");
                }
                for (int i = 0; i < list.Count; i++)
                {
                    string     key = keys[i];
                    JSONObject obj = list[i];
                    if (obj)
                    {
                        if (pretty)
                        {
                            for (int j = 0; j < depth; j++)
                            {
                                builder.Append("\t");
                            }
                        }
                        builder.AppendFormat("\"{0}\":", key);
                        foreach (IEnumerable e in obj.StringifyAsync(depth, builder, pretty))
                        {
                            yield return(e);
                        }
                        builder.Append(",");
                        if (pretty)
                        {
                            builder.Append("\n");
                        }
                    }
                }
                if (pretty)
                {
                    builder.Length -= 2;
                }
                else
                {
                    builder.Length--;
                }
            }
            if (pretty && list.Count > 0)
            {
                builder.Append("\n");
                for (int j = 0; j < depth - 1; j++)
                {
                    builder.Append("\t");
                }
            }
            builder.Append("}");
            break;

        case JSONType.ARRAY:
            builder.Append("[");
            if (list.Count > 0)
            {
                if (pretty)
                {
                    builder.Append("\n");
                }
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i])
                    {
                        if (pretty)
                        {
                            for (int j = 0; j < depth; j++)
                            {
                                builder.Append("\t");
                            }
                        }
                        foreach (IEnumerable e in list[i].StringifyAsync(depth, builder, pretty))
                        {
                            yield return(e);
                        }
                        builder.Append(",");
                        if (pretty)
                        {
                            builder.Append("\n");
                        }
                    }
                }
                if (pretty)
                {
                    builder.Length -= 2;
                }
                else
                {
                    builder.Length--;
                }
            }
            if (pretty && list.Count > 0)
            {
                builder.Append("\n");
                for (int j = 0; j < depth - 1; j++)
                {
                    builder.Append("\t");
                }
            }
            builder.Append("]");
            break;

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

        case JSONType.NULL:
            builder.Append("null");
            break;
        }
    }