IEnumerable StringifyAsync(int depth, StringBuilder builder, bool pretty = false)
{ //Convert the JSONObject into a string
//Profiler.BeginSample("JSONprint");
if (depth++ > MAX_DEPTH)
{
Debug.Log("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 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);
foreach (IEnumerable e in obj.StringifyAsync(depth, builder, pretty))
yield return e;
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
foreach (IEnumerable e in list[i].StringifyAsync(depth, builder, pretty))
yield return e;
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();
}
//TODO: Refactor Stringify functions to share core logic