示例#1
0
        public static object ObjectStringToType(StringSegment strType)
        {
            var type = ExtractType(strType);

            if (type != null)
            {
                var parseFn       = Serializer.GetParseStringSegmentFn(type);
                var propertyValue = parseFn(strType);
                return(propertyValue);
            }

            if (JsConfig.ConvertObjectTypesIntoStringDictionary && !strType.IsNullOrEmpty())
            {
                if (strType.GetChar(0) == JsWriter.MapStartChar)
                {
                    var dynamicMatch = DeserializeDictionary <TSerializer> .ParseDictionary <string, object>(strType, null, v => Serializer.UnescapeString(v).Value, v => Serializer.UnescapeString(v).Value);

                    if (dynamicMatch != null && dynamicMatch.Count > 0)
                    {
                        return(dynamicMatch);
                    }
                }

                if (strType.GetChar(0) == JsWriter.ListStartChar)
                {
                    return(DeserializeList <List <object>, TSerializer> .ParseStringSegment(strType));
                }
            }

            return((JsConfig.TryToParsePrimitiveTypeValues
                ? ParsePrimitive(strType.Value)
                : null) ?? Serializer.UnescapeString(strType).Value);
        }
示例#2
0
        internal static ParseStringSegmentDelegate GetPropertyMethod(ITypeSerializer serializer, PropertyInfo propertyInfo)
        {
            var getPropertyFn = serializer.GetParseStringSegmentFn(propertyInfo.PropertyType);

            if (propertyInfo.PropertyType == typeof(object) ||
                propertyInfo.PropertyType.HasInterface(typeof(IEnumerable <object>)))
            {
                var declaringTypeNamespace = propertyInfo.DeclaringType?.Namespace;
                if (declaringTypeNamespace == null || (!JsConfig.AllowRuntimeTypeInTypesWithNamespaces.Contains(declaringTypeNamespace) &&
                                                       !JsConfig.AllowRuntimeTypeInTypes.Contains(propertyInfo.DeclaringType.FullName)))
                {
                    return(value =>
                    {
                        var hold = JsState.IsRuntimeType;
                        try
                        {
                            JsState.IsRuntimeType = true;
                            return getPropertyFn(value);
                        }
                        finally
                        {
                            JsState.IsRuntimeType = hold;
                        }
                    });
                }
            }
            return(getPropertyFn);
        }
示例#3
0
        public static ParseStringSegmentDelegate GetParseStringSegmentMethod(Type type)
        {
            var collectionInterface = type.GetTypeWithGenericInterfaceOf(typeof(ICollection <>));

            if (collectionInterface == null)
            {
                throw new ArgumentException(string.Format("Type {0} is not of type ICollection<>", type.FullName));
            }

            //optimized access for regularly used types
            if (type.HasInterface(typeof(ICollection <string>)))
            {
                return(value => ParseStringCollection(value, type));
            }

            if (type.HasInterface(typeof(ICollection <int>)))
            {
                return(value => ParseIntCollection(value, type));
            }

            var elementType = collectionInterface.GetGenericArguments()[0];
            var supportedTypeParseMethod = Serializer.GetParseStringSegmentFn(elementType);

            if (supportedTypeParseMethod != null)
            {
                var createCollectionType = type.HasAnyTypeDefinitionsOf(typeof(ICollection <>))
                    ? null : type;

                return(value => ParseCollectionType(value, createCollectionType, elementType, supportedTypeParseMethod));
            }

            return(null);
        }
示例#4
0
 public static TypeAccessor Create(ITypeSerializer serializer, TypeConfig typeConfig, FieldInfo fieldInfo)
 {
     return(new TypeAccessor
     {
         PropertyType = fieldInfo.FieldType,
         GetProperty = serializer.GetParseStringSegmentFn(fieldInfo.FieldType),
         SetProperty = GetSetFieldMethod(typeConfig, fieldInfo),
     });
 }
示例#5
0
        public static ParseStringSpanDelegate GetParseStringSegmentMethod(Type type)
        {
            var mapInterface = type.GetTypeWithGenericInterfaceOf(typeof(KeyValuePair <,>));

            var keyValuePairArgs   = mapInterface.GetGenericArguments();
            var keyTypeParseMethod = Serializer.GetParseStringSegmentFn(keyValuePairArgs[KeyIndex]);

            if (keyTypeParseMethod == null)
            {
                return(null);
            }

            var valueTypeParseMethod = Serializer.GetParseStringSegmentFn(keyValuePairArgs[ValueIndex]);

            if (valueTypeParseMethod == null)
            {
                return(null);
            }

            var createMapType = type.HasAnyTypeDefinitionsOf(typeof(KeyValuePair <,>))
                ? null : type;

            return(value => ParseKeyValuePairType(value, createMapType, keyValuePairArgs, keyTypeParseMethod, valueTypeParseMethod));
        }
示例#6
0
        public static object ParseTuple(Type tupleType, StringSegment value)
        {
            var index = 0;

            Serializer.EatMapStartChar(value, ref index);
            if (JsonTypeSerializer.IsEmptyMap(value, index))
            {
                //return tupleType.CreateInstance();
                return(ActivatorUtils.FastCreateInstance(tupleType));
            }

            var genericArgs = tupleType.GetGenericArguments();
            var argValues   = new object[genericArgs.Length];
            var valueLength = value.Length;

            while (index < valueLength)
            {
                var keyValue = Serializer.EatMapKey(value, ref index);
                Serializer.EatMapKeySeperator(value, ref index);
                var elementValue = Serializer.EatValue(value, ref index);
                if (!keyValue.HasValue)
                {
                    continue;
                }

                var keyIndex = keyValue.Substring("Item".Length).ToInt() - 1;
                var parseFn  = Serializer.GetParseStringSegmentFn(genericArgs[keyIndex]);
                argValues[keyIndex] = parseFn(elementValue);

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            var ctor = tupleType.GetConstructors()
                       .First(x => x.GetParameters().Length == genericArgs.Length);

            return(ctor.Invoke(argValues));
        }