Exemple #1
0
 public static bool TryParseTextSerializer(string s, out ITextSerializer value)
 {
     switch (s)
     {
         case "php":
             value = new PHPSerializer();
             return true;
         case "json":
             value = new JSONSerializer();
             return true;
         case "udmf":
             value = new UDMFSerializer();
             return true;
         case "ini":
             value = new INISerializer();
             return true;
         default:
             value = null;
             return false;
     }
 }
Exemple #2
0
        public static bool TryParseKeyValuePair(string s, out KeyValuePair<string, object> kvp)
        {
            if (s.Length == 0)
            {
                kvp = default(KeyValuePair<string, object>);
                return false;
            }

            int equals = s.IndexOf('=');

            int dollar = s.IndexOf('$');

            if (dollar > equals)
                dollar = -1;

            string key;
            string typename;

            if (dollar != -1)
            {
                key = s.Substring(0, dollar);
                typename = s.Substring(dollar + 1, equals - dollar - 1);
            }
            else
            {
                key = s.Substring(0, equals == -1 ? s.Length : equals);
                typename = null;
            }

            string value;
            if (equals != -1)
            {
                value = s.Substring(equals + 1);
            }
            else
                value = null;

            if (value == null)
            {
                kvp = new KeyValuePair<string, object>(key, null);
                return true;
            }
            else if (typename == null)
            {
                kvp = new KeyValuePair<string, object>(key, value);
                return true;
            }
            else
            {
                object rvalue = null;

                string[] types =
                {
                    "string",
                    "sbyte",
                    "byte",
                    "short",
                    "ushort",
                    "int",
                    "uint",
                    "long",
                    "ulong",
                    "float",
                    "double",
                    "decimal",
                    "json",
                    "php",
                };

                var fullname = types.Where(x => x.StartsWith(typename, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

                if (fullname == null)
                    fullname = "";

                switch (fullname)
                {
                    case "string":
                        rvalue = value;
                        break;
                    case "sbyte":
                        {
                            sbyte v;
                            if (sbyte.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "byte":
                        {
                            byte v;
                            if (byte.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "short":
                        {
                            short v;
                            if (short.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "ushort":
                        {
                            ushort v;
                            if (ushort.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "int":
                        {
                            int v;
                            if (int.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "uint":
                        {
                            uint v;
                            if (uint.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "long":
                        {
                            long v;
                            if (long.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "ulong":
                        {
                            ulong v;
                            if (ulong.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "float":
                        {
                            float v;
                            if (float.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "double":
                        {
                            double v;
                            if (double.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "decimal":
                        {
                            decimal v;
                            if (decimal.TryParse(value, out v))
                                rvalue = v;
                        }
                        break;
                    case "php":
                        rvalue = new PHPSerializer().Deserialize(value);
                        break;
                    case "json":
                        rvalue = new JSONSerializer().Deserialize(value);
                        break;
                }

                if (rvalue == null)
                {
                    kvp = default(KeyValuePair<string, object>);
                    return false;
                }
                else
                {
                    kvp = new KeyValuePair<string, object>(key, rvalue);
                    return true;
                }
            }
        }