GetCatelPropertiesToSerialize() public method

Gets the catel properties to serialize.
The is null.
public GetCatelPropertiesToSerialize ( Type type ) : MemberMetadata>.Dictionary
type System.Type The type.
return MemberMetadata>.Dictionary
Esempio n. 1
0
            public void ThrowsArgumentNullExceptionForNullType()
            {
                var serializationManager = new SerializationManager();

                ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => serializationManager.GetCatelPropertiesToSerialize(null));
            }
Esempio n. 2
0
        /// <summary>
        /// Gets the serializable members for the specified model.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="model">The model.</param>
        /// <param name="membersToIgnore">The members to ignore.</param>
        /// <returns>The list of members to serialize.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public virtual List <MemberValue> GetSerializableMembers(ISerializationContext <TSerializationContext> context, object model, params string[] membersToIgnore)
        {
            Argument.IsNotNull("model", model);

            var listToSerialize        = new List <MemberValue>();
            var membersToIgnoreHashSet = new HashSet <string>(membersToIgnore);

            var modelType = model.GetType();

            // If a basic type, we need to directly deserialize as member and replace the context model
            if (ShouldExternalSerializerHandleMember(context.ModelType))
            {
                listToSerialize.Add(new MemberValue(SerializationMemberGroup.SimpleRootObject, modelType, modelType, RootObjectName, RootObjectName, model));
                return(listToSerialize);
            }

            if (ShouldSerializeAsDictionary(modelType))
            {
                // CTL-688: only support json for now. In the future, these checks (depth AND type) should be removed
                if (SupportsDictionarySerialization(context))
                {
                    listToSerialize.Add(new MemberValue(SerializationMemberGroup.Dictionary, modelType, modelType, CollectionName, CollectionName, model));
                    return(listToSerialize);
                }
            }

            if (ShouldSerializeAsCollection(modelType))
            {
                listToSerialize.Add(new MemberValue(SerializationMemberGroup.Collection, modelType, modelType, CollectionName, CollectionName, model));
                return(listToSerialize);
            }

            if (modelType == typeof(SerializableKeyValuePair))
            {
                var keyValuePair = (SerializableKeyValuePair)model;

                var keyType   = typeof(object);
                var valueType = typeof(object);

                // Search max 2 levels deep, if not found, then we failed
                var parentDictionary = context.FindParentType(x => x.IsDictionary(), 2);
                if (parentDictionary != null)
                {
                    var genericTypeDefinition = parentDictionary.GetGenericArgumentsEx();

                    keyType   = genericTypeDefinition[0];
                    valueType = genericTypeDefinition[1];
                }

                listToSerialize.Add(new MemberValue(SerializationMemberGroup.RegularProperty, modelType, keyType, "Key", "Key", keyValuePair.Key));
                listToSerialize.Add(new MemberValue(SerializationMemberGroup.RegularProperty, modelType, valueType, "Value", "Value", keyValuePair.Value));

                return(listToSerialize);
            }

            var modelInfo = _serializationModelCache.GetFromCacheOrFetch(modelType, () =>
            {
                var catelProperties = SerializationManager.GetCatelPropertiesToSerialize(modelType);
                var fields          = SerializationManager.GetFieldsToSerialize(modelType);
                var properties      = SerializationManager.GetRegularPropertiesToSerialize(modelType);

                return(new SerializationModelInfo(modelType, catelProperties, fields, properties));
            });

            var members = new List <MemberMetadata>();

            members.AddRange(modelInfo.CatelPropertiesByName.Values);
            members.AddRange(modelInfo.PropertiesByName.Values);
            members.AddRange(modelInfo.FieldsByName.Values);

            foreach (var memberMetadata in members)
            {
                var memberName = memberMetadata.MemberName;
                if (membersToIgnoreHashSet.Contains(memberName) || ShouldIgnoreMember(model, memberName))
                {
                    Log.Debug("Member '{0}' is being ignored for serialization", memberName);
                    continue;
                }

                var memberValue = ObjectAdapter.GetMemberValue(model, memberName, modelInfo);
                if (memberValue != null)
                {
                    listToSerialize.Add(memberValue);
                }
            }

            return(listToSerialize);
        }
Esempio n. 3
0
            public void ReturnsCorrectProperties()
            {
                var serializationManager = new SerializationManager();

                var propertiesToSerialize = serializationManager.GetCatelPropertiesToSerialize(typeof(TestModel)).ToArray();

                Assert.AreEqual(2, propertiesToSerialize.Length);
                Assert.AreEqual("DateTimeProperty", propertiesToSerialize[0].Key);
                Assert.AreEqual("IncludedCatelProperty", propertiesToSerialize[1].Key);
            }