Пример #1
0
        private static JSONNode DoObject2JSONNode(string key, object obj, JSONNode parent)
        {
            if (obj == null)
            {
                JSONNode jnull = new JSONNull();
                if (!DoObject2JSONNode_AddToParent(parent, key, jnull))
                {
                    return(jnull);
                }
            }
            else if (obj is IDictionary)
            {
                JSONObject jobject = Johny.JSONNodePool.Claim(typeof(Johny.JSONObject)).AsObject;
                var        it      = (obj as IDictionary).GetEnumerator();
                while (it.MoveNext())
                {
                    DoObject2JSONNode(it.Key as string, it.Value, jobject);
                }

                if (!DoObject2JSONNode_AddToParent(parent, key, jobject))
                {
                    return(jobject);
                }
            }
            else if (obj is ICollection)
            {
                JSONArray jarray = Johny.JSONNodePool.Claim(typeof(Johny.JSONArray)).AsArray;
                var       it     = (obj as ICollection).GetEnumerator();
                while (it.MoveNext())
                {
                    DoObject2JSONNode(string.Empty, it.Current, jarray);
                }

                if (!DoObject2JSONNode_AddToParent(parent, key, jarray))
                {
                    return(jarray);
                }
            }
            else if (obj is string)
            {
                JSONString jstring = Johny.JSONNodePool.Claim(typeof(Johny.JSONString)) as JSONString;
                jstring.InitString(obj as string);

                if (!DoObject2JSONNode_AddToParent(parent, key, jstring))
                {
                    return(jstring);
                }
            }
            else if (obj is bool)
            {
                JSONBool jbool = new JSONBool((bool)obj);
                if (!DoObject2JSONNode_AddToParent(parent, key, jbool))
                {
                    return(jbool);
                }
            }
            else if (obj is System.Enum)
            {
                JSONNumber jnum = new JSONNumber((int)obj);
                if (!DoObject2JSONNode_AddToParent(parent, key, jnum))
                {
                    return(jnum);
                }
            }
            else if (IsNumeric(obj))
            {
                double     dd   = Convert.ToDouble(obj);
                JSONNumber jnum = new JSONNumber(dd);
                if (!DoObject2JSONNode_AddToParent(parent, key, jnum))
                {
                    return(jnum);
                }
            }
            else
            {
                EB.Debug.LogError($"此类型JSON无法解析,请自行转换成基本类型!==> {obj.GetType().FullName}");
            }

            return(null);
        }