Example #1
0
        private void DetermineSerializationCapabilities()
        {
            if ((ClassType & (ClassType.Enumerable | ClassType.Dictionary)) == 0)
            {
                // We serialize if there is a getter + not ignoring readonly properties.
                ShouldSerialize = HasGetter && (HasSetter || !Options.IgnoreReadOnlyProperties);

                // We deserialize if there is a setter.
                ShouldDeserialize = HasSetter;
            }
            else
            {
                if (HasGetter)
                {
                    ShouldSerialize = true;

                    if (HasSetter)
                    {
                        ShouldDeserialize = true;

                        if (RuntimePropertyType.IsArray)
                        {
                            // Verify that we don't have a multidimensional array.
                            if (RuntimePropertyType.GetArrayRank() > 1)
                            {
                                throw ThrowHelper.GetNotSupportedException_SerializationNotSupportedCollection(RuntimePropertyType, ParentClassType, PropertyInfo);
                            }

                            EnumerableConverter = s_jsonArrayConverter;
                        }
                        else if (ClassType == ClassType.Dictionary && DefaultImmutableDictionaryConverter.IsImmutableDictionary(RuntimePropertyType))
                        {
                            DefaultImmutableDictionaryConverter.RegisterImmutableDictionary(RuntimePropertyType, ElementType, Options);
                            DictionaryConverter = s_jsonImmutableDictionaryConverter;
                        }
                        else if (ClassType == ClassType.Enumerable && DefaultImmutableEnumerableConverter.IsImmutableEnumerable(RuntimePropertyType, out bool isImmutableArray))
                        {
                            DefaultImmutableEnumerableConverter.RegisterImmutableCollection(RuntimePropertyType, ElementType, Options);
                            EnumerableConverter = s_jsonImmutableEnumerableConverter;

                            IsImmutableArray = isImmutableArray;
                        }
                    }
                }
            }
        }
Example #2
0
        public static ClassType GetClassType(Type type)
        {
            Debug.Assert(type != null);

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                type = Nullable.GetUnderlyingType(type);
            }

            if (DefaultConverters.IsValueConvertable(type))
            {
                return(ClassType.Value);
            }

            if (DefaultImmutableDictionaryConverter.IsImmutableDictionary(type) ||
                IsDeserializedByConstructingWithIDictionary(type))
            {
                return(ClassType.IDictionaryConstructible);
            }

            if (typeof(IDictionary).IsAssignableFrom(type) ||
                (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IDictionary <,>) ||
                                        type.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary <,>))))
            {
                return(ClassType.Dictionary);
            }

            if (IsKeyValuePair(type))
            {
                return(ClassType.KeyValuePair);
            }

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                return(ClassType.Enumerable);
            }

            if (type == typeof(object))
            {
                return(ClassType.Unknown);
            }

            return(ClassType.Object);
        }