示例#1
0
        //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)
            {

                Debug.WriteLine

            ("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 (double.IsInfinity(n))
                            builder.Append(INFINITY);
                        else if (double.IsNegativeInfinity(n))
                            builder.Append(NEGINFINITY);
                        else if (double.IsNaN(n))
                            builder.Append(NaN);

                        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();
        }
示例#2
0
 public static JSONObject Create(AddJSONContents content)
 {
     JSONObject obj = Create();
     content.Invoke(obj);
     return obj;
 }
示例#3
0
 /*
  * The Merge function is experimental. Use at your own risk.
  */
 public void Merge(JSONObject obj)
 {
     MergeRecur(this, obj);
 }
示例#4
0
 /// <summary>
 /// Create a JSONObject by parsing string data
 /// </summary>
 /// <param name="val">The string to be parsed</param>
 /// <param name="maxDepth">The maximum depth for the parser to search.  Set this to to 1 for the first level, 
 /// 2 for the first 2 levels, etc.  It defaults to -2 because -1 is the depth value that is parsed (see below)</param>
 /// <param name="storeExcessLevels">Whether to store levels beyond maxDepth in baked JSONObjects</param>
 /// <param name="strict">Whether to be strict in the parsing. For example, non-strict parsing will successfully 
 /// parse "a string" into a string-type </param>
 /// <returns></returns>
 public static JSONObject Create(string val, int maxDepth = -2, bool storeExcessLevels = false, bool strict = false)
 {
     JSONObject obj = Create();
     obj.Parse(val, maxDepth, storeExcessLevels, strict);
     return obj;
 }