Exemplo n.º 1
0
        public GenericStructSerializationInfo(TypeDefinition definition, CustomFormatterTypeInfo customFormatter, MethodDefinition serializationConstructorDefinition, GenericInstanceType[] definitionVariations)
#endif
        {
            Definition                          = definition;
            CustomFormatter                     = customFormatter;
            FieldInfos                          = Array.Empty <FieldSerializationInfo>();
            PropertyInfos                       = Array.Empty <PropertySerializationInfo>();
            StringKeyValuePairs                 = Array.Empty <StringKeySerializationInfoTuple>();
            MinIntKey                           = 0;
            MaxIntKey                           = 0;
            SerializationConstructor            = serializationConstructorDefinition;
            DefinitionGenericInstanceVariations = definitionVariations;

            AreAllMessagePackPrimitive = FieldInfos.All(x => x.IsMessagePackPrimitive) && PropertyInfos.All(x => x.IsMessagePackPrimitive);
            PublicAccessible           = PublicTypeTestUtility.IsPublicType(definition) && FieldInfos.All(x => x.PublicAccessible) && PropertyInfos.All(x => x.PublicAccessible);
        }
Exemplo n.º 2
0
        internal static List <IPatchBase> ReadPatchDocumentProperties <T>(JsonReader reader, string path,
                                                                          BindAttribute bindAttribute        = null, Action <JsonReader> patchTypeAction = null,
                                                                          Action <IPatchPrimitive> keyAction = null)
        {
            if (reader == null)
            {
                return(null);
            }

            var          modelTypeAssemblyName = typeof(T).AssemblyQualifiedName;
            bool         cacheProperties;
            const string patchTypeKey = PatchArrayDocument <object> .PatchTypeKey;

            // ReSharper disable once AssignmentInConditionalExpression
            var modelProperties = (cacheProperties = PropertyInfos.All(x => x.Key.Key1 != modelTypeAssemblyName))
                ? typeof(T).GetTypeInfo().GetProperties()
                : PropertyInfos.Where(x => x.Key.Key1 == modelTypeAssemblyName).Select(x => x.Value).ToArray();

            var values    = new List <IPatchBase>();
            var startPath = reader.Path;

            bindAttribute = bindAttribute ?? typeof(T).GetAttribute <BindAttribute>();
            path          = string.IsNullOrWhiteSpace(path) ? string.Empty : path + ".";

            var keys = new List <IPatchPrimitive>();

            while (reader.Read())
            {
                if (reader.Path == startPath)
                {
                    break;
                }

                if (reader.TokenType != JsonToken.PropertyName)
                {
                    while (reader.Read())
                    {
                        if (reader.Path == startPath)
                        {
                            return(null);
                        }
                    }
                }

                var readerValueAsName = (string)reader.Value;

                if (readerValueAsName == patchTypeKey)
                {
                    if (patchTypeAction != null)
                    {
                        patchTypeAction(reader);
                    }
                    else
                    {
                        reader.Skip();
                    }

                    continue;
                }

                var propertyInfo = modelProperties.FirstOrDefault(x =>
                {
                    var jsonPropertyNameAttribute = x.GetCustomAttribute <JsonPropertyAttribute>();

                    if (jsonPropertyNameAttribute != null)
                    {
                        return(string.Equals(jsonPropertyNameAttribute.PropertyName, readerValueAsName));
                    }

                    return(readerValueAsName.Equals(new CamelCaseNamingStrategy().GetPropertyName(x.Name, false)) ||
                           readerValueAsName.Equals(x.Name) ||
                           readerValueAsName.Equals(new SnakeCaseNamingStrategy().GetPropertyName(x.Name, false)));
                });

                // If property not found, we should report an error with it.
                if (propertyInfo == null)
                {
                    values.Add(new PatchPrimitive <object>
                    {
                        Errors = new List <Error>
                        {
                            new Error
                            {
                                ErrorType = typeof(InvalidCastException),
                                JsonName  = readerValueAsName,
                                JsonPath  = reader.Path,
                                Message   = string.Format(Resources.UnknownProperty, readerValueAsName),
                                Name      = readerValueAsName,
                                Path      = reader.Path
                            }
                        },
                        Found    = true,
                        JsonName = readerValueAsName,
                        JsonPath = reader.Path,
                        Name     = readerValueAsName,
                        Path     = reader.Path,
                        Value    = null
                    });

                    // Since this is an unknown property, we should not parse it.
                    reader.Skip();

                    continue;
                }

                var propertyName = propertyInfo.Name;
                var isKey        = propertyInfo.GetCustomAttribute <KeyAttribute>() != null;
                var isBindable   = IsBindable(bindAttribute, propertyName);

                if (!isBindable && (!isKey || keyAction == null))
                {
                    reader.Skip();
                    continue;
                }

                var value = (IPatchBase)SelectStrategyMethodInfo.MakeGenericMethod(propertyInfo.PropertyType)
                            .Invoke(null, new object[] { reader, propertyName, path + propertyName, propertyInfo });

                value.IgnoreApply = propertyInfo.GetCustomAttribute <IgnorePatchAttribute>() != null;

                if (isKey && keyAction != null)
                {
                    keys.Add((IPatchPrimitive)value);
                }

                if (isBindable)
                {
                    values.Add(value);
                }
            }

            foreach (var propertyInfo in modelProperties)
            {
                if (cacheProperties)
                {
                    PropertyInfos.Add(modelTypeAssemblyName, propertyInfo.Name, propertyInfo);
                }

                if (values.Any(x => x.Name == propertyInfo.Name))
                {
                    continue;
                }

                var propertyName = propertyInfo.Name;
                var isKey        = propertyInfo.GetCustomAttribute <KeyAttribute>() != null;
                var isBindable   = IsBindable(bindAttribute, propertyName);

                if (!isBindable && (!isKey || keyAction == null))
                {
                    continue;
                }

                var value = (IPatchBase)SelectStrategyMethodInfo.MakeGenericMethod(propertyInfo.PropertyType)
                            .Invoke(null, new object[] { null, propertyName, path + propertyName, propertyInfo });

                value.IgnoreApply = propertyInfo.GetCustomAttribute <IgnorePatchAttribute>() != null;

                if (isKey && keyAction != null && keys.All(x => x.Name != value.Name))
                {
                    keys.Add((IPatchPrimitive)value);
                }

                if (isBindable)
                {
                    values.Add(value);
                }
            }

            if (keys.Any())
            {
                foreach (var key in keys)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    keyAction.Invoke(key);
                }
            }

            return(values);
        }