public ObjectConverter(JsonSerializerOptions options) { _objectMapping = options.GetObjectMappingRegistry().Lookup <T>(); _memberConverters = new Lazy <MemberConverters>(() => MemberConverters.Create(options, _objectMapping)); _isInterfaceOrAbstract = typeof(T).IsInterface || typeof(T).IsAbstract; _isStruct = typeof(T).IsStruct(); if (!_isInterfaceOrAbstract && _objectMapping.CreatorMapping == null && !_isStruct) { ConstructorInfo defaultConstructorInfo = typeof(T).GetConstructor( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); if (defaultConstructorInfo == null) { throw new JsonException($"Cannot find a default constructor on type {typeof(T)}"); } _constructor = defaultConstructorInfo.CreateDelegate <T>(); } _discriminatorConvention = options.GetDiscriminatorConventionRegistry().GetConvention(typeof(T)); _referenceHandling = _isStruct ? ReferenceHandling.Default : options.GetReferenceHandling(); }
public static MemberConverters Create(JsonSerializerOptions options, IObjectMapping objectMapping) { MemberConverters converters = new MemberConverters(); if (options.PropertyNameCaseInsensitive) { converters.ForReadAsString = new Dictionary <string, IMemberConverter>(StringComparer.OrdinalIgnoreCase); } converters.ExtensionData = objectMapping.ExtensionData; foreach (IMemberMapping memberMapping in objectMapping.MemberMappings) { IMemberConverter memberConverter = memberMapping.GenerateMemberConverter(); if (memberMapping.CanBeDeserialized || objectMapping.IsCreatorMember(memberConverter.MemberName)) { if (options.PropertyNameCaseInsensitive) { converters.ForReadAsString.Add(memberConverter.MemberNameAsString, memberConverter); } converters.ForRead.Add(memberConverter.MemberName, memberConverter); if (memberConverter.RequirementPolicy == RequirementPolicy.AllowNull || memberConverter.RequirementPolicy == RequirementPolicy.Always) { converters.RequiredForRead.Add(memberConverter); } } if (memberMapping.CanBeSerialized) { converters.ForWrite.Add(memberConverter); } } return(converters); }