private ItemConfigKeyVal Convert(ItemConfig item)
        {
            if (item.ConfigValueType != ConfigValueTypes.OBJECT)
            {
                return new ItemConfigKeyVal {
                           Key = item.Key, Value = item.Value
                }
            }
            ;

            var list = new List <ItemConfigKeyVal>();

            foreach (var itemCf in item.ValueItems)
            {
                list.Add(Convert(itemCf));
            }
            return(new ItemConfigKeyVal {
                Key = item.Key, Value = list
            });
        }
示例#2
0
        private ItemConfig Decrypt(ItemConfig item, string secureKey, bool isFirstLoop = false)
        {
            var itemLocal = item;

            switch (item.ConfigValueType)
            {
            case ConfigValueTypes.STRING:
                var key = isFirstLoop ? FindKey(item.Key, secureKey, isFirstLoop) : secureKey;
                if (SecurityUtil.TryDecrypt(item.Value, key, out string txtDecrypt))
                {
                    itemLocal.SetDecrypt(txtDecrypt);
                    return(itemLocal);
                }
                break;

            case ConfigValueTypes.OBJECT:
                var res        = new List <ItemConfig>();
                var primaryKey = secureKey;
                if (isFirstLoop)
                {
                    primaryKey  = FindKey(item.Key, secureKey, isFirstLoop);
                    isFirstLoop = false;
                }

                foreach (var itemCf in item.ValueItems)
                {
                    //item.Key + ":" +
                    key = FindKey(itemCf.Key, primaryKey, isFirstLoop);
                    // những lần sau thì mặc định isFirstLoop=false để chỉ dùng format <pathKey>:<key>,
                    // với pathKey là key được cộng dồn cho tới thời điểm hiện tại
                    res.Add(Decrypt(itemCf, key));
                }

                itemLocal.SetDecrypt(res);

                return(itemLocal);

            case ConfigValueTypes.ARRAY:

                var resArrays = new List <ItemArrayConfig>();
                foreach (var itemArray in item.ValueArrayItems)
                {
                    var tmpItemConfigs = new List <ItemConfig>();
                    foreach (var itemCf in itemArray.ValueItems)
                    {
                        key = FindKey(itemCf.Key, secureKey, isFirstLoop);
                        tmpItemConfigs.Add(Decrypt(itemCf, key));
                    }

                    // những lần sau thì mặc định isFirstLoop=false để chỉ dùng format <pathKey>:<key>,
                    // với pathKey là key được cộng dồn cho tới thời điểm hiện tại
                    resArrays.Add(new ItemArrayConfig(tmpItemConfigs));
                }

                itemLocal.SetDecrypt(resArrays);

                return(itemLocal);

            default:
                return(itemLocal);
            }
            return(itemLocal);
        }
        private string toJson(ItemConfig item, bool isFlag = false)
        {
            switch (item.ConfigValueType)
            {
            case ConfigValueTypes.STRING:
                switch (item.TypeCode)
                {
                case TypeCode.Byte:        //array
                    return($" \"{item.Key}\":{item.Value},");

                case TypeCode.Double:
                    return($" \"{item.Key}\":{item.Value},");

                case TypeCode.Int32:
                    return($" \"{item.Key}\":{item.Value},");

                case TypeCode.String:
                    return($" \"{item.Key}\": \"{item.Value}\",");

                case TypeCode.Boolean:
                    return($" \"{item.Key}\":{item.Value.ToLower()},");

                default:
                    return("");
                }

            case ConfigValueTypes.OBJECT:
                var txt    = $"\"{item.Key}\":" + "{";
                int icount = item.ValueItems.Count();
                foreach (var itemCf in item.ValueItems)
                {
                    var ji = toJson(itemCf);
                    if (ji.Length != 0)
                    {
                        icount--;
                        if (icount == 0)
                        {
                            ji = ji.Substring(0, ji.Length - 1);
                        }
                    }
                    txt += ji;
                }
                txt += "},";
                return(txt);

            case ConfigValueTypes.ARRAY:
                var txtJson = $"\"{item.Key}\":" + "[";

                var arrayCount = item.ValueArrayItems.Count();
                foreach (var itemArray in item.ValueArrayItems)
                {
                    icount   = itemArray.ValueItems.Count();
                    txtJson += "{";
                    foreach (var itemCf in itemArray.ValueItems)
                    {
                        var ji = toJson(itemCf);
                        icount--;
                        if (ji.Length != 0)
                        {
                            if (icount == 0)
                            {
                                ji = ji.Substring(0, ji.Length - 1);
                            }
                        }
                        txtJson += ji;
                    }
                    txtJson += "},";
                    arrayCount--;
                    if (txtJson.Length != 0)
                    {
                        if (arrayCount == 0)
                        {
                            txtJson.Substring(0, txtJson.Length - 1);
                        }
                    }
                }
                txtJson += "],";
                return(txtJson);

            default:
                return(string.Empty);
            }
        }