Exemple #1
0
        public override JsonConverter CreateConverter(
            Type typeToConvert, JsonSerializerOptions options)
        {
            var descriptor    = JsonConverterHelper.GetMessageDescriptor(typeToConvert) !;
            var converterType = WellKnownTypeNames[descriptor.FullName];

            var converter = (JsonConverter)Activator.CreateInstance(
                converterType.MakeGenericType(new Type[] { typeToConvert }),
                BindingFlags.Instance | BindingFlags.Public,
                binder: null,
                args: new object[] { _settings },
                culture: null) !;

            return(converter);
        }
Exemple #2
0
        public override bool CanConvert(Type typeToConvert)
        {
            if (!typeof(IMessage).IsAssignableFrom(typeToConvert))
            {
                return(false);
            }

            var descriptor = JsonConverterHelper.GetMessageDescriptor(typeToConvert);

            if (descriptor == null)
            {
                return(false);
            }

            return(JsonConverterHelper.IsWrapperType(descriptor));
        }
Exemple #3
0
        public override bool CanConvert(Type typeToConvert)
        {
            if (!typeof(IMessage).IsAssignableFrom(typeToConvert))
            {
                return(false);
            }

            var descriptor = JsonConverterHelper.GetMessageDescriptor(typeToConvert);

            if (descriptor == null)
            {
                return(false);
            }

            return(WellKnownTypeNames.ContainsKey(descriptor.FullName));
        }
Exemple #4
0
        private static EnumDescriptor?ResolveEnumDescriptor(Type typeToConvert)
        {
            var containingType = typeToConvert?.DeclaringType?.DeclaringType;

            if (containingType != null)
            {
                var messageDescriptor = JsonConverterHelper.GetMessageDescriptor(containingType);
                if (messageDescriptor != null)
                {
                    for (var i = 0; i < messageDescriptor.EnumTypes.Count; i++)
                    {
                        if (messageDescriptor.EnumTypes[i].ClrType == typeToConvert)
                        {
                            return(messageDescriptor.EnumTypes[i]);
                        }
                    }
                }
            }

            return(null);
        }