Пример #1
0
        void IRpcJson.ReadRpcJson(ref Utf8JsonReader p_reader)
        {
            p_reader.Read();
            MethodName = p_reader.GetString();

            // 参数外层 [
            p_reader.Read();
            Params = new List <object>();
            while (p_reader.Read())
            {
                // 参数外层 ]
                if (p_reader.TokenType == JsonTokenType.EndArray)
                {
                    break;
                }
                Params.Add(JsonRpcSerializer.Deserialize(ref p_reader));
            }

            p_reader.Read();
            Title = p_reader.GetString();
            p_reader.Read();
            Content = p_reader.GetString();

            // 最外层 ]
            p_reader.Read();
        }
Пример #2
0
        /// <summary>
        /// 按照系统规则反序列化json串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="p_content"></param>
        /// <returns></returns>
        public static T Deserialize <T>(string p_content)
        {
            Throw.IfNullOrEmpty(p_content);

            Utf8JsonReader reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(p_content));

            reader.Read();
            return(JsonRpcSerializer.Deserialize <T>(ref reader));
        }
Пример #3
0
Файл: Dict.cs Проект: Daoting/dt
        void IRpcJson.ReadRpcJson(ref Utf8JsonReader p_reader)
        {
            //[
            //	"#dict",
            //	["key1", "类型", "val1"], // 简单类型System.XXX
            //	["key2", "Int64", 11],
            //	["key3", "Byte[]", "CgwOEA=="],
            //	["key4", "", ["#row",...]] // 复杂类型空即可
            //]

            // 项起始 [
            while (p_reader.Read())
            {
                // 外层末尾 ]
                if (p_reader.TokenType == JsonTokenType.EndArray)
                {
                    break;
                }

                // 项
                p_reader.Read();
                string key = p_reader.GetString();
                p_reader.Read();
                string tpName = p_reader.GetString();
                p_reader.Read();
                if (tpName == string.Empty)
                {
                    try
                    {
                        this[key] = JsonRpcSerializer.Deserialize(ref p_reader);
                    }
                    catch (Exception exception)
                    {
                        throw new Exception("反序列化Dict时异常: " + exception.Message);
                    }
                }
                else
                {
                    Type tp;
                    if (tpName.EndsWith("?"))
                    {
                        tp = Type.GetType("System." + tpName.TrimEnd('?'), true, false);
                        tp = typeof(Nullable <>).MakeGenericType(tp);
                    }
                    else
                    {
                        tp = Type.GetType("System." + tpName, true, false);
                    }

                    try
                    {
                        this[key] = JsonRpcSerializer.Deserialize(ref p_reader, tp);
                    }
                    catch (Exception exception)
                    {
                        throw new Exception("反序列化Dict时异常: " + exception.Message);
                    }
                }
                // 项末尾 ]
                p_reader.Read();
            }
        }
Пример #4
0
        /// <summary>
        /// 反序列化读取Rpc Json数据
        /// </summary>
        void IRpcJson.ReadRpcJson(ref Utf8JsonReader p_reader)
        {
            // Entity类型
            Type tpEntity = null;

            if (GetType().IsGenericType)
            {
                tpEntity = GetType().GetGenericArguments()[0];
            }

            // cols外层 [
            p_reader.Read();
            // 列[
            while (p_reader.Read())
            {
                // cols外层 ]
                if (p_reader.TokenType == JsonTokenType.EndArray)
                {
                    break;
                }

                int    index   = 0;
                string colName = null;
                Type   colType = null;

                // 列 [
                while (p_reader.Read())
                {
                    // 列 ]
                    if (p_reader.TokenType == JsonTokenType.EndArray)
                    {
                        break;
                    }

                    if (index == 0)
                    {
                        colName = p_reader.GetString();
                    }
                    else
                    {
                        colType = GetColType(p_reader.GetString());
                        if (colType == typeof(byte) && tpEntity != null)
                        {
                            // Entity 时根据属性类型将 byte 自动转为 enum 类型
                            var prop = tpEntity.GetProperty(colName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.IgnoreCase);
                            if (prop != null)
                            {
                                colType = prop.PropertyType;
                            }
                        }
                    }
                    index++;
                }
                _columns.Add(new Column(colName, colType));
            }

            // rows外层 [
            p_reader.Read();
            int colCnt = _columns.Count;

            // [
            while (p_reader.Read())
            {
                // rows外层 ]
                if (p_reader.TokenType == JsonTokenType.EndArray)
                {
                    break;
                }

                int index = 0;
                Row row   = AddRow();
                row.IsAdded = false;
                // 行 [
                while (p_reader.Read())
                {
                    // 行 ]
                    if (p_reader.TokenType == JsonTokenType.EndArray)
                    {
                        break;
                    }

                    if (index < colCnt)
                    {
                        // 值变化时两值 [原始值,当前值]
                        if (p_reader.TokenType == JsonTokenType.StartArray)
                        {
                            // 两值 [
                            p_reader.Read();
                            row.Cells[index].OriginalVal = JsonRpcSerializer.Deserialize(ref p_reader, _columns[index].Type);
                            p_reader.Read();
                            row.Cells[index].SetVal(JsonRpcSerializer.Deserialize(ref p_reader, _columns[index].Type));
                            // 两值 ]
                            p_reader.Read();
                        }
                        else
                        {
                            // 不会引起连续IsChanged改变
                            row.Cells[index].InitVal(JsonRpcSerializer.Deserialize(ref p_reader, _columns[index].Type));
                        }
                    }
                    else
                    {
                        // 超出的列值为行状态
                        string state = p_reader.GetString();
                        if (state == "Added")
                        {
                            row.IsAdded = true;
                        }
                        else if (state == "Modified")
                        {
                            row.IsChanged = true;
                        }
                    }
                    index++;
                }
            }

            // 最外层 ]
            p_reader.Read();
        }
Пример #5
0
Файл: Row.cs Проект: Daoting/dt
        void IRpcJson.ReadRpcJson(ref Utf8JsonReader p_reader)
        {
            try
            {
                // 可能为状态或 {
                p_reader.Read();
                // 状态
                if (p_reader.TokenType == JsonTokenType.String)
                {
                    string state = p_reader.GetString();
                    if (state == "Added")
                    {
                        IsAdded = true;
                    }
                    else if (state == "Modified")
                    {
                        IsChanged = true;
                    }
                    // {
                    p_reader.Read();
                }

                bool isEntity = GetType().IsSubclassOf(typeof(Entity));
                while (p_reader.Read() && p_reader.TokenType == JsonTokenType.PropertyName)
                {
                    string id = p_reader.GetString();
                    p_reader.Read();
                    if (p_reader.TokenType == JsonTokenType.StartArray)
                    {
                        // 数组内容: ["类型", "当前值", "原始值"] 或 ["类型", "当前值"]

                        // 类型
                        p_reader.Read();
                        Type type = Table.GetColType(p_reader.GetString());
                        if (type == typeof(byte) && isEntity)
                        {
                            // Entity 时根据属性类型将 byte 自动转为 enum 类型
                            var prop = GetType().GetProperty(id, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.IgnoreCase);
                            if (prop != null)
                            {
                                type = prop.PropertyType;
                            }
                        }

                        // 当前值
                        p_reader.Read();
                        object val = JsonRpcSerializer.Deserialize(ref p_reader, type);

                        p_reader.Read();
                        if (p_reader.TokenType == JsonTokenType.EndArray)
                        {
                            // 非string类型,值无变化 ["类型", "当前值"]
                            new Cell(this, id, type, val);
                        }
                        else
                        {
                            // 值变化时传递完整信息 ["类型", "当前值", "原始值"]
                            new Cell(this, id, type, JsonRpcSerializer.Deserialize(ref p_reader, type)).SetVal(val);
                            // ]
                            p_reader.Read();
                        }
                    }
                    else
                    {
                        // string类型,值无变化
                        new Cell(this, id, typeof(string), JsonRpcSerializer.Deserialize(ref p_reader));
                    }
                }
                // 最外层 ]
                p_reader.Read();
                AttachHook();
            }
            catch (Exception ex)
            {
                throw new Exception($"反序列化Row时异常: {ex.Message}");
            }
        }