Ejemplo n.º 1
0
        private static void CollectProperties(fsConfig config, List <fsMetaProperty> properties, Type reflectedType)
        {
            // do we require a [SerializeField] or [fsProperty] attribute?
            bool requireOptIn  = config.DefaultMemberSerialization == fsMemberSerialization.OptIn;
            bool requireOptOut = config.DefaultMemberSerialization == fsMemberSerialization.OptOut;

            fsObjectAttribute attr = fsPortableReflection.GetAttribute <fsObjectAttribute>(reflectedType);

            if (attr != null)
            {
                requireOptIn  = attr.MemberSerialization == fsMemberSerialization.OptIn;
                requireOptOut = attr.MemberSerialization == fsMemberSerialization.OptOut;
            }

            MemberInfo[] members = reflectedType.GetDeclaredMembers();
            foreach (var member in members)
            {
                // We don't serialize members annotated with any of the ignore serialize attributes
                if (config.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t)))
                {
                    continue;
                }

                PropertyInfo property = member as PropertyInfo;
                FieldInfo    field    = member as FieldInfo;

                // Early out if it's neither a field or a property, since we don't serialize anything else.
                if (property == null && field == null)
                {
                    continue;
                }

                // Skip properties if we don't want them, to avoid the cost of checking attributes.
                if (property != null && !config.EnablePropertySerialization)
                {
                    continue;
                }

                // If an opt-in annotation is required, then skip the property if it doesn't have one
                // of the serialize attributes
                if (requireOptIn &&
                    !config.SerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t)))
                {
                    continue;
                }

                // If an opt-out annotation is required, then skip the property *only if* it has one of
                // the not serialize attributes
                if (requireOptOut &&
                    config.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t)))
                {
                    continue;
                }

                if (property != null)
                {
                    if (CanSerializeProperty(config, property, members, requireOptOut))
                    {
                        properties.Add(new fsMetaProperty(config, property));
                    }
                }
                else if (field != null)
                {
                    if (CanSerializeField(config, field, requireOptOut))
                    {
                        properties.Add(new fsMetaProperty(config, field));
                    }
                }
            }

            if (reflectedType.Resolve().BaseType != null)
            {
                CollectProperties(config, properties, reflectedType.Resolve().BaseType);
            }
        }
        public fsSerializer()
        {
            _cachedConverterTypeInstances = new Dictionary <Type, fsBaseConverter>();
            _cachedConverters             = new Dictionary <Type, fsBaseConverter>();
            _cachedProcessors             = new Dictionary <Type, List <fsObjectProcessor> >();

            _references          = new fsCyclicReferenceManager();
            _lazyReferenceWriter = new fsLazyCycleDefinitionWriter();

            // note: The order here is important. Items at the beginning of this
            //       list will be used before converters at the end. Converters
            //       added via AddConverter() are added to the front of the list.
            _availableConverters = new List <fsConverter> {
                new fsNullableConverter {
                    Serializer = this
                },
                new fsGuidConverter {
                    Serializer = this
                },
                new fsTypeConverter {
                    Serializer = this
                },
                new fsDateConverter {
                    Serializer = this
                },
                new fsEnumConverter {
                    Serializer = this
                },
                new fsPrimitiveConverter {
                    Serializer = this
                },
                new fsArrayConverter {
                    Serializer = this
                },
                new fsDictionaryConverter {
                    Serializer = this
                },
                new fsIEnumerableConverter {
                    Serializer = this
                },
                new fsKeyValuePairConverter {
                    Serializer = this
                },
                new fsWeakReferenceConverter {
                    Serializer = this
                },
                new fsReflectedConverter {
                    Serializer = this
                }
            };
            _availableDirectConverters = new Dictionary <Type, fsDirectConverter>();

            _processors = new List <fsObjectProcessor>()
            {
                new fsSerializationCallbackProcessor()
            };

#if !NO_UNITY
            _processors.Add(new fsSerializationCallbackReceiverProcessor());
#endif

            Context = new fsContext();
            Config  = new fsConfig();

            // Register the converters from the registrar
            foreach (var converterType in fsConverterRegistrar.Converters)
            {
                AddConverter((fsBaseConverter)Activator.CreateInstance(converterType));
            }
        }