HasFlag() private méthode

private HasFlag ( DefaultValueHandling value, DefaultValueHandling flag ) : bool
value DefaultValueHandling
flag DefaultValueHandling
Résultat bool
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            JsonSchemaType?nullable;
            Type           type1;
            Type           type2;
            JsonSchemaType jsonSchemaType;
            JsonSchemaType?nullable1;

            ValidationUtils.ArgumentNotNull(type, "type");
            string typeId = this.GetTypeId(type, false);
            string str    = this.GetTypeId(type, true);

            if (!string.IsNullOrEmpty(typeId))
            {
                JsonSchema schema = this._resolver.GetSchema(typeId);
                if (schema != null)
                {
                    if (valueRequired != Required.Always && !JsonSchemaGenerator.HasFlag(schema.Type, JsonSchemaType.Null))
                    {
                        JsonSchema jsonSchema = schema;
                        nullable = jsonSchema.Type;
                        if (nullable.HasValue)
                        {
                            nullable1 = new JsonSchemaType?(nullable.GetValueOrDefault() | JsonSchemaType.Null);
                        }
                        else
                        {
                            nullable1 = null;
                        }
                        jsonSchema.Type = nullable1;
                    }
                    if (required)
                    {
                        bool?nullable2 = schema.Required;
                        if (!nullable2.GetValueOrDefault() | !nullable2.HasValue)
                        {
                            schema.Required = new bool?(true);
                        }
                    }
                    return(schema);
                }
            }
            if (this._stack.Any <JsonSchemaGenerator.TypeSchema>((JsonSchemaGenerator.TypeSchema tc) => tc.Type == type))
            {
                throw new JsonException("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.".FormatWith(CultureInfo.InvariantCulture, type));
            }
            JsonContract  jsonContract = this.ContractResolver.ResolveContract(type);
            JsonConverter converter    = jsonContract.Converter ?? jsonContract.InternalConverter;

            this.Push(new JsonSchemaGenerator.TypeSchema(type, new JsonSchema()));
            if (str != null)
            {
                this.CurrentSchema.Id = str;
            }
            if (required)
            {
                this.CurrentSchema.Required = new bool?(true);
            }
            this.CurrentSchema.Title       = this.GetTitle(type);
            this.CurrentSchema.Description = this.GetDescription(type);
            if (converter == null)
            {
                switch (jsonContract.ContractType)
                {
                case JsonContractType.Object:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateObjectSchema(type, (JsonObjectContract)jsonContract);
                    break;
                }

                case JsonContractType.Array:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Array, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    JsonArrayAttribute cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonArrayAttribute>(type);
                    bool flag = (cachedAttribute == null ? true : cachedAttribute.AllowNullItems);
                    Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                    if (collectionItemType == null)
                    {
                        break;
                    }
                    this.CurrentSchema.Items = new List <JsonSchema>()
                    {
                        this.GenerateInternal(collectionItemType, (!flag ? Required.Always : Required.Default), false)
                    };
                    break;
                }

                case JsonContractType.Primitive:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.GetJsonSchemaType(type, valueRequired));
                    nullable = this.CurrentSchema.Type;
                    if (!(nullable.GetValueOrDefault() == JsonSchemaType.Integer & nullable.HasValue) || !type.IsEnum() || type.IsDefined(typeof(FlagsAttribute), true))
                    {
                        break;
                    }
                    this.CurrentSchema.Enum = new List <JToken>();
                    EnumInfo enumValuesAndNames = EnumUtils.GetEnumValuesAndNames(type);
                    for (int i = 0; i < (int)enumValuesAndNames.Names.Length; i++)
                    {
                        ulong  values  = enumValuesAndNames.Values[i];
                        JToken jTokens = JToken.FromObject(Enum.ToObject(type, values));
                        this.CurrentSchema.Enum.Add(jTokens);
                    }
                    break;
                }

                case JsonContractType.String:
                {
                    jsonSchemaType          = (!ReflectionUtils.IsNullable(jsonContract.UnderlyingType) ? JsonSchemaType.String : this.AddNullType(JsonSchemaType.String, valueRequired));
                    this.CurrentSchema.Type = new JsonSchemaType?(jsonSchemaType);
                    break;
                }

                case JsonContractType.Dictionary:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    ReflectionUtils.GetDictionaryKeyValueTypes(type, out type1, out type2);
                    if (!(type1 != null) || this.ContractResolver.ResolveContract(type1).ContractType != JsonContractType.Primitive)
                    {
                        break;
                    }
                    this.CurrentSchema.AdditionalProperties = this.GenerateInternal(type2, Required.Default, false);
                    break;
                }

                case JsonContractType.Dynamic:
                case JsonContractType.Linq:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
                    break;
                }

                case JsonContractType.Serializable:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateISerializableContract(type, (JsonISerializableContract)jsonContract);
                    break;
                }

                default:
                {
                    throw new JsonException("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, jsonContract));
                }
                }
            }
            else
            {
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            return(this.Pop().Schema);
        }
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            ValidationUtils.ArgumentNotNull(type, "type");
            string typeId  = this.GetTypeId(type, false);
            string typeId2 = this.GetTypeId(type, true);

            if (!string.IsNullOrEmpty(typeId))
            {
                JsonSchema schema = this._resolver.GetSchema(typeId);
                if (schema != null)
                {
                    if (valueRequired != Required.Always && !JsonSchemaGenerator.HasFlag(schema.Type, JsonSchemaType.Null))
                    {
                        JsonSchema     expr_76 = schema;
                        JsonSchemaType?type2   = expr_76.Type;
                        expr_76.Type = ((!type2.get_HasValue()) ? default(JsonSchemaType?) : new JsonSchemaType?(type2.GetValueOrDefault() | JsonSchemaType.Null));
                    }
                    if (required)
                    {
                        bool?required2 = schema.Required;
                        if (!required2.GetValueOrDefault() || !required2.get_HasValue())
                        {
                            schema.Required = new bool?(true);
                        }
                    }
                    return(schema);
                }
            }
            if (Enumerable.Any <JsonSchemaGenerator.TypeSchema>(this._stack, (JsonSchemaGenerator.TypeSchema tc) => tc.Type == type))
            {
                throw new Exception("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                {
                    type
                }));
            }
            JsonContract  jsonContract = this.ContractResolver.ResolveContract(type);
            JsonConverter jsonConverter;

            if ((jsonConverter = jsonContract.Converter) != null || (jsonConverter = jsonContract.InternalConverter) != null)
            {
                JsonSchema schema2 = jsonConverter.GetSchema();
                if (schema2 != null)
                {
                    return(schema2);
                }
            }
            this.Push(new JsonSchemaGenerator.TypeSchema(type, new JsonSchema()));
            if (typeId2 != null)
            {
                this.CurrentSchema.Id = typeId2;
            }
            if (required)
            {
                this.CurrentSchema.Required = new bool?(true);
            }
            this.CurrentSchema.Title       = this.GetTitle(type);
            this.CurrentSchema.Description = this.GetDescription(type);
            if (jsonConverter != null)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            else if (jsonContract is JsonDictionaryContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                Type type3;
                Type type4;
                ReflectionUtils.GetDictionaryKeyValueTypes(type, out type3, out type4);
                if (type3 != null && typeof(IConvertible).IsAssignableFrom(type3))
                {
                    this.CurrentSchema.AdditionalProperties = this.GenerateInternal(type4, Required.Default, false);
                }
            }
            else if (jsonContract is JsonArrayContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Array, valueRequired));
                this.CurrentSchema.Id   = this.GetTypeId(type, false);
                JsonArrayAttribute jsonArrayAttribute = JsonTypeReflector.GetJsonContainerAttribute(type) as JsonArrayAttribute;
                bool flag = jsonArrayAttribute == null || jsonArrayAttribute.AllowNullItems;
                Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                if (collectionItemType != null)
                {
                    this.CurrentSchema.Items = new List <JsonSchema>();
                    this.CurrentSchema.Items.Add(this.GenerateInternal(collectionItemType, flag ? Required.Default : Required.Always, false));
                }
            }
            else if (jsonContract is JsonPrimitiveContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.GetJsonSchemaType(type, valueRequired));
                JsonSchemaType?type5 = this.CurrentSchema.Type;
                if (type5.GetValueOrDefault() == JsonSchemaType.Integer && type5.get_HasValue() && type.get_IsEnum() && !type.IsDefined(typeof(FlagsAttribute), true))
                {
                    this.CurrentSchema.Enum    = new List <JToken>();
                    this.CurrentSchema.Options = new Dictionary <JToken, string>();
                    EnumValues <long> namesAndValues = EnumUtils.GetNamesAndValues <long>(type);
                    using (IEnumerator <EnumValue <long> > enumerator = namesAndValues.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            EnumValue <long> current = enumerator.get_Current();
                            JToken           jToken  = JToken.FromObject(current.Value);
                            this.CurrentSchema.Enum.Add(jToken);
                            this.CurrentSchema.Options.Add(jToken, current.Name);
                        }
                    }
                }
            }
            else if (jsonContract is JsonObjectContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                this.CurrentSchema.Id   = this.GetTypeId(type, false);
                this.GenerateObjectSchema(type, (JsonObjectContract)jsonContract);
            }
            else if (jsonContract is JsonISerializableContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                this.CurrentSchema.Id   = this.GetTypeId(type, false);
                this.GenerateISerializableContract(type, (JsonISerializableContract)jsonContract);
            }
            else if (jsonContract is JsonStringContract)
            {
                JsonSchemaType jsonSchemaType = ReflectionUtils.IsNullable(jsonContract.UnderlyingType) ? this.AddNullType(JsonSchemaType.String, valueRequired) : JsonSchemaType.String;
                this.CurrentSchema.Type = new JsonSchemaType?(jsonSchemaType);
            }
            else
            {
                if (!(jsonContract is JsonLinqContract))
                {
                    throw new Exception("Unexpected contract type: {0}".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                    {
                        jsonContract
                    }));
                }
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            return(this.Pop().Schema);
        }
Exemple #3
0
        // Token: 0x0600118E RID: 4494 RVA: 0x0006175C File Offset: 0x0005F95C
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            ValidationUtils.ArgumentNotNull(type, "type");
            string typeId  = this.GetTypeId(type, false);
            string typeId2 = this.GetTypeId(type, true);

            if (!StringUtils.IsNullOrEmpty(typeId))
            {
                JsonSchema schema = this._resolver.GetSchema(typeId);
                if (schema != null)
                {
                    if (valueRequired != Required.Always && !JsonSchemaGenerator.HasFlag(schema.Type, JsonSchemaType.Null))
                    {
                        schema.Type |= JsonSchemaType.Null;
                    }
                    if (required)
                    {
                        bool?required2 = schema.Required;
                        bool flag      = true;
                        if (!(required2.GetValueOrDefault() == flag & required2 != null))
                        {
                            schema.Required = new bool?(true);
                        }
                    }
                    return(schema);
                }
            }
            if (this._stack.Any((JsonSchemaGenerator.TypeSchema tc) => tc.Type == type))
            {
                throw new JsonException("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.".FormatWith(CultureInfo.InvariantCulture, type));
            }
            JsonContract jsonContract = this.ContractResolver.ResolveContract(type);
            bool         flag2        = (jsonContract.Converter ?? jsonContract.InternalConverter) != null;

            this.Push(new JsonSchemaGenerator.TypeSchema(type, new JsonSchema()));
            if (typeId2 != null)
            {
                this.CurrentSchema.Id = typeId2;
            }
            if (required)
            {
                this.CurrentSchema.Required = new bool?(true);
            }
            this.CurrentSchema.Title       = this.GetTitle(type);
            this.CurrentSchema.Description = this.GetDescription(type);
            if (flag2)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            else
            {
                switch (jsonContract.ContractType)
                {
                case JsonContractType.Object:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateObjectSchema(type, (JsonObjectContract)jsonContract);
                    break;

                case JsonContractType.Array:
                    {
                        this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Array, valueRequired));
                        this.CurrentSchema.Id   = this.GetTypeId(type, false);
                        JsonArrayAttribute cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonArrayAttribute>(type);
                        bool flag3 = cachedAttribute == null || cachedAttribute.AllowNullItems;
                        Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                        if (collectionItemType != null)
                        {
                            this.CurrentSchema.Items = new List <JsonSchema>();
                            this.CurrentSchema.Items.Add(this.GenerateInternal(collectionItemType, (!flag3) ? Required.Always : Required.Default, false));
                        }
                        break;
                    }

                case JsonContractType.Primitive:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.GetJsonSchemaType(type, valueRequired));
                    JsonSchemaType?type2          = this.CurrentSchema.Type;
                    JsonSchemaType jsonSchemaType = JsonSchemaType.Integer;
                    if ((type2.GetValueOrDefault() == jsonSchemaType & type2 != null) && type.IsEnum() && !type.IsDefined(typeof(FlagsAttribute), true))
                    {
                        this.CurrentSchema.Enum = new List <JToken>();
                        EnumInfo enumValuesAndNames = EnumUtils.GetEnumValuesAndNames(type);
                        for (int i = 0; i < enumValuesAndNames.Names.Length; i++)
                        {
                            ulong  value = enumValuesAndNames.Values[i];
                            JToken item  = JToken.FromObject(Enum.ToObject(type, value));
                            this.CurrentSchema.Enum.Add(item);
                        }
                    }
                    break;
                }

                case JsonContractType.String:
                {
                    JsonSchemaType value2 = (!ReflectionUtils.IsNullable(jsonContract.UnderlyingType)) ? JsonSchemaType.String : this.AddNullType(JsonSchemaType.String, valueRequired);
                    this.CurrentSchema.Type = new JsonSchemaType?(value2);
                    break;
                }

                case JsonContractType.Dictionary:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    Type type3;
                    Type type4;
                    ReflectionUtils.GetDictionaryKeyValueTypes(type, out type3, out type4);
                    if (type3 != null && this.ContractResolver.ResolveContract(type3).ContractType == JsonContractType.Primitive)
                    {
                        this.CurrentSchema.AdditionalProperties = this.GenerateInternal(type4, Required.Default, false);
                    }
                    break;
                }

                case JsonContractType.Dynamic:
                case JsonContractType.Linq:
                    this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
                    break;

                case JsonContractType.Serializable:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateISerializableContract(type, (JsonISerializableContract)jsonContract);
                    break;

                default:
                    throw new JsonException("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, jsonContract));
                }
            }
            return(this.Pop().Schema);
        }
Exemple #4
0
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            ValidationUtils.ArgumentNotNull((object)type, "type");
            string typeId1 = this.GetTypeId(type, false);
            string typeId2 = this.GetTypeId(type, true);

            if (!string.IsNullOrEmpty(typeId1))
            {
                JsonSchema schema = this._resolver.GetSchema(typeId1);
                if (schema != null)
                {
                    if (valueRequired != Required.Always && !JsonSchemaGenerator.HasFlag(schema.Type, JsonSchemaType.Null))
                    {
                        JsonSchema     jsonSchema = schema;
                        JsonSchemaType?type1      = jsonSchema.Type;
                        JsonSchemaType?nullable   = type1.HasValue ? new JsonSchemaType?(type1.GetValueOrDefault() | JsonSchemaType.Null) : new JsonSchemaType?();
                        jsonSchema.Type = nullable;
                    }
                    if (required)
                    {
                        bool?required1 = schema.Required;
                        if ((!required1.GetValueOrDefault() ? 1 : (!required1.HasValue ? 1 : 0)) != 0)
                        {
                            schema.Required = new bool?(true);
                        }
                    }
                    return(schema);
                }
            }
            if (Enumerable.Any <JsonSchemaGenerator.TypeSchema>((IEnumerable <JsonSchemaGenerator.TypeSchema>) this._stack, (Func <JsonSchemaGenerator.TypeSchema, bool>)(tc => tc.Type == type)))
            {
                throw new JsonException(StringUtils.FormatWith("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.", (IFormatProvider)CultureInfo.InvariantCulture, (object)type));
            }
            JsonContract  jsonContract = this.ContractResolver.ResolveContract(type);
            JsonConverter jsonConverter;

            if ((jsonConverter = jsonContract.Converter) != null || (jsonConverter = jsonContract.InternalConverter) != null)
            {
                JsonSchema schema = jsonConverter.GetSchema();
                if (schema != null)
                {
                    return(schema);
                }
            }
            this.Push(new JsonSchemaGenerator.TypeSchema(type, new JsonSchema()));
            if (typeId2 != null)
            {
                this.CurrentSchema.Id = typeId2;
            }
            if (required)
            {
                this.CurrentSchema.Required = new bool?(true);
            }
            this.CurrentSchema.Title       = this.GetTitle(type);
            this.CurrentSchema.Description = this.GetDescription(type);
            if (jsonConverter != null)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            else
            {
                switch (jsonContract.ContractType)
                {
                case JsonContractType.Object:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateObjectSchema(type, (JsonObjectContract)jsonContract);
                    break;

                case JsonContractType.Array:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Array, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    JsonArrayAttribute jsonArrayAttribute = JsonTypeReflector.GetJsonContainerAttribute(type) as JsonArrayAttribute;
                    bool flag = jsonArrayAttribute == null || jsonArrayAttribute.AllowNullItems;
                    Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                    if (collectionItemType != null)
                    {
                        this.CurrentSchema.Items = (IList <JsonSchema>) new List <JsonSchema>();
                        this.CurrentSchema.Items.Add(this.GenerateInternal(collectionItemType, !flag ? Required.Always : Required.Default, false));
                        break;
                    }
                    else
                    {
                        break;
                    }

                case JsonContractType.Primitive:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.GetJsonSchemaType(type, valueRequired));
                    JsonSchemaType?type2 = this.CurrentSchema.Type;
                    if ((type2.GetValueOrDefault() != JsonSchemaType.Integer ? 0 : (type2.HasValue ? 1 : 0)) != 0 && TypeExtensions.IsEnum(type) && !type.IsDefined(typeof(FlagsAttribute), true))
                    {
                        this.CurrentSchema.Enum    = (IList <JToken>) new List <JToken>();
                        this.CurrentSchema.Options = (IDictionary <JToken, string>) new Dictionary <JToken, string>();
                        using (IEnumerator <EnumValue <long> > enumerator = EnumUtils.GetNamesAndValues <long>(type).GetEnumerator())
                        {
                            while (enumerator.MoveNext())
                            {
                                EnumValue <long> current = enumerator.Current;
                                JToken           key     = JToken.FromObject((object)current.Value);
                                this.CurrentSchema.Enum.Add(key);
                                this.CurrentSchema.Options.Add(key, current.Name);
                            }
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }

                case JsonContractType.String:
                    this.CurrentSchema.Type = new JsonSchemaType?(!ReflectionUtils.IsNullable(jsonContract.UnderlyingType) ? JsonSchemaType.String : this.AddNullType(JsonSchemaType.String, valueRequired));
                    break;

                case JsonContractType.Dictionary:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    Type keyType;
                    Type valueType;
                    ReflectionUtils.GetDictionaryKeyValueTypes(type, out keyType, out valueType);
                    if (keyType != null && ConvertUtils.IsConvertible(keyType))
                    {
                        this.CurrentSchema.AdditionalProperties = this.GenerateInternal(valueType, Required.Default, false);
                        break;
                    }
                    else
                    {
                        break;
                    }

                case JsonContractType.Serializable:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateISerializableContract(type, (JsonISerializableContract)jsonContract);
                    break;

                case JsonContractType.Linq:
                    this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
                    break;

                default:
                    throw new JsonException(StringUtils.FormatWith("Unexpected contract type: {0}", (IFormatProvider)CultureInfo.InvariantCulture, (object)jsonContract));
                }
            }
            return(this.Pop().Schema);
        }
Exemple #5
0
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            JsonSchemaType?nullable;
            Type           type1;
            Type           type2;
            JsonSchemaType?nullable1;

            ValidationUtils.ArgumentNotNull(type, "type");
            string typeId = this.GetTypeId(type, false);
            string str    = this.GetTypeId(type, true);

            if (!string.IsNullOrEmpty(typeId))
            {
                JsonSchema schema = this._resolver.GetSchema(typeId);
                if (schema != null)
                {
                    if (valueRequired != Required.Always && !JsonSchemaGenerator.HasFlag(schema.Type, JsonSchemaType.Null))
                    {
                        JsonSchema     jsonSchema = schema;
                        JsonSchemaType?nullable2  = jsonSchema.Type;
                        if (!nullable2.HasValue)
                        {
                            nullable  = null;
                            nullable1 = nullable;
                        }
                        else
                        {
                            nullable1 = new JsonSchemaType?(nullable2.GetValueOrDefault() | JsonSchemaType.Null);
                        }
                        jsonSchema.Type = nullable1;
                    }
                    if (required)
                    {
                        bool?nullable3 = schema.Required;
                        if ((!nullable3.GetValueOrDefault() ? true : !nullable3.HasValue))
                        {
                            schema.Required = new bool?(true);
                        }
                    }
                    return(schema);
                }
            }
            if (this._stack.Any <JsonSchemaGenerator.TypeSchema>((JsonSchemaGenerator.TypeSchema tc) => tc.Type == type))
            {
                throw new Exception("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.".FormatWith(CultureInfo.InvariantCulture, new object[] { type }));
            }
            JsonContract  jsonContract  = this.ContractResolver.ResolveContract(type);
            JsonConverter converter     = jsonContract.Converter;
            JsonConverter jsonConverter = converter;

            if (converter == null)
            {
                JsonConverter internalConverter = jsonContract.InternalConverter;
                jsonConverter = internalConverter;
                if (internalConverter == null)
                {
                    goto Label0;
                }
            }
            JsonSchema schema1 = jsonConverter.GetSchema();

            if (schema1 != null)
            {
                return(schema1);
            }
Label0:
            this.Push(new JsonSchemaGenerator.TypeSchema(type, new JsonSchema()));
            if (str != null)
            {
                this.CurrentSchema.Id = str;
            }
            if (required)
            {
                this.CurrentSchema.Required = new bool?(true);
            }
            this.CurrentSchema.Title       = this.GetTitle(type);
            this.CurrentSchema.Description = this.GetDescription(type);
            if (jsonConverter != null)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            else if (jsonContract is JsonDictionaryContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                ReflectionUtils.GetDictionaryKeyValueTypes(type, out type1, out type2);
                if (type1 != null && typeof(IConvertible).IsAssignableFrom(type1))
                {
                    this.CurrentSchema.AdditionalProperties = this.GenerateInternal(type2, Required.Default, false);
                }
            }
            else if (jsonContract is JsonArrayContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Array, valueRequired));
                this.CurrentSchema.Id   = this.GetTypeId(type, false);
                JsonArrayAttribute jsonContainerAttribute = JsonTypeReflector.GetJsonContainerAttribute(type) as JsonArrayAttribute;
                bool flag = (jsonContainerAttribute == null ? true : jsonContainerAttribute.AllowNullItems);
                Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                if (collectionItemType != null)
                {
                    this.CurrentSchema.Items = new List <JsonSchema>()
                    {
                        this.GenerateInternal(collectionItemType, (flag ? Required.Default : Required.Always), false)
                    };
                }
            }
            else if (jsonContract is JsonPrimitiveContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.GetJsonSchemaType(type, valueRequired));
                nullable = this.CurrentSchema.Type;
                if ((nullable.GetValueOrDefault() != JsonSchemaType.Integer ? false : nullable.HasValue) && type.IsEnum && !type.IsDefined(typeof(FlagsAttribute), true))
                {
                    this.CurrentSchema.Enum    = new List <JToken>();
                    this.CurrentSchema.Options = new Dictionary <JToken, string>();
                    foreach (EnumValue <long> namesAndValue in EnumUtils.GetNamesAndValues <long>(type))
                    {
                        JToken jTokens = JToken.FromObject(namesAndValue.Value);
                        this.CurrentSchema.Enum.Add(jTokens);
                        this.CurrentSchema.Options.Add(jTokens, namesAndValue.Name);
                    }
                }
            }
            else if (jsonContract is JsonObjectContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                this.CurrentSchema.Id   = this.GetTypeId(type, false);
                this.GenerateObjectSchema(type, (JsonObjectContract)jsonContract);
            }
            else if (jsonContract is JsonISerializableContract)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                this.CurrentSchema.Id   = this.GetTypeId(type, false);
                this.GenerateISerializableContract(type, (JsonISerializableContract)jsonContract);
            }
            else if (!(jsonContract is JsonStringContract))
            {
                if (!(jsonContract is JsonLinqContract))
                {
                    throw new Exception("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, new object[] { jsonContract }));
                }
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            else
            {
                this.CurrentSchema.Type = new JsonSchemaType?((ReflectionUtils.IsNullable(jsonContract.UnderlyingType) ? this.AddNullType(JsonSchemaType.String, valueRequired) : JsonSchemaType.String));
            }
            return(this.Pop().Schema);
        }
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            ValidationUtils.ArgumentNotNull(type, "type");
            string typeId  = this.GetTypeId(type, false);
            string typeId2 = this.GetTypeId(type, true);

            if (!string.IsNullOrEmpty(typeId))
            {
                JsonSchema schema = this._resolver.GetSchema(typeId);
                if (schema != null)
                {
                    if (valueRequired != Required.Always && !JsonSchemaGenerator.HasFlag(schema.Type, JsonSchemaType.Null))
                    {
                        schema.Type |= JsonSchemaType.Null;
                    }
                    if (required && schema.Required != true)
                    {
                        schema.Required = new bool?(true);
                    }
                    return(schema);
                }
            }
            if (this._stack.Any((JsonSchemaGenerator.TypeSchema tc) => tc.Type == type))
            {
                throw new JsonException("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.".FormatWith(CultureInfo.InvariantCulture, type));
            }
            JsonContract  jsonContract = this.ContractResolver.ResolveContract(type);
            JsonConverter jsonConverter;

            if ((jsonConverter = jsonContract.Converter) != null || (jsonConverter = jsonContract.InternalConverter) != null)
            {
                JsonSchema schema2 = jsonConverter.GetSchema();
                if (schema2 != null)
                {
                    return(schema2);
                }
            }
            this.Push(new JsonSchemaGenerator.TypeSchema(type, new JsonSchema()));
            if (typeId2 != null)
            {
                this.CurrentSchema.Id = typeId2;
            }
            if (required)
            {
                this.CurrentSchema.Required = new bool?(true);
            }
            this.CurrentSchema.Title       = this.GetTitle(type);
            this.CurrentSchema.Description = this.GetDescription(type);
            if (jsonConverter != null)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            else
            {
                switch (jsonContract.ContractType)
                {
                case JsonContractType.Object:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateObjectSchema(type, (JsonObjectContract)jsonContract);
                    goto IL_51E;

                case JsonContractType.Array:
                    {
                        this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Array, valueRequired));
                        this.CurrentSchema.Id   = this.GetTypeId(type, false);
                        JsonArrayAttribute jsonArrayAttribute = JsonTypeReflector.GetJsonContainerAttribute(type) as JsonArrayAttribute;
                        bool flag = jsonArrayAttribute == null || jsonArrayAttribute.AllowNullItems;
                        Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                        if (collectionItemType != null)
                        {
                            this.CurrentSchema.Items = new List <JsonSchema>();
                            this.CurrentSchema.Items.Add(this.GenerateInternal(collectionItemType, (!flag) ? Required.Always : Required.Default, false));
                            goto IL_51E;
                        }
                        goto IL_51E;
                    }

                case JsonContractType.Primitive:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.GetJsonSchemaType(type, valueRequired));
                    if (!(this.CurrentSchema.Type == JsonSchemaType.Integer) || !type.IsEnum() || type.IsDefined(typeof(FlagsAttribute), true))
                    {
                        goto IL_51E;
                    }
                    this.CurrentSchema.Enum = new List <JToken>();
                    EnumValues <long> namesAndValues = EnumUtils.GetNamesAndValues <long>(type);
                    using (IEnumerator <EnumValue <long> > enumerator = namesAndValues.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            EnumValue <long> current = enumerator.Current;
                            JToken           item    = JToken.FromObject(current.Value);
                            this.CurrentSchema.Enum.Add(item);
                        }
                        goto IL_51E;
                    }
                    break;
                }

                case JsonContractType.String:
                    break;

                case JsonContractType.Dictionary:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    Type type2;
                    Type type3;
                    ReflectionUtils.GetDictionaryKeyValueTypes(type, out type2, out type3);
                    if (!(type2 != null))
                    {
                        goto IL_51E;
                    }
                    JsonContract jsonContract2 = this.ContractResolver.ResolveContract(type2);
                    if (jsonContract2.ContractType == JsonContractType.Primitive)
                    {
                        this.CurrentSchema.AdditionalProperties = this.GenerateInternal(type3, Required.Default, false);
                        goto IL_51E;
                    }
                    goto IL_51E;
                }

                case JsonContractType.Dynamic:
                case JsonContractType.Linq:
                    this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
                    goto IL_51E;

                case JsonContractType.Serializable:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateISerializableContract(type, (JsonISerializableContract)jsonContract);
                    goto IL_51E;

                default:
                    throw new JsonException("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, jsonContract));
                }
                JsonSchemaType value = (!ReflectionUtils.IsNullable(jsonContract.UnderlyingType)) ? JsonSchemaType.String : this.AddNullType(JsonSchemaType.String, valueRequired);
                this.CurrentSchema.Type = new JsonSchemaType?(value);
            }
            IL_51E:
            return(this.Pop().Schema);
        }