Example #1
0
        public static ExploreProtocol Create(ExploreProtocolDataType type, string jsonStr, string context = "")
        {
            try
            {
                var node = JsonMapper.ToObject(jsonStr);
                if (node != null)
                {
                    int device = -1;
                    if (node.ContainsKey("device"))
                    {
                        device = (int)node["device"];
                    }

                    int mode = -1;
                    if (node.ContainsKey("mode"))
                    {
                        mode = (int)node["mode"];
                    }

                    int id = -1;
                    if (node.ContainsKey("id"))
                    {
                        id = (int)node["id"];
                    }

                    int code = -1;
                    if (node.ContainsKey("code"))
                    {
                        code = (int)node["code"];
                    }

                    string uuid = string.Empty;
                    if (node.ContainsKey("uuid"))
                    {
                        uuid = ((string)node["uuid"]);                        //.ToLower();
                    }
                    bool debug = false;
                    if (node.ContainsKey("debug"))
                    {
                        debug = (int)node["debug"] == 1;
                    }

                    ExploreProtocol p = new ExploreProtocol();
                    p.dataType = type;
                    p.device   = device;
                    p.mode     = mode;
                    p.id       = id;
                    p.code     = code;
                    p.uuid     = uuid;
                    p.debug    = debug;

                    if (node.ContainsKey("data"))
                    {
                        var datas = node["data"];                         // it's array of int
                        switch (type)
                        {
                        case ExploreProtocolDataType.DTObject:
                        {
                            p.datas = LitJsonHelper.ParseJsonArray(datas, jsonParam =>
                                {
                                    LitJsonHelper.SafeConvert(jsonParam, out object result);
                                    return(result);
                                }, ArrayHelper <object> .Empty);
                            break;
                        }

                        case ExploreProtocolDataType.DTInt:
                        {
                            p.datas = LitJsonHelper.ParseJsonArray(datas, jsonParam =>
                                {
                                    LitJsonHelper.SafeConvert(jsonParam, out int result);
                                    return(result);
                                }, ArrayHelper <int> .Empty);
                            break;
                        }

                        case ExploreProtocolDataType.DTFloat:
                        {
                            p.datas = LitJsonHelper.ParseJsonArray(datas, jsonParam =>
                                {
                                    LitJsonHelper.SafeConvert(jsonParam, out float result);
                                    return(result);
                                }, ArrayHelper <float> .Empty);
                            break;
                        }
                        }
                    }
                    return(p);
                }
            }
            catch (System.Exception ex)
            {
                DebugUtility.LogError(LoggerTags.Module, "Failure to create json data , Context : {0}, Stack : {1}", context, ex.StackTrace);
            }
            return(null);
        }
Example #2
0
        public static ExploreProtocol Create(ExploreProtocolDataType type, byte[] jsonData, int offset, int length, bool encryption, string context = "")
        {
            string json = Encoding.UTF8.GetString(jsonData, offset, length);

            return(Create(type, json, context));
        }