Exemplo n.º 1
0
        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();
            _missingMemberHandling   = options.GetMissingMemberHandling();
        }