示例#1
0
 /// <summary>Initializes a new instance of the <see cref="SchemaProcessorContext" /> class.</summary>
 /// <param name="type">The source type.</param>
 /// <param name="schema">The JSON Schema.</param>
 /// <param name="resolver">The resolver.</param>
 /// <param name="generator">The generator.</param>
 /// <param name="settings">The settings.</param>
 public SchemaProcessorContext(Type type, JsonSchema4 schema, JsonSchemaResolver resolver, JsonSchemaGenerator generator, JsonSchemaGeneratorSettings settings)
 {
     Type      = type;
     Schema    = schema;
     Resolver  = resolver;
     Generator = generator;
     Settings  = settings;
 }
示例#2
0
        /// <summary>Creates a <see cref="JsonTypeDescription"/> from a <see cref="Type"/>. </summary>
        /// <param name="type">The type. </param>
        /// <param name="parentAttributes">The parent's attributes (i.e. parameter or property attributes).</param>
        /// <param name="settings">The settings.</param>
        /// <returns>The <see cref="JsonTypeDescription"/>. </returns>
        public virtual JsonTypeDescription GetDescription(Type type, IEnumerable <Attribute> parentAttributes, JsonSchemaGeneratorSettings settings)
        {
            if (type.GetTypeInfo().IsEnum)
            {
                var isStringEnum = IsStringEnum(type, parentAttributes, settings.DefaultEnumHandling);
                return(JsonTypeDescription.CreateForEnumeration(type,
                                                                isStringEnum ? JsonObjectType.String : JsonObjectType.Integer, false));
            }

            if (type == typeof(short) ||
                type == typeof(uint) ||
                type == typeof(ushort))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.Integer, false, null));
            }

            if (type == typeof(int))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.Integer, false, JsonFormatStrings.Integer));
            }

            if (type == typeof(long) ||
                type == typeof(ulong))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.Integer, false, JsonFormatStrings.Long));
            }

            if (type == typeof(double) ||
                type == typeof(float))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.Number, false, JsonFormatStrings.Double));
            }

            if (type == typeof(decimal))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.Number, false, JsonFormatStrings.Decimal));
            }

            if (type == typeof(bool))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.Boolean, false, null));
            }

            var isNullable = IsNullable(type, parentAttributes, settings);

            if (type == typeof(string) || type == typeof(Type))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, null));
            }

            if (type == typeof(char))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, false, null));
            }

            if (type == typeof(Guid))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, false, JsonFormatStrings.Guid));
            }

            if (type == typeof(DateTime) ||
                type == typeof(DateTimeOffset) ||
                type.FullName == "NodaTime.OffsetDateTime" ||
                type.FullName == "NodaTime.LocalDateTime" ||
                type.FullName == "NodaTime.ZonedDateTime")
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, false, JsonFormatStrings.DateTime));
            }

            if (type == typeof(TimeSpan) ||
                type.FullName == "NodaTime.Duration")
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, false, JsonFormatStrings.TimeSpan));
            }

            if (type.FullName == "NodaTime.LocalDate")
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, false, JsonFormatStrings.Date));
            }

            if (type.FullName == "NodaTime.LocalTime")
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, false, JsonFormatStrings.Time));
            }

            if (type == typeof(Uri))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, JsonFormatStrings.Uri));
            }

            if (type == typeof(byte))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.Integer, false, JsonFormatStrings.Byte));
            }

            if (type == typeof(byte[]))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, JsonFormatStrings.Byte));
            }

            if (type == typeof(JObject) ||
                type == typeof(JToken) ||
                type.FullName == "System.Dynamic.ExpandoObject" ||
                type == typeof(object))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.None, isNullable, null));
            }

            if (IsFileType(type, parentAttributes))
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.File, isNullable, null));
            }

            var contract = settings.ResolveContract(type);

            if (IsDictionaryType(type, parentAttributes) && contract is JsonDictionaryContract)
            {
                return(JsonTypeDescription.CreateForDictionary(type, JsonObjectType.Object, isNullable));
            }

            if (IsArrayType(type, parentAttributes) && contract is JsonArrayContract)
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.Array, isNullable, null));
            }

            if (type.Name == "Nullable`1")
            {
#if !LEGACY
                var typeDescription = GetDescription(type.GenericTypeArguments[0], parentAttributes, settings);
#else
                var typeDescription = GetDescription(type.GetGenericArguments()[0], parentAttributes, settings);
#endif
                typeDescription.IsNullable = true;
                return(typeDescription);
            }

            var jsonSchemaAttribute = type.GetTypeInfo().GetCustomAttribute <JsonSchemaAttribute>() ??
                                      parentAttributes?.OfType <JsonSchemaAttribute>().SingleOrDefault();
            if (jsonSchemaAttribute != null)
            {
                var classType = jsonSchemaAttribute.Type != JsonObjectType.None ? jsonSchemaAttribute.Type : JsonObjectType.Object;
                var format    = !string.IsNullOrEmpty(jsonSchemaAttribute.Format) ? jsonSchemaAttribute.Format : null;
                return(JsonTypeDescription.Create(type, classType, isNullable, format));
            }

            if (contract is JsonStringContract)
            {
                return(JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, null));
            }

            return(JsonTypeDescription.Create(type, JsonObjectType.Object, isNullable, null));
        }
示例#3
0
        /// <summary>Checks whether a type is nullable.</summary>
        /// <param name="type">The type.</param>
        /// <param name="parentAttributes">The parent attributes (e.g. property or parameter attributes).</param>
        /// <param name="settings">The settings</param>
        /// <returns>true if the type can be null.</returns>
        public virtual bool IsNullable(Type type, IEnumerable <Attribute> parentAttributes, JsonSchemaGeneratorSettings settings)
        {
            var jsonPropertyAttribute = parentAttributes?.OfType <JsonPropertyAttribute>().SingleOrDefault();

            if (jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.DisallowNull)
            {
                return(false);
            }

            if (parentAttributes.TryGetIfAssignableTo("NotNullAttribute", TypeNameStyle.Name) != null)
            {
                return(false);
            }

            if (parentAttributes.TryGetIfAssignableTo("CanBeNullAttribute", TypeNameStyle.Name) != null)
            {
                return(true);
            }

            if (type.Name == "Nullable`1")
            {
                return(true);
            }

            var isValueType = type != typeof(string) && type.GetTypeInfo().IsValueType;

            return(isValueType == false && settings.DefaultReferenceTypeNullHandling == ReferenceTypeNullHandling.Null);
        }