コード例 #1
0
        public object Parse(IDictionary <string, string> keyValuePairs, Type returnType)
        {
            StringMapTypeDeserializer stringMapTypeDeserializer;

            lock (typeStringMapSerializerMap)
            {
                if (!typeStringMapSerializerMap.TryGetValue(returnType, out stringMapTypeDeserializer))
                {
                    stringMapTypeDeserializer = new StringMapTypeDeserializer(returnType);
                    typeStringMapSerializerMap.Add(returnType, stringMapTypeDeserializer);
                }
            }

            return(stringMapTypeDeserializer.CreateFromMap(keyValuePairs));
        }
コード例 #2
0
ファイル: RestPath.cs プロジェクト: namman/ServiceStack
        public RestPath(Type requestType, string path, string verbs, string summary = null, string notes = null)
        {
            this.RequestType = requestType;
            this.Summary = summary;
            this.Notes = notes;
            this.restPath = path;

            this.allowsAllVerbs = verbs == null || verbs == WildCard;
            if (!this.allowsAllVerbs)
            {
                this.allowedVerbs = verbs.ToUpper();
            }

            var componentsList = new List<string>();

            //We only split on '.' if the restPath has them. Allows for /{action}.{type}
            var hasSeparators = new List<bool>();
            foreach (var component in this.restPath.Split(PathSeperatorChar))
            {
                if (string.IsNullOrEmpty(component)) continue;

                if (component.Contains(VariablePrefix)
                    && component.Contains(ComponentSeperator))
                {
                    hasSeparators.Add(true);
                    componentsList.AddRange(component.Split(ComponentSeperator));
                }
                else
                {
                    hasSeparators.Add(false);
                    componentsList.Add(component);
                }
            }

            var components = componentsList.ToArray();
            this.TotalComponentsCount = components.Length;

            this.literalsToMatch = new string[this.TotalComponentsCount];
            this.variablesNames = new string[this.TotalComponentsCount];
            this.componentsWithSeparators = hasSeparators.ToArray();
            this.PathComponentsCount = this.componentsWithSeparators.Length;
            string firstLiteralMatch = null;
            var lastVariableMatchPos = -1;

            var sbHashKey = new StringBuilder();
            for (var i = 0; i < components.Length; i++)
            {
                var component = components[i];

                if (component.StartsWith(VariablePrefix))
                {
                    this.variablesNames[i] = component.Substring(1, component.Length - 2);
                    this.variableArgsCount++;
                    lastVariableMatchPos = i;
                }
                else
                {
                    this.literalsToMatch[i] = component.ToLower();
                    sbHashKey.Append(i + PathSeperatorChar.ToString() + this.literalsToMatch);

                    if (firstLiteralMatch == null)
                    {
                        firstLiteralMatch = this.literalsToMatch[i];
                    }
                }
            }

            if (lastVariableMatchPos != -1)
            {
                var lastVariableMatch = this.variablesNames[lastVariableMatchPos];
                this.isWildCardPath = lastVariableMatch[lastVariableMatch.Length - 1] == WildCardChar;
                if (this.isWildCardPath)
                {
                    this.variablesNames[lastVariableMatchPos] = lastVariableMatch.Substring(0, lastVariableMatch.Length - 1);
                }
            }

            this.FirstMatchHashKey = !this.isWildCardPath
                ? this.PathComponentsCount + PathSeperator + firstLiteralMatch
                : WildCardChar + PathSeperator + firstLiteralMatch;

            this.IsValid = sbHashKey.Length > 0;
            this.UniqueMatchHashKey = sbHashKey.ToString();

            this.typeDeserializer = new StringMapTypeDeserializer(this.RequestType);
            RegisterCaseInsenstivePropertyNameMappings();
        }