private void DetermineSerializationCapabilities() { if (ClassType != ClassType.Enumerable && ClassType != ClassType.Dictionary && ClassType != ClassType.IDictionaryConstructible) { // 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) { if (HasSetter) { ShouldDeserialize = true; } else if (!RuntimePropertyType.IsArray && (typeof(IList).IsAssignableFrom(RuntimePropertyType) || typeof(IDictionary).IsAssignableFrom(RuntimePropertyType))) { ShouldDeserialize = true; } } //else if (HasSetter) //{ // // todo: Special case where there is no getter but a setter (and an EnumerableConverter) //} if (ShouldDeserialize) { ShouldSerialize = HasGetter; if (RuntimePropertyType.IsArray) { EnumerableConverter = s_jsonArrayConverter; } else if (ClassType == ClassType.IDictionaryConstructible) { if (RuntimePropertyType.FullName.StartsWith(JsonClassInfo.ImmutableNamespaceName)) { DefaultImmutableDictionaryConverter.RegisterImmutableDictionary( RuntimePropertyType, JsonClassInfo.GetElementType(RuntimePropertyType, ParentClassType, PropertyInfo, Options), Options); DictionaryConverter = s_jsonImmutableDictionaryConverter; } else if (JsonClassInfo.IsDeserializedByConstructingWithIDictionary(RuntimePropertyType)) { DictionaryConverter = s_jsonIDictionaryConverter; } } else if (typeof(IEnumerable).IsAssignableFrom(RuntimePropertyType)) { if (JsonClassInfo.IsDeserializedByConstructingWithIList(RuntimePropertyType) || (!typeof(IList).IsAssignableFrom(RuntimePropertyType) && JsonClassInfo.HasConstructorThatTakesGenericIEnumerable(RuntimePropertyType, Options))) { EnumerableConverter = s_jsonICollectionConverter; } else if (RuntimePropertyType.IsGenericType && RuntimePropertyType.FullName.StartsWith(JsonClassInfo.ImmutableNamespaceName) && RuntimePropertyType.GetGenericArguments().Length == 1) { DefaultImmutableEnumerableConverter.RegisterImmutableCollection(RuntimePropertyType, JsonClassInfo.GetElementType(RuntimePropertyType, ParentClassType, PropertyInfo, Options), Options); EnumerableConverter = s_jsonImmutableEnumerableConverter; } } } else { ShouldSerialize = HasGetter && !Options.IgnoreReadOnlyProperties; } } }
private void DetermineSerializationCapabilities() { if (ClassType != ClassType.Enumerable && ClassType != ClassType.Dictionary && ClassType != ClassType.IDictionaryConstructible) { // 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.IDictionaryConstructible) { // Natively supported type. if (DeclaredPropertyType == ImplementedPropertyType) { if (RuntimePropertyType.FullName.StartsWith(JsonClassInfo.ImmutableNamespaceName)) { DefaultImmutableDictionaryConverter.RegisterImmutableDictionary( RuntimePropertyType, JsonClassInfo.GetElementType(RuntimePropertyType, ParentClassType, PropertyInfo, Options), Options); DictionaryConverter = s_jsonImmutableDictionaryConverter; } else if (JsonClassInfo.IsDeserializedByConstructingWithIDictionary(RuntimePropertyType)) { DictionaryConverter = s_jsonIDictionaryConverter; } } // Type that implements a type with ClassType IDictionaryConstructible. else { DictionaryConverter = s_jsonDerivedDictionaryConverter; } } else if (ClassType == ClassType.Enumerable) { // Else if it's an implementing type whose runtime type is not assignable to IList. if (DeclaredPropertyType != ImplementedPropertyType && (!typeof(IList).IsAssignableFrom(RuntimePropertyType) || ImplementedPropertyType == typeof(ArrayList) || ImplementedPropertyType == typeof(IList))) { EnumerableConverter = s_jsonDerivedEnumerableConverter; } else if (JsonClassInfo.IsDeserializedByConstructingWithIList(RuntimePropertyType) || (!typeof(IList).IsAssignableFrom(RuntimePropertyType) && JsonClassInfo.HasConstructorThatTakesGenericIEnumerable(RuntimePropertyType, Options))) { EnumerableConverter = s_jsonICollectionConverter; } else if (RuntimePropertyType.IsGenericType && RuntimePropertyType.FullName.StartsWith(JsonClassInfo.ImmutableNamespaceName) && RuntimePropertyType.GetGenericArguments().Length == 1) { DefaultImmutableEnumerableConverter.RegisterImmutableCollection(RuntimePropertyType, JsonClassInfo.GetElementType(RuntimePropertyType, ParentClassType, PropertyInfo, Options), Options); EnumerableConverter = s_jsonImmutableEnumerableConverter; } } } } } }
private void DetermineSerializationCapabilities() { if (ClassType != ClassType.Enumerable && ClassType != ClassType.Dictionary && ClassType != ClassType.ImmutableDictionary) { // 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) { if (HasSetter) { ShouldDeserialize = true; } else if (!RuntimePropertyType.IsArray && (typeof(IList).IsAssignableFrom(RuntimePropertyType) || typeof(IDictionary).IsAssignableFrom(RuntimePropertyType))) { ShouldDeserialize = true; } } //else if (HasSetter) //{ // // todo: Special case where there is no getter but a setter (and an EnumerableConverter) //} if (ShouldDeserialize) { ShouldSerialize = HasGetter; if (RuntimePropertyType.IsArray) { EnumerableConverter = s_jsonArrayConverter; } else if (ClassType == ClassType.ImmutableDictionary) { DefaultImmutableConverter.RegisterImmutableDictionary( RuntimePropertyType, JsonClassInfo.GetElementType(RuntimePropertyType, parentType: null, memberInfo: null), Options); EnumerableConverter = s_jsonImmutableConverter; } else if (typeof(IEnumerable).IsAssignableFrom(RuntimePropertyType)) { Type elementType = JsonClassInfo.GetElementType(RuntimePropertyType, ParentClassType, PropertyInfo); // If the property type only has interface(s) exposed by JsonEnumerableT<T> then use JsonEnumerableT as the converter. if (RuntimePropertyType.IsAssignableFrom(typeof(JsonEnumerableT <>).MakeGenericType(elementType))) { EnumerableConverter = s_jsonEnumerableConverter; } // Else if IList can't be assigned from the property type (we populate and return an IList directly) // and the type can be constructed with an IEnumerable<T>, then use the // IEnumerableConstructible converter to create the instance. else if (!typeof(IList).IsAssignableFrom(RuntimePropertyType) && RuntimePropertyType.GetConstructor(new Type[] { typeof(List <>).MakeGenericType(elementType) }) != null) { EnumerableConverter = s_jsonIEnumerableConstuctibleConverter; } // Else if it's a System.Collections.Immutable type with one generic argument. else if (RuntimePropertyType.IsGenericType && RuntimePropertyType.FullName.StartsWith(DefaultImmutableConverter.ImmutableNamespace) && RuntimePropertyType.GetGenericArguments().Length == 1) { DefaultImmutableConverter.RegisterImmutableCollection(RuntimePropertyType, elementType, Options); EnumerableConverter = s_jsonImmutableConverter; } } } else { ShouldSerialize = HasGetter && !Options.IgnoreReadOnlyProperties; } } }