public static EnumerableConverterAttribute GetEnumerableConverter( Type parentClassType, PropertyInfo propertyInfo, Type propertyType, JsonSerializerOptions options) { Type enumerableType; if (propertyType.IsGenericType) { enumerableType = propertyType.GetGenericTypeDefinition(); } else if (propertyType.IsArray) { enumerableType = typeof(Array); } else { enumerableType = propertyType; } EnumerableConverterAttribute attr = null; if (propertyInfo != null) { // Use Property first attr = options.GetAttributes <EnumerableConverterAttribute>(propertyInfo).Where(a => a.EnumerableType == enumerableType).FirstOrDefault(); } if (attr == null) { // Then class type attr = options.GetAttributes <EnumerableConverterAttribute>(parentClassType, inherit: true).Where(a => a.EnumerableType == enumerableType).FirstOrDefault(); if (attr == null) { // Then declaring assembly attr = options.GetAttributes <EnumerableConverterAttribute>(parentClassType.Assembly).Where(a => a.EnumerableType == enumerableType).FirstOrDefault(); if (attr == null) { // Then global attr = options.GetAttributes <EnumerableConverterAttribute>(JsonSerializerOptions.GlobalAttributesProvider).Where(a => a.EnumerableType == enumerableType).FirstOrDefault(); // Then default if (enumerableType == typeof(Array)) { attr = s_arrayConverterAttibute; } } } } return(attr); }
public void EnumerableConverter_EnumerableModelEmptyList_Test() { var context = new DittoProcessorContext { PropertyInfo = typeof(WrappedModel).GetProperty("MyProperty3") }; var processor = new EnumerableConverterAttribute(); var result = processor.ProcessValue(null, context); Assert.That(result, Is.Not.Null); }
public void EnumerableConverter_EnumerableModelEmptyList_Test() { var context = new DittoProcessorContext { PropertyDescriptor = TypeDescriptor.GetProperties(new WrappedModel())["MyProperty3"] }; var processor = new EnumerableConverterAttribute(); var result = processor.ProcessValue(null, context); Assert.That(result, Is.Not.Null); }
public void EnumerableConverter_InheritedEnumerableModel_Test() { var context = new DittoProcessorContext { PropertyInfo = typeof(WrappedModel).GetProperty("MyProperty2") }; var processor = new EnumerableConverterAttribute(); var result = processor.ProcessValue(null, context); // The value should be null since we don't have an empty constructor. Assert.That(result, Is.Null); }
private static bool HandleEndArray( JsonSerializerOptions options, ref ReadObjectState current, ref List <ReadObjectState> previous, ref int arrayIndex) { IEnumerable value = ReadObjectState.GetEnumerableValue(current); if (value == null) { // We added the items to the list property already. current.ResetProperty(); return(false); } bool lastFrame = (arrayIndex == 0); bool setPropertyDirectly; if (current.TempEnumerableValues != null) { EnumerableConverterAttribute converter = current.PropertyInfo.EnumerableConverter; if (converter == null) { converter = current.ClassInfo.EnumerableConverter; } Type elementType = current.GetElementType(); value = converter.CreateFromList(elementType, (IList)value); setPropertyDirectly = true; } else { setPropertyDirectly = false; } if (current.PopStackOnEndArray) { ReadObjectState previousFrame = default; GetPreviousState(ref previous, ref previousFrame, --arrayIndex); current = previousFrame; } if (lastFrame) { if (current.ReturnValue == null) { // Returning a converted list or object. current.Reset(); current.ReturnValue = value; return(true); } else if (current.IsEnumerable()) { // Returning a non-converted list. return(true); } // else there must be an outer object, so we'll return false here. } ReadObjectState.SetReturnValue(value, options, ref current, setPropertyDirectly: setPropertyDirectly); return(false); }