internal virtual void GetPolicies(JsonSerializerOptions options) { if (RuntimePropertyType.IsArray) { EnumerableConverter = new DefaultArrayConverter(); } else if (typeof(IEnumerable).IsAssignableFrom(RuntimePropertyType)) { Type elementType = JsonClassInfo.GetElementType(RuntimePropertyType); if (RuntimePropertyType.IsAssignableFrom(typeof(JsonEnumerableT <>).MakeGenericType(elementType))) { EnumerableConverter = new DefaultEnumerableConverter(); } } }
private void DetermineSerializationCapabilities(JsonSerializerOptions options) { bool hasIgnoreAttribute = (GetAttribute <JsonIgnoreAttribute>() != null); if (hasIgnoreAttribute) { // We don't serialize or deserialize. return; } if (ClassType != ClassType.Enumerable) { // We serialize if there is a getter + no [Ignore] attribute + not ignoring readonly properties. ShouldSerialize = HasGetter && (HasSetter || !options.IgnoreReadOnlyProperties); // We deserialize if there is a setter + no [Ignore] attribute. ShouldDeserialize = HasSetter; } else { if (HasGetter) { if (HasSetter) { ShouldDeserialize = true; } else if (RuntimePropertyType.IsAssignableFrom(typeof(IList))) { ShouldDeserialize = true; } //else //{ // // todo: future feature that allows non-List types (e.g. from System.Collections.Immutable) to have converters. //} } //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 (typeof(IEnumerable).IsAssignableFrom(RuntimePropertyType)) { Type elementType = JsonClassInfo.GetElementType(RuntimePropertyType); // 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 { ShouldSerialize = HasGetter && !options.IgnoreReadOnlyProperties; } } }
private void DetermineSerializationCapabilities(JsonSerializerOptions options) { if (ClassType != ClassType.Enumerable && ClassType != ClassType.Dictionary) { // 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 (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) { EnumerableConverter = s_jsonImmutableConverter; ((DefaultImmutableConverter)EnumerableConverter).RegisterImmutableCollectionType(RuntimePropertyType, elementType, options); } } } else { ShouldSerialize = HasGetter && !options.IgnoreReadOnlyProperties; } } }