Exemplo n.º 1
0
        /// <summary>
        /// 从对象到JsonNode
        /// </summary>
        public static JsonNode ToJsonNode(object rObject)
        {
            Type rType = rObject.GetType();

            JsonNode rRootNode = null;

            //如果是List
            if (rType.IsGenericType && typeof(IList).IsAssignableFrom(rType.GetGenericTypeDefinition()))
            {
                rRootNode = new JsonArray();
                IList rListObj = (IList)rObject;
                foreach (var rItem in rListObj)
                {
                    JsonNode rNode = ToJsonNode(rItem);
                    rRootNode.Add(rNode);
                }
            }
            else if (rType.IsArray) //如果是Array
            {
                rRootNode = new JsonArray();
                Array rArrayObj = (Array)rObject;
                foreach (var rItem in rArrayObj)
                {
                    JsonNode rNode = ToJsonNode(rItem);
                    rRootNode.Add(rNode);
                }
            }
            else if (rType.IsGenericType && typeof(IDictionary).IsAssignableFrom(rType.GetGenericTypeDefinition()))
            {
                //如果是Dictionary
                rRootNode = new JsonClass();
                IDictionary rDictObj = (IDictionary)rObject;
                foreach (var rKey in rDictObj.Keys)
                {
                    JsonNode rValueNode = ToJsonNode(rDictObj[rKey]);
                    rRootNode.Add(rKey.ToString(), rValueNode);
                }
            }
            else if (rType.IsGenericType && typeof(IDict).IsAssignableFrom(rType.GetGenericTypeDefinition()))
            {
                rRootNode = new JsonClass();
                IDict rDictObj = (IDict)rObject;
                foreach (var rItem in rDictObj.OriginCollection)
                {
                    JsonNode rValueNode = ToJsonNode(rItem.Value);
                    rRootNode.Add(rItem.Key.ToString(), rValueNode);
                }
            }
            else if (rType.IsClass) //如果是Class,获取Class所有的public的字段和属性
            {
                rRootNode = new JsonClass();
                // 所有公共的属性
                PropertyInfo[] rPropInfos = rType.GetProperties(ReflectionAssist.flags_public);
                for (int i = 0; i < rPropInfos.Length; i++)
                {
                    object   rValueObj  = rPropInfos[i].GetValue(rObject, null);
                    JsonNode rValueNode = ToJsonNode(rValueObj);
                    rRootNode.Add(rPropInfos[i].Name, rValueNode);
                }
                // 所有公共的字段
                FieldInfo[] rFieldInfos = rType.GetFields(ReflectionAssist.flags_public);
                for (int i = 0; i < rFieldInfos.Length; i++)
                {
                    object   rValueObj  = rFieldInfos[i].GetValue(rObject);
                    JsonNode rValueNode = ToJsonNode(rValueObj);
                    rRootNode.Add(rFieldInfos[i].Name, rValueNode);
                }
                // TODO: 所有预定义的序列化属性的private的字段
            }
            else if (rType.IsPrimitive) //如果是实例
            {
                if (rType == typeof(int))
                {
                    rRootNode = new JsonData((int)rObject);
                }
                else if (rType == typeof(long))
                {
                    rRootNode = new JsonData((long)rObject);
                }
                else if (rType == typeof(float))
                {
                    rRootNode = new JsonData((float)rObject);
                }
                else if (rType == typeof(double))
                {
                    rRootNode = new JsonData((double)rObject);
                }
                else if (rType == typeof(bool))
                {
                    rRootNode = new JsonData((bool)rObject);
                }
                else if (rType == typeof(string))
                {
                    rRootNode = new JsonData((string)rObject);
                }
                else
                {
                    Debug.LogError(string.Format("Type = {0}, 不支持序列化的变量类型!", rObject.GetType()));
                }
            }

            return(rRootNode);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 解析Json
        /// </summary>
        public JsonNode Parser()
        {
            this.isValid = true;
            int end = 0;
            int i   = 0;

            JsonSymbolItem rCurSymbol  = null;
            JsonSymbolItem rLastSymbol = null;

            Stack <JsonSymbolItem> rNodeStack = new Stack <JsonSymbolItem>();

            while (i < this.originData.Length)
            {
                //跳过那些无用的字符 ' ', '\t', '\r' '\n' 注释
                if (!string.IsNullOrEmpty(LexicalAnalysis.isSpecialSymbol(originData, i, ref end)) || !string.IsNullOrEmpty(LexicalAnalysis.isComment(originData, i, ref end)))
                {
                    i = end;
                    continue;
                }

                rCurSymbol = buildSymbolItem(rLastSymbol, i, ref end);
                if (rCurSymbol != null)
                {
                    switch (rCurSymbol.type)
                    {
                    case JsonSymbolType.Unknown:
                        Debug.LogError("Json format error.");
                        break;

                    case JsonSymbolType.ObjStart:
                        rCurSymbol.node = new JsonClass();
                        rNodeStack.Push(rCurSymbol);
                        break;

                    case JsonSymbolType.ObjEnd:
                        JsonNode rObject0 = new JsonClass();
                        while (rNodeStack.Count != 0 && rNodeStack.Peek().type != JsonSymbolType.ObjStart)
                        {
                            var rTopSymbol = rNodeStack.Pop();
                            if (rTopSymbol.type == JsonSymbolType.ObjSplit)
                            {
                                continue;
                            }
                            else if (rTopSymbol.type == JsonSymbolType.Element)
                            {
                                rObject0.AddHead(rTopSymbol.node.Key, rTopSymbol.node[rTopSymbol.node.Key]);
                            }
                        }
                        rNodeStack.Pop();
                        var rSymbol0 = new JsonSymbolItem();
                        rSymbol0.type  = JsonSymbolType.Value;
                        rSymbol0.node  = rObject0;
                        rSymbol0.value = rObject0.ToString();
                        Generate_ElementSymbol(ref rNodeStack, rSymbol0);
                        break;

                    case JsonSymbolType.ArrayStart:
                        rCurSymbol.node = new JsonArray();
                        rNodeStack.Push(rCurSymbol);
                        break;

                    case JsonSymbolType.ArrayEnd:
                        JsonNode rArray = new JsonArray();
                        while (rNodeStack.Peek().type != JsonSymbolType.ArrayStart)
                        {
                            var rTopSymbol = rNodeStack.Pop();
                            if (rTopSymbol.type == JsonSymbolType.ObjSplit)
                            {
                                continue;
                            }
                            else if (rTopSymbol.type == JsonSymbolType.Element)
                            {
                                rArray.AddHead(rTopSymbol.node);
                            }
                        }
                        rNodeStack.Pop();
                        var rSymbol = new JsonSymbolItem();
                        rSymbol.type  = JsonSymbolType.Value;
                        rSymbol.node  = rArray;
                        rSymbol.value = rArray.ToString();
                        Generate_ElementSymbol(ref rNodeStack, rSymbol);
                        break;

                    case JsonSymbolType.ObjSplit:
                        rNodeStack.Push(rCurSymbol);
                        break;

                    case JsonSymbolType.ElementSplit:
                        rNodeStack.Push(rCurSymbol);
                        break;

                    case JsonSymbolType.Key:
                        rNodeStack.Push(rCurSymbol);
                        break;

                    case JsonSymbolType.Value:
                        Generate_ElementSymbol(ref rNodeStack, rCurSymbol);
                        break;

                    default:
                        break;
                    }
                    i           = end;
                    rLastSymbol = rCurSymbol;
                    continue;
                }
                i++;
            }
            return(rNodeStack.Peek().node);
        }