コード例 #1
0
        protected static T InternalDeserialize <T>(Dictionary <string, string> keyval)
        {
            Dictionary <string, object> root = new Dictionary <string, object>();

            foreach (KeyValuePair <string, string> k in keyval)
            {
                string   itemKey = k.Key.Replace("\"", "");
                string[] parts   = itemKey.Split('.');
                object   current = root;
                int      counter = 0;
                foreach (string p in parts)
                {
                    counter++;
                    // last one
                    if (counter == parts.Length)
                    {
                        // an empty list;
                        if (k.Value == "[]")
                        {
                            if (current is Dictionary <string, object> )
                            {
                                (current as Dictionary <string, object>).Add(p, new List <int>());
                            }
                            else if (current is List <object> )
                            {
                                (current as List <object>).Add(new List <int>());
                            }
                        }
                        // an empty object;
                        else if (k.Value == "{}")
                        {
                            if (current is Dictionary <string, object> )
                            {
                                (current as Dictionary <string, object>).Add(p, new Dictionary <string, object>());
                            }
                            else if (current is List <object> )
                            {
                                (current as List <object>).Add(new Dictionary <string, object>());
                            }
                        }
                        // a value
                        else
                        {
                            object value    = null;
                            string lowerval = k.Value.ToLower();
                            if (lowerval == "true")
                            {
                                value = true;
                            }
                            else if (lowerval == "false")
                            {
                                value = false;
                            }
                            else if (lowerval == "null")
                            {
                                value = null;
                            }
                            else if (Misc.IsInt(lowerval))
                            {
                                value = Int32.Parse(lowerval);
                            }
                            else if (Misc.IsDouble(lowerval))
                            {
                                value = Double.Parse(lowerval);
                            }
                            else
                            {
                                string strval = k.Value;
                                if (strval.StartsWith("\""))
                                {
                                    strval = strval.Substring(1);
                                }
                                if (strval.EndsWith("\""))
                                {
                                    strval = strval.Substring(0, strval.Length - 1);
                                }
                                value = strval;
                            }
                            if (current is Dictionary <string, object> )
                            {
                                (current as Dictionary <string, object>).Add(p, value);
                            }
                            else if (current is List <object> )
                            {
                                (current as List <object>).Add(value);
                            }
                        }
                    }
                    // in middle, .. key.key ..
                    // current is dict, child is dict
                    else if (Misc.TryParseInt(p, -1) == -1 && Misc.TryParseInt(parts[counter], -1) == -1)
                    {
                        // current must be a dict in this situation...
                        // x.y.0.z.w,5
                        // {
                        //  x: {
                        //   y: [
                        //    {
                        //     z : {
                        //      w : 5;
                        Dictionary <string, object> dcurrent = current as Dictionary <string, object>;
                        if (dcurrent.ContainsKey(p))
                        {
                            current = dcurrent[p];
                        }
                        else
                        {
                            dcurrent.Add(p, new Dictionary <string, object>());
                            current = dcurrent[p];
                        }
                    }
                    // in middle, .. key.number ...
                    // current is dict, child is list
                    else if (Misc.TryParseInt(p, -1) == -1 && Misc.TryParseInt(parts[counter], -1) >= 0)
                    {
                        Dictionary <string, object> dcurrent = current as Dictionary <string, object>;
                        if (dcurrent.ContainsKey(p))
                        {
                            current = dcurrent[p];
                        }
                        else
                        {
                            dcurrent.Add(p, new List <object>());
                            current = dcurrent[p];
                        }
                    }
                    // in middle, .. number.key ..
                    // current is list, child is dict
                    else if (Misc.TryParseInt(p, -1) >= 0 && Misc.TryParseInt(parts[counter], -1) == -1)
                    {
                        int           index    = Misc.TryParseInt(p, -1);
                        List <object> lcurrent = current as List <object>;
                        if (lcurrent.Count > index)
                        {
                            current = lcurrent[index];
                        }
                        else
                        {
                            for (int i = lcurrent.Count; i < index + 1; i++)
                            {
                                lcurrent.Add(new Dictionary <string, object>());
                            }
                            current = lcurrent[index];
                        }
                    }
                    // in middle, .. number.number ..
                    // current is list, child is list
                    else if (Misc.TryParseInt(p, -1) >= 0 && Misc.TryParseInt(parts[counter], -1) >= 0)
                    {
                        int           index    = Misc.TryParseInt(p, -1);
                        List <object> lcurrent = current as List <object>;
                        if (lcurrent.Count > index)
                        {
                            current = lcurrent[index];
                        }
                        else
                        {
                            for (int i = lcurrent.Count; i < index + 1; i++)
                            {
                                lcurrent.Add(new List <object>());
                            }
                            current = lcurrent[index];
                        }
                    }
                }
            }

            string json = JsonObjectSerializer.Serialize(root);

            return(JsonObjectSerializer.Deserialize <T>(json));
        }