示例#1
0
        internal static object StringToType(
            Type type,
            string strType,
            EmptyCtorDelegate ctorFn,
            Dictionary <string, TypeAccessor> typeAccessorMap)
        {
            var index = 0;

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

            //if (!Serializer.EatMapStartChar(strType, ref index))
            if (strType[index++] != JsWriter.MapStartChar)
            {
                throw DeserializeTypeRef.CreateSerializationError(type, strType);
            }

            if (strType == JsWriter.EmptyMap)
            {
                return(ctorFn());
            }

            object instance = null;

            var strTypeLength = strType.Length;

            while (index < strTypeLength)
            {
                var propertyName = Serializer.EatMapKey(strType, ref index);

                //Serializer.EatMapKeySeperator(strType, ref index);
                index++;

                var propertyValueStr = Serializer.EatValue(strType, ref index);
                var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1 && propertyValueStr[0] == '_';

                if (possibleTypeInfo && propertyName == JsWriter.TypeAttr)
                {
                    var typeName = Serializer.ParseString(propertyValueStr);
                    instance = ReflectionExtensions.CreateInstance(typeName);
                    if (instance == null)
                    {
                        Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr);
                    }
                    else
                    {
                        //If __type info doesn't match, ignore it.
                        if (!type.GetTypeInfo().IsInstanceOfType(instance))
                        {
                            instance = null;
                        }
                    }

                    //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    if (index != strType.Length)
                    {
                        index++;
                    }

                    continue;
                }

                if (instance == null)
                {
                    instance = ctorFn();
                }

                TypeAccessor typeAccessor;
                typeAccessorMap.TryGetValue(propertyName, out typeAccessor);

                var propType = possibleTypeInfo ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null;
                if (propType != null)
                {
                    try
                    {
                        if (typeAccessor != null)
                        {
                            var parseFn       = Serializer.GetParseFn(propType);
                            var propertyValue = parseFn(propertyValueStr);
                            typeAccessor.SetProperty(instance, propertyValue);
                        }

                        //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                        if (index != strType.Length)
                        {
                            index++;
                        }

                        continue;
                    }
                    catch
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr);
                    }
                }

                if (typeAccessor != null && typeAccessor.GetProperty != null && typeAccessor.SetProperty != null)
                {
                    try
                    {
                        var propertyValue = typeAccessor.GetProperty(propertyValueStr);
                        typeAccessor.SetProperty(instance, propertyValue);
                    }
                    catch
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr);
                    }
                }

                //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                if (index != strType.Length)
                {
                    index++;
                }
            }

            return(instance);
        }
示例#2
0
        /// <summary>String to type.</summary>
        /// <exception cref="CreateSerializationError"> Thrown when a create serialization error error
        /// condition occurs.</exception>
        /// <exception cref="GetSerializationException">Thrown when a Get Serialization error condition
        /// occurs.</exception>
        /// <param name="type">           The type.</param>
        /// <param name="strType">        The type.</param>
        /// <param name="ctorFn">         The constructor function.</param>
        /// <param name="typeAccessorMap">The type accessor map.</param>
        /// <returns>An object.</returns>
        internal static object StringToType(
            Type type,
            string strType,
            EmptyCtorDelegate ctorFn,
            Dictionary <string, TypeAccessor> typeAccessorMap)
        {
            var index = 0;

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

            //if (!Serializer.EatMapStartChar(strType, ref index))
            if (strType[index++] != JsWriter.MapStartChar)
            {
                throw DeserializeTypeRef.CreateSerializationError(type, strType);
            }

            if (JsonTypeSerializer.IsEmptyMap(strType))
            {
                return(ctorFn());
            }

            object instance = null;

            var strTypeLength = strType.Length;

            while (index < strTypeLength)
            {
                var propertyName = Serializer.EatMapKey(strType, ref index);

                //Serializer.EatMapKeySeperator(strType, ref index);
                index++;

                var propertyValueStr = Serializer.EatValue(strType, ref index);
                var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1;

                if (possibleTypeInfo && propertyName == JsWriter.TypeAttr)
                {
                    var explicitTypeName = Serializer.ParseString(propertyValueStr);
                    var explicitType     = AssemblyUtils.FindType(explicitTypeName);

                    if (explicitType != null && !explicitType.IsInterface() && !explicitType.IsAbstract())
                    {
                        instance = explicitType.CreateInstance();
                    }

                    if (instance == null)
                    {
                        Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr);
                    }
                    else
                    {
                        //If __type info doesn't match, ignore it.
                        if (!type.InstanceOfType(instance))
                        {
                            instance = null;
                        }
                        else
                        {
                            var derivedType = instance.GetType();
                            if (derivedType != type)
                            {
                                var derivedTypeConfig = new TypeConfig(derivedType);
                                var map = DeserializeTypeRef.GetTypeAccessorMap(derivedTypeConfig, Serializer);
                                if (map != null)
                                {
                                    typeAccessorMap = map;
                                }
                            }
                        }
                    }

                    //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    if (index != strType.Length)
                    {
                        index++;
                    }

                    continue;
                }

                if (instance == null)
                {
                    instance = ctorFn();
                }

                TypeAccessor typeAccessor;
                typeAccessorMap.TryGetValue(propertyName, out typeAccessor);

                var propType = possibleTypeInfo && propertyValueStr[0] == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null;
                if (propType != null)
                {
                    try
                    {
                        if (typeAccessor != null)
                        {
                            var parseFn       = Serializer.GetParseFn(propType);
                            var propertyValue = parseFn(propertyValueStr);
                            typeAccessor.SetProperty(instance, propertyValue);
                        }

                        //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                        if (index != strType.Length)
                        {
                            index++;
                        }

                        continue;
                    }
                    catch (Exception e)
                    {
                        if (JsConfig.ThrowOnDeserializationError)
                        {
                            throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, propType, e);
                        }
                        else
                        {
                            Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr);
                        }
                    }
                }

                if (typeAccessor != null && typeAccessor.GetProperty != null && typeAccessor.SetProperty != null)
                {
                    try
                    {
                        var propertyValue = typeAccessor.GetProperty(propertyValueStr);
                        typeAccessor.SetProperty(instance, propertyValue);
                    }
                    catch (Exception e)
                    {
                        if (JsConfig.ThrowOnDeserializationError)
                        {
                            throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, propType, e);
                        }
                        else
                        {
                            Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr);
                        }
                    }
                }

                //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                if (index != strType.Length)
                {
                    index++;
                }
            }

            return(instance);
        }
        internal static object StringToType(
            TypeConfig typeConfig,
            string strType,
            EmptyCtorDelegate ctorFn,
            Dictionary <string, TypeAccessor> typeAccessorMap)
        {
            var index = 0;
            var type  = typeConfig.Type;

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

            //if (!Serializer.EatMapStartChar(strType, ref index))
            if (strType[index++] != JsWriter.MapStartChar)
            {
                throw DeserializeTypeRef.CreateSerializationError(type, strType);
            }

            if (JsonTypeSerializer.IsEmptyMap(strType))
            {
                return(ctorFn());
            }

            object instance = null;

            var propertyResolver = JsConfig.PropertyConvention == PropertyConvention.Lenient
                ? ParseUtils.LenientPropertyNameResolver
                : ParseUtils.DefaultPropertyNameResolver;

            var strTypeLength = strType.Length;

            while (index < strTypeLength)
            {
                var propertyName = Serializer.EatMapKey(strType, ref index);

                //Serializer.EatMapKeySeperator(strType, ref index);
                index++;

                var propertyValueStr = Serializer.EatValue(strType, ref index);
                var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1;

                if (possibleTypeInfo && propertyName == JsWriter.TypeAttr)
                {
                    var explicitTypeName = Serializer.ParseString(propertyValueStr);
                    var explicitType     = JsConfig.TypeFinder(explicitTypeName);

                    if (explicitType == null || explicitType.IsInterface() || explicitType.IsAbstract())
                    {
                        Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr);
                    }
                    else if (!type.IsAssignableFromType(explicitType))
                    {
                        Tracer.Instance.WriteWarning("Could not assign type: " + propertyValueStr);
                    }
                    else
                    {
                        JsWriter.AssertAllowedRuntimeType(explicitType);
                        instance = explicitType.CreateInstance();
                    }

                    if (instance != null)
                    {
                        //If __type info doesn't match, ignore it.
                        if (!type.InstanceOfType(instance))
                        {
                            instance = null;
                        }
                        else
                        {
                            var derivedType = instance.GetType();
                            if (derivedType != type)
                            {
                                var derivedTypeConfig = new TypeConfig(derivedType);
                                var map = DeserializeTypeRef.GetTypeAccessorMap(derivedTypeConfig, Serializer);
                                if (map != null)
                                {
                                    typeAccessorMap = map;
                                }
                            }
                        }
                    }

                    //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    if (index != strType.Length)
                    {
                        index++;
                    }

                    continue;
                }

                if (instance == null)
                {
                    instance = ctorFn();
                }

                var typeAccessor = propertyResolver.GetTypeAccessorForProperty(propertyName, typeAccessorMap);

                var propType = possibleTypeInfo && propertyValueStr[0] == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null;
                if (propType != null)
                {
                    try
                    {
                        if (typeAccessor != null)
                        {
                            var parseFn       = Serializer.GetParseFn(propType);
                            var propertyValue = parseFn(propertyValueStr);
                            if (typeConfig.OnDeserializing != null)
                            {
                                propertyValue = typeConfig.OnDeserializing(instance, propertyName, propertyValue);
                            }
                            typeAccessor.SetProperty(instance, propertyValue);
                        }

                        //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                        if (index != strType.Length)
                        {
                            index++;
                        }

                        continue;
                    }
                    catch (Exception e)
                    {
                        if (JsConfig.OnDeserializationError != null)
                        {
                            JsConfig.OnDeserializationError(instance, propType, propertyName, propertyValueStr, e);
                        }
                        if (JsConfig.ThrowOnDeserializationError)
                        {
                            throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, propType, e);
                        }
                        else
                        {
                            Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr);
                        }
                    }
                }

                if (typeAccessor != null && typeAccessor.GetProperty != null && typeAccessor.SetProperty != null)
                {
                    try
                    {
                        var propertyValue = typeAccessor.GetProperty(propertyValueStr);
                        if (typeConfig.OnDeserializing != null)
                        {
                            propertyValue = typeConfig.OnDeserializing(instance, propertyName, propertyValue);
                        }
                        typeAccessor.SetProperty(instance, propertyValue);
                    }
                    catch (NotSupportedException) { throw; }
                    catch (Exception e)
                    {
                        if (JsConfig.OnDeserializationError != null)
                        {
                            JsConfig.OnDeserializationError(instance, propType ?? typeAccessor.PropertyType, propertyName, propertyValueStr, e);
                        }
                        if (JsConfig.ThrowOnDeserializationError)
                        {
                            throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, propType, e);
                        }
                        else
                        {
                            Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr);
                        }
                    }
                }
                else if (typeConfig.OnDeserializing != null)
                {
                    // the property is not known by the DTO
                    typeConfig.OnDeserializing(instance, propertyName, propertyValueStr);
                }

                //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                if (index != strType.Length)
                {
                    index++;
                }
            }

            return(instance);
        }
示例#4
0
    /// <summary>
    /// Strings to type.
    /// </summary>
    /// <param name="strType">Type of the string.</param>
    /// <param name="typeConfig">The type configuration.</param>
    /// <param name="ctorFn">The ctor function.</param>
    /// <param name="typeAccessors">The type accessors.</param>
    /// <returns>System.Object.</returns>
    internal static object StringToType(ReadOnlySpan <char> strType,
                                        TypeConfig typeConfig,
                                        EmptyCtorDelegate ctorFn,
                                        KeyValuePair <string, TypeAccessor>[] typeAccessors)
    {
        var index = 0;
        var type  = typeConfig.Type;

        if (strType.IsEmpty)
        {
            return(null);
        }

        //if (!Serializer.EatMapStartChar(strType, ref index))
        if (strType[index++] != JsWriter.MapStartChar)
        {
            throw DeserializeTypeRef.CreateSerializationError(type, strType.ToString());
        }

        if (JsonTypeSerializer.IsEmptyMap(strType))
        {
            return(ctorFn());
        }

        var config = JsConfig.GetConfig();

        object instance = null;
        var    lenient  = config.PropertyConvention == PropertyConvention.Lenient || config.TextCase == TextCase.SnakeCase;

        var strTypeLength = strType.Length;

        while (index < strTypeLength)
        {
            var propertyName = Serializer.EatMapKey(strType, ref index).Trim();

            //Serializer.EatMapKeySeperator(strType, ref index);
            index++;

            var propertyValueStr = Serializer.EatValue(strType, ref index);
            var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1;

            if (possibleTypeInfo && propertyName.Equals(typeAttr.Span, StringComparison.OrdinalIgnoreCase))
            {
                var explicitTypeName = Serializer.ParseString(propertyValueStr);
                var explicitType     = config.TypeFinder(explicitTypeName);

                if (explicitType == null || explicitType.IsInterface || explicitType.IsAbstract)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr.ToString());
                }
                else if (!type.IsAssignableFrom(explicitType))
                {
                    Tracer.Instance.WriteWarning("Could not assign type: " + propertyValueStr.ToString());
                }
                else
                {
                    JsWriter.AssertAllowedRuntimeType(explicitType);
                    instance = explicitType.CreateInstance();
                }

                if (instance != null)
                {
                    //If __type info doesn't match, ignore it.
                    if (!type.IsInstanceOfType(instance))
                    {
                        instance = null;
                    }
                    else
                    {
                        var derivedType = instance.GetType();
                        if (derivedType != type)
                        {
                            var map = DeserializeTypeRef.GetCachedTypeAccessors(derivedType, Serializer);
                            if (map != null)
                            {
                                typeAccessors = map;
                            }
                        }
                    }
                }

                //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                if (index != strType.Length)
                {
                    index++;
                }

                continue;
            }

            instance ??= ctorFn();

            var typeAccessor = typeAccessors.Get(propertyName, lenient);

            var propType = possibleTypeInfo && propertyValueStr[0] == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null;
            if (propType != null)
            {
                try
                {
                    if (typeAccessor != null)
                    {
                        var parseFn       = Serializer.GetParseStringSpanFn(propType);
                        var propertyValue = parseFn(propertyValueStr);
                        if (typeConfig.OnDeserializing != null)
                        {
                            propertyValue = typeConfig.OnDeserializing(instance, propertyName.ToString(), propertyValue);
                        }
                        typeAccessor.SetProperty(instance, propertyValue);
                    }

                    //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    if (index != strType.Length)
                    {
                        index++;
                    }

                    continue;
                }
                catch (Exception e)
                {
                    config.OnDeserializationError?.Invoke(instance, propType, propertyName.ToString(), propertyValueStr.ToString(), e);
                    if (config.ThrowOnError)
                    {
                        throw DeserializeTypeRef.GetSerializationException(propertyName.ToString(), propertyValueStr.ToString(), propType, e);
                    }
                    else
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName.ToString(), propertyValueStr.ToString());
                    }
                }
            }

            if (typeAccessor is { GetProperty : { }, SetProperty : { } })