Example #1
0
        public static ParseStringDelegate GetParseMethod(TypeConfig typeConfig)
        {
            var type = typeConfig.Type;

            var info = type.GetTypeInfo();

            if (!info.IsClass || info.IsAbstract || info.IsInterface)
            {
                return(null);
            }

            var propertyInfos = type.GetSerializableProperties();

            if (propertyInfos.Length == 0)
            {
                var emptyCtorFn = ReflectionExtensions.GetConstructorMethodToCache(type);
                return(value => emptyCtorFn());
            }

            var map = new Dictionary <string, TypeAccessor>(StringComparer.OrdinalIgnoreCase);

            var isDataContract = type.GetTypeInfo().GetCustomAttributes(typeof(DataContractAttribute), false).Any();

            foreach (var propertyInfo in propertyInfos)
            {
                var propertyName = propertyInfo.Name;
                if (isDataContract)
                {
                    var dcsDataMember = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), false).FirstOrDefault() as DataMemberAttribute;
                    if (dcsDataMember != null && dcsDataMember.Name != null)
                    {
                        propertyName = dcsDataMember.Name;
                    }
                }
                map[propertyName] = TypeAccessor.Create(Serializer, typeConfig, propertyInfo);
            }

            var ctorFn = ReflectionExtensions.GetConstructorMethodToCache(type);

            return(typeof(TSerializer) == typeof(Json.JsonTypeSerializer)
                                ? (ParseStringDelegate)(value => DeserializeTypeRefJson.StringToType(type, value, ctorFn, map))
                                : value => DeserializeTypeRefJsv.StringToType(type, value, ctorFn, map));
        }
        public static ParseStringDelegate GetParseMethod(TypeConfig typeConfig)
        {
            var type = typeConfig.Type;

            if (!type.IsStandardClass())
            {
                return(null);
            }
            var map = DeserializeTypeRef.GetTypeAccessorMap(typeConfig, Serializer);

            var ctorFn = JsConfig.ModelFactory(type);

            if (map == null)
            {
                return(value => ctorFn());
            }

            return(typeof(TSerializer) == typeof(Json.JsonTypeSerializer)
                ? (ParseStringDelegate)(value => DeserializeTypeRefJson.StringToType(type, value, ctorFn, map))
                : value => DeserializeTypeRefJsv.StringToType(type, value, ctorFn, map));
        }
        public static ParseStringDelegate GetParseMethod(TypeConfig typeConfig)
        {
            var type = typeConfig.Type;

            if (!type.IsClass || type.IsAbstract || type.IsInterface)
            {
                return(null);
            }

            var map    = DeserializeTypeRef.GetTypeAccessorMap(typeConfig, Serializer);
            var ctorFn = ReflectionExtensions.GetConstructorMethodToCache(type);

            if (map == null)
            {
                return(value => ctorFn());
            }

            return(typeof(TSerializer) == typeof(Json.JsonTypeSerializer)
                                ? (ParseStringDelegate)(value => DeserializeTypeRefJson.StringToType(type, value, ctorFn, map))
                                : value => DeserializeTypeRefJsv.StringToType(type, value, ctorFn, map));
        }
 internal object DeserializeJson(ReadOnlySpan <char> value) => DeserializeTypeRefJson.StringToType(value, typeConfig, ctorFn, accessors);