Example #1
0
        private JsonSchemaType LoadJsonSchemaType(JObject schema)
        {
            JProperty type = schema.Property("type");

            if (type == null)
            {
                return(JsonSchemaType.Any);
            }

            var arrayOfTypes = type.Value as JArray;

            if (arrayOfTypes != null)
            {
                JsonSchemaType result = JsonSchemaType.None;
                foreach (string typeName in arrayOfTypes.Values <string>())
                {
                    result |= s_jsonSchemaTypeMapping[typeName];
                }
                return(result);
            }

            return(s_jsonSchemaTypeMapping[type.Value.Value <string>()]);
        }
Example #2
0
        // Token: 0x06001193 RID: 4499 RVA: 0x00061DB4 File Offset: 0x0005FFB4
        internal static bool HasFlag(JsonSchemaType?value, JsonSchemaType flag)
        {
            if (value == null)
            {
                return(true);
            }
            JsonSchemaType?jsonSchemaType = value & flag;

            if (jsonSchemaType.GetValueOrDefault() == flag & jsonSchemaType != null)
            {
                return(true);
            }
            if (flag == JsonSchemaType.Integer)
            {
                jsonSchemaType = (value & JsonSchemaType.Float);
                JsonSchemaType jsonSchemaType2 = JsonSchemaType.Float;
                if (jsonSchemaType.GetValueOrDefault() == jsonSchemaType2 & jsonSchemaType != null)
                {
                    return(true);
                }
            }
            return(false);
        }
Example #3
0
        internal static bool HasFlag(JsonSchemaType?value, JsonSchemaType flag)
        {
            // default value is Any
            if (value == null)
            {
                return(true);
            }

            bool match = ((value & flag) == flag);

            if (match)
            {
                return(true);
            }

            // integer is a subset of float
            if (flag == JsonSchemaType.Integer && (value & JsonSchemaType.Float) == JsonSchemaType.Float)
            {
                return(true);
            }

            return(false);
        }
Example #4
0
        private void method_4(string string_0, JsonWriter jsonWriter_1, JsonSchemaType jsonSchemaType_0)
        {
            IList <JsonSchemaType> list;

            if (Enum.IsDefined(typeof(JsonSchemaType), jsonSchemaType_0))
            {
                list = new List <JsonSchemaType> {
                    jsonSchemaType_0
                };
            }
            else
            {
                if (func_0 == null)
                {
                    func_0 = new Func <JsonSchemaType, bool>(Class125.smethod_0);
                }
                list = Class186.smethod_0 <JsonSchemaType>(jsonSchemaType_0).Where <JsonSchemaType>(func_0).ToList <JsonSchemaType>();
            }
            if (list.Count != 0)
            {
                jsonWriter_1.WritePropertyName(string_0);
                if (list.Count == 1)
                {
                    jsonWriter_1.WriteValue(Class130.smethod_1(list[0]));
                }
                else
                {
                    jsonWriter_1.WriteStartArray();
                    foreach (JsonSchemaType type in list)
                    {
                        jsonWriter_1.WriteValue(Class130.smethod_1(type));
                    }
                    jsonWriter_1.WriteEndArray();
                }
            }
        }
Example #5
0
        private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
        {
            IList <JsonSchemaType> types;

            if (System.Enum.IsDefined(typeof(JsonSchemaType), type))
            {
                types = new List <JsonSchemaType> {
                    type
                }
            }
            ;
            else
            {
                types = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).ToList();
            }

            if (types.Count == 0)
            {
                return;
            }

            writer.WritePropertyName(propertyName);

            if (types.Count == 1)
            {
                writer.WriteValue(JsonSchemaBuilder.MapType(types[0]));
                return;
            }

            writer.WriteStartArray();
            foreach (JsonSchemaType jsonSchemaType in types)
            {
                writer.WriteValue(JsonSchemaBuilder.MapType(jsonSchemaType));
            }
            writer.WriteEndArray();
        }
        private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
        {
            IList <JsonSchemaType> list;

            if (Enum.IsDefined(typeof(JsonSchemaType), type))
            {
                List <JsonSchemaType> list1 = new List <JsonSchemaType> {
                    type
                };
                list = list1;
            }
            else
            {
                if (< > c.< > 9__7_0 == null)
                {
                }
                list = EnumUtils.GetFlagsValues <JsonSchemaType>(type).Where <JsonSchemaType>((< > c.< > 9__7_0 = new Func <JsonSchemaType, bool>(< > c.< > 9. < WriteType > b__7_0))).ToList <JsonSchemaType>();
            }
            if (list.Count != 0)
            {
                writer.WritePropertyName(propertyName);
                if (list.Count == 1)
                {
                    writer.WriteValue(JsonSchemaBuilder.MapType(list[0]));
                }
                else
                {
                    writer.WriteStartArray();
                    foreach (JsonSchemaType type2 in list)
                    {
                        writer.WriteValue(JsonSchemaBuilder.MapType(type2));
                    }
                    writer.WriteEndArray();
                }
            }
        }
    private JsonSchemaType AddNullType(JsonSchemaType type, Required valueRequired)
    {
      if (valueRequired != Required.Always)
        return type | JsonSchemaType.Null;

      return type;
    }
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            ValidationUtils.ArgumentNotNull(type, "type");

            string resolvedId = GetTypeId(type, false);
            string explicitId = GetTypeId(type, true);

            if (!string.IsNullOrEmpty(resolvedId))
            {
                JsonSchema resolvedSchema = _resolver.GetSchema(resolvedId);
                if (resolvedSchema != null)
                {
                    // resolved schema is not null but referencing member allows nulls
                    // change resolved schema to allow nulls. hacky but what are ya gonna do?
                    if (valueRequired != Required.Always && !HasFlag(resolvedSchema.Type, JsonSchemaType.Null))
                    {
                        resolvedSchema.Type |= JsonSchemaType.Null;
                    }
                    if (required && resolvedSchema.Required != true)
                    {
                        resolvedSchema.Required = true;
                    }

                    return(resolvedSchema);
                }
            }

            // test for unresolved circular reference
            if (_stack.Any(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, type));
            }

            JsonContract  contract = ContractResolver.ResolveContract(type);
            JsonConverter converter;

            if ((converter = contract.Converter) != null || (converter = contract.InternalConverter) != null)
            {
                JsonSchema converterSchema = converter.GetSchema();
                if (converterSchema != null)
                {
                    return(converterSchema);
                }
            }

            Push(new TypeSchema(type, new JsonSchema()));

            if (explicitId != null)
            {
                CurrentSchema.Id = explicitId;
            }

            if (required)
            {
                CurrentSchema.Required = true;
            }
            CurrentSchema.Title       = GetTitle(type);
            CurrentSchema.Description = GetDescription(type);

            if (converter != null)
            {
                // todo: Add GetSchema to JsonConverter and use here?
                CurrentSchema.Type = JsonSchemaType.Any;
            }
            else
            {
                switch (contract.ContractType)
                {
                case JsonContractType.Object:
                    CurrentSchema.Type = AddNullType(JsonSchemaType.Object, valueRequired);
                    CurrentSchema.Id   = GetTypeId(type, false);
                    GenerateObjectSchema(type, (JsonObjectContract)contract);
                    break;

                case JsonContractType.Array:
                    CurrentSchema.Type = AddNullType(JsonSchemaType.Array, valueRequired);

                    CurrentSchema.Id = GetTypeId(type, false);

                    JsonArrayAttribute arrayAttribute = JsonTypeReflector.GetJsonContainerAttribute(type) as JsonArrayAttribute;
                    bool allowNullItem = (arrayAttribute == null || arrayAttribute.AllowNullItems);

                    Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                    if (collectionItemType != null)
                    {
                        CurrentSchema.Items = new List <JsonSchema>();
                        CurrentSchema.Items.Add(GenerateInternal(collectionItemType, (!allowNullItem) ? Required.Always : Required.Default, false));
                    }
                    break;

                case JsonContractType.Primitive:
                    CurrentSchema.Type = GetJsonSchemaType(type, valueRequired);

                    if (CurrentSchema.Type == JsonSchemaType.Integer && type.IsEnum && !type.IsDefined(typeof(FlagsAttribute), true))
                    {
                        CurrentSchema.Enum    = new List <JToken>();
                        CurrentSchema.Options = new Dictionary <JToken, string>();

                        EnumValues <long> enumValues = EnumUtils.GetNamesAndValues <long>(type);
                        foreach (EnumValue <long> enumValue in enumValues)
                        {
                            JToken value = JToken.FromObject(enumValue.Value);

                            CurrentSchema.Enum.Add(value);
                            CurrentSchema.Options.Add(value, enumValue.Name);
                        }
                    }
                    break;

                case JsonContractType.String:
                    JsonSchemaType schemaType = (!ReflectionUtils.IsNullable(contract.UnderlyingType))
                                          ? JsonSchemaType.String
                                          : AddNullType(JsonSchemaType.String, valueRequired);

                    CurrentSchema.Type = schemaType;
                    break;

                case JsonContractType.Dictionary:
                    CurrentSchema.Type = AddNullType(JsonSchemaType.Object, valueRequired);

                    Type keyType;
                    Type valueType;
                    ReflectionUtils.GetDictionaryKeyValueTypes(type, out keyType, out valueType);

                    if (keyType != null)
                    {
                        // can be converted to a string
                        if (typeof(IConvertible).IsAssignableFrom(keyType))
                        {
                            CurrentSchema.AdditionalProperties = GenerateInternal(valueType, Required.Default, false);
                        }
                    }
                    break;

#if !SILVERLIGHT && !PocketPC
                case JsonContractType.Serializable:
                    CurrentSchema.Type = AddNullType(JsonSchemaType.Object, valueRequired);
                    CurrentSchema.Id   = GetTypeId(type, false);
                    GenerateISerializableContract(type, (JsonISerializableContract)contract);
                    break;
#endif
#if !(NET35 || NET20 || WINDOWS_PHONE)
                case JsonContractType.Dynamic:
#endif
                case JsonContractType.Linq:
                    CurrentSchema.Type = JsonSchemaType.Any;
                    break;

                default:
                    throw new Exception("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, contract));
                }
            }

            return(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);
        }
        private static string JsonSchemaTypesToLabel(JsonSchemaType type)
        {
            var types = new string[6];

            int index = 0;

            if ((type & JsonSchemaType.String) == JsonSchemaType.String)
                types[index++] = "string";
            if ((type & JsonSchemaType.Float) == JsonSchemaType.Float || (type & JsonSchemaType.Integer) == JsonSchemaType.Integer)
                types[index++] = "number";
            if ((type & JsonSchemaType.Boolean) == JsonSchemaType.Boolean)
                types[index++] = "boolean";
            if ((type & JsonSchemaType.Object) == JsonSchemaType.Object)
                types[index++] = "object";
            if ((type & JsonSchemaType.Array) == JsonSchemaType.Array)
                types[index++] = "array";
            if ((type & JsonSchemaType.Null) == JsonSchemaType.Null)
                types[index++] = "null";

            switch (index)
            {
                case 0:
                    return "";
                case 1:
                    return types[0];
                case 2:
                    return types[0] + " or " + types[1];
                default:
                    return string.Join(", ", types, 0, index - 1) + ", or " + types[index - 1];
            }
        }
 /// <summary>
 /// Builds an object from a <see cref="JsonValue"/>.
 /// </summary>
 /// <param name="json">The <see cref="JsonValue"/> representation of the object.</param>
 /// <param name="serializer">The <see cref="JsonSerializer"/> instance to use for additional
 /// serialization of values.</param>
 public void FromJson(JsonValue json, JsonSerializer serializer)
 {
     Value = json.ToSchemaType();
 }
Example #12
0
 // Token: 0x06001176 RID: 4470 RVA: 0x000613F4 File Offset: 0x0005F5F4
 internal static string MapType(JsonSchemaType type)
 {
     return(JsonSchemaConstants.JsonSchemaTypeMapping.Single((KeyValuePair <string, JsonSchemaType> kv) => kv.Value == type).Key);
 }
Example #13
0
 /// <summary>
 ///     Get the primitive name of a type if it exists.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>The primitive type as a string, if it exists.</returns>
 public static string GetPrimitiveTypeAsString(JsonSchemaType? type)
 {
     string sType = type.ToString();
     return Primitives.ContainsKey(sType) ? Primitives[sType] : "System.Object";
 }
Example #14
0
        public Schemas.JsonSchemaBase Create(bool useUpperCamelName, string rootName = default)
        {
            // if (baseSchema != null)
            // {
            //     baseSchema.MergeTo(this);
            // }

            if (baseSchema != null)
            {
                if (type == JsonSchemaType.Unknown)
                {
                    type = baseSchema.type;
                }
            }

            switch (type)
            {
            case JsonSchemaType.Object:
                if (this.JsonPath.EndsWith(".extensions") || this.JsonPath.EndsWith(".extras"))
                {
                    return(new Schemas.ExtensionJsonSchema(this));
                }

                if ((m_properties != null && m_properties.Any()) || additionalProperties is null)
                {
                    var obj = new Schemas.ObjectJsonSchema(this, useUpperCamelName);
                    if (!string.IsNullOrEmpty(rootName))
                    {
                        obj.Title = rootName;
                    }
                    return(obj);
                }
                else
                {
                    return(new Schemas.DictionaryJsonSchema(this, useUpperCamelName));
                }

            case JsonSchemaType.Array:
                return(new Schemas.ArrayJsonSchema(this, useUpperCamelName));

            case JsonSchemaType.Boolean:
                return(new Schemas.BoolJsonSchema(this));

            case JsonSchemaType.String:
                return(new Schemas.StringJsonSchema(this));

            case JsonSchemaType.Number:
                return(new Schemas.NumberJsonSchema(this));

            case JsonSchemaType.Integer:
                return(new Schemas.IntegerJsonSchema(this));

            case JsonSchemaType.Enum:
                return(new Schemas.EnumJsonSchema(this));

            case JsonSchemaType.EnumString:
                return(new Schemas.EnumStringJsonSchema(this));

            default:
                return(null);
            }
        }
Example #15
0
 internal static bool HasFlag(JsonSchemaType?value, JsonSchemaType flag)
 {
     return(value == null || ((value == null) ? null : new JsonSchemaType?(value.GetValueOrDefault() & flag)) == flag);
 }
 // Token: 0x06000B8C RID: 2956
 // RVA: 0x000431E0 File Offset: 0x000413E0
 internal static bool HasFlag(JsonSchemaType? value, JsonSchemaType flag)
 {
     return !value.HasValue || (value & flag) == flag || (flag == JsonSchemaType.Integer && (value & JsonSchemaType.Float) == JsonSchemaType.Float);
 }
Example #17
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);
        }
 // Token: 0x06000A0F RID: 2575
 // RVA: 0x0003F7F8 File Offset: 0x0003D9F8
 private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
 {
     IList<JsonSchemaType> list;
     if (Enum.IsDefined(typeof(JsonSchemaType), type))
     {
         list = new List<JsonSchemaType>
         {
             type
         };
     }
     else
     {
         list = Enumerable.ToList<JsonSchemaType>(Enumerable.Where<JsonSchemaType>(EnumUtils.GetFlagsValues<JsonSchemaType>(type), (JsonSchemaType v) => v != JsonSchemaType.None));
     }
     if (list.Count == 0)
     {
         return;
     }
     writer.WritePropertyName(propertyName);
     if (list.Count == 1)
     {
         writer.WriteValue(JsonSchemaBuilder.MapType(list[0]));
         return;
     }
     writer.WriteStartArray();
     foreach (JsonSchemaType current in list)
     {
         writer.WriteValue(JsonSchemaBuilder.MapType(current));
     }
     writer.WriteEndArray();
 }
Example #19
0
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            ValidationUtils.ArgumentNotNull(type, "type");
            string typeId  = GetTypeId(type, explicitOnly: false);
            string typeId2 = GetTypeId(type, explicitOnly: true);

            if (!string.IsNullOrEmpty(typeId))
            {
                JsonSchema schema = _resolver.GetSchema(typeId);
                if (schema != null)
                {
                    if (valueRequired != Required.Always && !HasFlag(schema.Type, JsonSchemaType.Null))
                    {
                        schema.Type |= JsonSchemaType.Null;
                    }
                    if (required && schema.Required != true)
                    {
                        schema.Required = true;
                    }
                    return(schema);
                }
            }
            if (_stack.Any((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, type));
            }
            JsonContract  jsonContract = ContractResolver.ResolveContract(type);
            JsonConverter jsonConverter;

            if ((jsonConverter = jsonContract.Converter) != null || (jsonConverter = jsonContract.InternalConverter) != null)
            {
                JsonSchema schema2 = jsonConverter.GetSchema();
                if (schema2 != null)
                {
                    return(schema2);
                }
            }
            Push(new TypeSchema(type, new JsonSchema()));
            if (typeId2 != null)
            {
                CurrentSchema.Id = typeId2;
            }
            if (required)
            {
                CurrentSchema.Required = true;
            }
            CurrentSchema.Title       = GetTitle(type);
            CurrentSchema.Description = GetDescription(type);
            if (jsonConverter != null)
            {
                CurrentSchema.Type = JsonSchemaType.Any;
            }
            else if (jsonContract is JsonDictionaryContract)
            {
                CurrentSchema.Type = AddNullType(JsonSchemaType.Object, valueRequired);
                ReflectionUtils.GetDictionaryKeyValueTypes(type, out Type keyType, out Type valueType);
                if (keyType != null && typeof(IConvertible).IsAssignableFrom(keyType))
                {
                    CurrentSchema.AdditionalProperties = GenerateInternal(valueType, Required.Default, required: false);
                }
            }
            else if (jsonContract is JsonArrayContract)
            {
                CurrentSchema.Type = AddNullType(JsonSchemaType.Array, valueRequired);
                CurrentSchema.Id   = GetTypeId(type, explicitOnly: false);
                bool flag = (JsonTypeReflector.GetJsonContainerAttribute(type) as JsonArrayAttribute)?.AllowNullItems ?? true;
                Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                if (collectionItemType != null)
                {
                    CurrentSchema.Items = new List <JsonSchema>();
                    CurrentSchema.Items.Add(GenerateInternal(collectionItemType, (!flag) ? Required.Always : Required.Default, required: false));
                }
            }
            else if (jsonContract is JsonPrimitiveContract)
            {
                CurrentSchema.Type = GetJsonSchemaType(type, valueRequired);
                if (CurrentSchema.Type == JsonSchemaType.Integer && type.IsEnum && !type.IsDefined(typeof(FlagsAttribute), inherit: true))
                {
                    CurrentSchema.Enum    = new List <JToken>();
                    CurrentSchema.Options = new Dictionary <JToken, string>();
                    EnumValues <long> namesAndValues = EnumUtils.GetNamesAndValues <long>(type);
                    foreach (EnumValue <long> item in namesAndValues)
                    {
                        JToken jToken = JToken.FromObject(item.Value);
                        CurrentSchema.Enum.Add(jToken);
                        CurrentSchema.Options.Add(jToken, item.Name);
                    }
                }
            }
            else if (jsonContract is JsonObjectContract)
            {
                CurrentSchema.Type = AddNullType(JsonSchemaType.Object, valueRequired);
                CurrentSchema.Id   = GetTypeId(type, explicitOnly: false);
                GenerateObjectSchema(type, (JsonObjectContract)jsonContract);
            }
            else if (jsonContract is JsonISerializableContract)
            {
                CurrentSchema.Type = AddNullType(JsonSchemaType.Object, valueRequired);
                CurrentSchema.Id   = GetTypeId(type, explicitOnly: false);
                GenerateISerializableContract(type, (JsonISerializableContract)jsonContract);
            }
            else if (jsonContract is JsonStringContract)
            {
                JsonSchemaType value = (!ReflectionUtils.IsNullable(jsonContract.UnderlyingType)) ? JsonSchemaType.String : AddNullType(JsonSchemaType.String, valueRequired);
                CurrentSchema.Type = value;
            }
            else
            {
                if (!(jsonContract is JsonLinqContract))
                {
                    throw new Exception("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, jsonContract));
                }
                CurrentSchema.Type = JsonSchemaType.Any;
            }
            return(Pop().Schema);
        }
 // Token: 0x06000B75 RID: 2933
 // RVA: 0x00042A00 File Offset: 0x00040C00
 internal static string MapType(JsonSchemaType type)
 {
     return Enumerable.Single<KeyValuePair<string, JsonSchemaType>>(JsonSchemaConstants.JsonSchemaTypeMapping, (KeyValuePair<string, JsonSchemaType> kv) => kv.Value == type).Key;
 }
Example #21
0
 public JSchema(JsonSchemaType type, JsonSchemaExtendedType extendedType)
 {
     ExtendedType = extendedType;
     Type         = type;
     Required     = false;
 }
        private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
        {
            IList<JsonSchemaType> types;
            if (System.Enum.IsDefined(typeof(JsonSchemaType), type))
            {
                types = new List<JsonSchemaType> { type };
            }
            else
            {
                types = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).ToList();
            }

            if (types.Count == 0)
            {
                return;
            }

            writer.WritePropertyName(propertyName);

            if (types.Count == 1)
            {
                writer.WriteValue(JsonSchemaBuilder.MapType(types[0]));
                return;
            }

            writer.WriteStartArray();
            foreach (JsonSchemaType jsonSchemaType in types)
            {
                writer.WriteValue(JsonSchemaBuilder.MapType(jsonSchemaType));
            }
            writer.WriteEndArray();
        }
Example #23
0
 internal static string MapType(JsonSchemaType type)
 {
   return Enumerable.Single<KeyValuePair<string, JsonSchemaType>>((IEnumerable<KeyValuePair<string, JsonSchemaType>>) JsonSchemaConstants.JsonSchemaTypeMapping, (Func<KeyValuePair<string, JsonSchemaType>, bool>) (kv => kv.Value == type)).Key;
 }
Example #24
0
 internal static string MapType(JsonSchemaType type)
 {
     return(JsonSchemaConstants.JsonSchemaTypeMapping
            .Single <KeyValuePair <string, JsonSchemaType> >(
                (Func <KeyValuePair <string, JsonSchemaType>, bool>)(kv => kv.Value == type)).Key);
 }
Example #25
0
 private bool TestType(JsonSchemaModel currentSchema, JsonSchemaType currentType)
 {
   if (JsonSchemaGenerator.HasFlag(new JsonSchemaType?(currentSchema.Type), currentType))
     return true;
   this.RaiseError(StringUtils.FormatWith("Invalid type. Expected {0} but got {1}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) currentSchema.Type, (object) currentType), currentSchema);
   return false;
 }
Example #26
0
        private JsonSchemaType GetJsonSchemaType(Type type, Required valueRequired)
        {
            JsonSchemaType schemaType = JsonSchemaType.None;

            if (valueRequired != Required.Always && ReflectionUtils.IsNullable(type))
            {
                schemaType = JsonSchemaType.Null;
                if (ReflectionUtils.IsNullableType(type))
                {
                    type = Nullable.GetUnderlyingType(type);
                }
            }

            PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(type);

            switch (typeCode)
            {
            case PrimitiveTypeCode.Empty:
            case PrimitiveTypeCode.Object:
                return(schemaType | JsonSchemaType.String);

#if !(NETFX_CORE || PORTABLE)
            case PrimitiveTypeCode.DBNull:
                return(schemaType | JsonSchemaType.Null);
#endif
            case PrimitiveTypeCode.Boolean:
                return(schemaType | JsonSchemaType.Boolean);

            case PrimitiveTypeCode.Char:
                return(schemaType | JsonSchemaType.String);

            case PrimitiveTypeCode.SByte:
            case PrimitiveTypeCode.Byte:
            case PrimitiveTypeCode.Int16:
            case PrimitiveTypeCode.UInt16:
            case PrimitiveTypeCode.Int32:
            case PrimitiveTypeCode.UInt32:
            case PrimitiveTypeCode.Int64:
            case PrimitiveTypeCode.UInt64:
#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
            case PrimitiveTypeCode.BigInteger:
#endif
                return(schemaType | JsonSchemaType.Integer);

            case PrimitiveTypeCode.Single:
            case PrimitiveTypeCode.Double:
            case PrimitiveTypeCode.Decimal:
                return(schemaType | JsonSchemaType.Float);

            // convert to string?
            case PrimitiveTypeCode.DateTime:
#if !NET20
            case PrimitiveTypeCode.DateTimeOffset:
#endif
                return(schemaType | JsonSchemaType.String);

            case PrimitiveTypeCode.String:
            case PrimitiveTypeCode.Uri:
            case PrimitiveTypeCode.Guid:
            case PrimitiveTypeCode.TimeSpan:
            case PrimitiveTypeCode.Bytes:
                return(schemaType | JsonSchemaType.String);

            default:
                throw new JsonException("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type));
            }
        }
    internal static bool HasFlag(JsonSchemaType? value, JsonSchemaType flag)
    {
      // default value is Any
      if (value == null)
        return true;

      return ((value & flag) == flag);
    }
 /// <summary>
 /// Creates an instance of the <see cref="TypeKeyword"/>.
 /// </summary>
 public TypeKeyword(JsonSchemaType type)
 {
     Value = type;
 }
Example #29
0
 private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
 {
   IList<JsonSchemaType> list;
   if (Enum.IsDefined(typeof (JsonSchemaType), (object) type))
     list = (IList<JsonSchemaType>) new List<JsonSchemaType>()
     {
       type
     };
   else
     list = (IList<JsonSchemaType>) Enumerable.ToList<JsonSchemaType>(Enumerable.Where<JsonSchemaType>((IEnumerable<JsonSchemaType>) EnumUtils.GetFlagsValues<JsonSchemaType>(type), (Func<JsonSchemaType, bool>) (v => v != JsonSchemaType.None)));
   if (list.Count == 0)
     return;
   writer.WritePropertyName(propertyName);
   if (list.Count == 1)
   {
     writer.WriteValue(JsonSchemaBuilder.MapType(list[0]));
   }
   else
   {
     writer.WriteStartArray();
     foreach (JsonSchemaType type1 in (IEnumerable<JsonSchemaType>) list)
       writer.WriteValue(JsonSchemaBuilder.MapType(type1));
     writer.WriteEndArray();
   }
 }
 internal bool <WriteType> b__7_0(JsonSchemaType v) =>
 (v > JsonSchemaType.None);
Example #31
0
        private JsonSchemaType GetJsonSchemaType(Type type, Required valueRequired)
        {
            JsonSchemaType schemaType = JsonSchemaType.None;

            if (valueRequired != Required.Always && ReflectionUtils.IsNullable(type))
            {
                schemaType = JsonSchemaType.Null;
                if (ReflectionUtils.IsNullableType(type))
                {
                    type = Nullable.GetUnderlyingType(type);
                }
            }

            TypeCode typeCode = Type.GetTypeCode(type);

            switch (typeCode)
            {
            case TypeCode.Empty:
            case TypeCode.Object:
                if (ConvertUtils.CanConvertType(type, typeof(string), false))
                {
                    return(schemaType | JsonSchemaType.String);
                }

                return(schemaType | JsonSchemaType.Object);

            case TypeCode.DBNull:
                return(schemaType | JsonSchemaType.Null);

            case TypeCode.Boolean:
                return(schemaType | JsonSchemaType.Boolean);

            case TypeCode.Char:
                return(schemaType | JsonSchemaType.String);

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
                return(schemaType | JsonSchemaType.Integer);

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                return(schemaType | JsonSchemaType.Float);

            // convert to string?
            case TypeCode.DateTime:
                return(schemaType | JsonSchemaType.String);

            case TypeCode.String:
                return(schemaType | JsonSchemaType.String);

            default:
                throw new Exception("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type));
            }
        }
        private static void Combine(JsonSchemaModel model, JsonSchema schema)
        {
            bool arg_2F_1;

            if (!model.Required)
            {
                bool?required = schema.Required;
                arg_2F_1 = (required.get_HasValue() && required.get_Value());
            }
            else
            {
                arg_2F_1 = true;
            }
            model.Required = arg_2F_1;
            JsonSchemaType arg_5C_0 = model.Type;
            JsonSchemaType?type     = schema.Type;

            model.Type          = (arg_5C_0 & ((!type.get_HasValue()) ? JsonSchemaType.Any : type.get_Value()));
            model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
            model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
            model.DivisibleBy   = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);
            model.Minimum       = MathUtils.Max(model.Minimum, schema.Minimum);
            model.Maximum       = MathUtils.Max(model.Maximum, schema.Maximum);
            bool arg_104_1;

            if (!model.ExclusiveMinimum)
            {
                bool?exclusiveMinimum = schema.ExclusiveMinimum;
                arg_104_1 = (exclusiveMinimum.get_HasValue() && exclusiveMinimum.get_Value());
            }
            else
            {
                arg_104_1 = true;
            }
            model.ExclusiveMinimum = arg_104_1;
            bool arg_138_1;

            if (!model.ExclusiveMaximum)
            {
                bool?exclusiveMaximum = schema.ExclusiveMaximum;
                arg_138_1 = (exclusiveMaximum.get_HasValue() && exclusiveMaximum.get_Value());
            }
            else
            {
                arg_138_1 = true;
            }
            model.ExclusiveMaximum          = arg_138_1;
            model.MinimumItems              = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
            model.MaximumItems              = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
            model.AllowAdditionalProperties = (model.AllowAdditionalProperties && schema.AllowAdditionalProperties);
            if (schema.Enum != null)
            {
                if (model.Enum == null)
                {
                    model.Enum = new List <JToken>();
                }
                model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer());
            }
            JsonSchemaType arg_1E5_0 = model.Disallow;
            JsonSchemaType?disallow  = schema.Disallow;

            model.Disallow = (arg_1E5_0 | ((!disallow.get_HasValue()) ? JsonSchemaType.None : disallow.get_Value()));
            if (schema.Pattern != null)
            {
                if (model.Patterns == null)
                {
                    model.Patterns = new List <string>();
                }
                model.Patterns.AddDistinct(schema.Pattern);
            }
        }
Example #33
0
        private static void Combine(JsonSchemaModel model, JsonSchema schema)
        {
            bool?           required;
            bool            flag;
            bool            flag1;
            bool            flag2;
            JsonSchemaModel jsonSchemaModel = model;

            if (model.Required)
            {
                flag = true;
            }
            else
            {
                required = schema.Required;
                flag     = (required.HasValue ? required.GetValueOrDefault() : false);
            }
            jsonSchemaModel.Required = flag;
            JsonSchemaModel jsonSchemaModel1 = model;
            JsonSchemaType  type             = model.Type;
            JsonSchemaType? disallow         = schema.Type;

            jsonSchemaModel1.Type = type & (disallow.HasValue ? disallow.GetValueOrDefault() : JsonSchemaType.Any);
            model.MinimumLength   = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
            model.MaximumLength   = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
            model.DivisibleBy     = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);
            model.Minimum         = MathUtils.Max(model.Minimum, schema.Minimum);
            model.Maximum         = MathUtils.Max(model.Maximum, schema.Maximum);
            JsonSchemaModel jsonSchemaModel2 = model;

            if (model.ExclusiveMinimum)
            {
                flag1 = true;
            }
            else
            {
                required = schema.ExclusiveMinimum;
                flag1    = (required.HasValue ? required.GetValueOrDefault() : false);
            }
            jsonSchemaModel2.ExclusiveMinimum = flag1;
            JsonSchemaModel jsonSchemaModel3 = model;

            if (model.ExclusiveMaximum)
            {
                flag2 = true;
            }
            else
            {
                required = schema.ExclusiveMaximum;
                flag2    = (required.HasValue ? required.GetValueOrDefault() : false);
            }
            jsonSchemaModel3.ExclusiveMaximum = flag2;
            model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
            model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
            model.PositionalItemsValidation = (model.PositionalItemsValidation ? true : schema.PositionalItemsValidation);
            model.AllowAdditionalProperties = (!model.AllowAdditionalProperties ? false : schema.AllowAdditionalProperties);
            model.AllowAdditionalItems      = (!model.AllowAdditionalItems ? false : schema.AllowAdditionalItems);
            model.UniqueItems = (model.UniqueItems ? true : schema.UniqueItems);
            if (schema.Enum != null)
            {
                if (model.Enum == null)
                {
                    model.Enum = new List <JToken>();
                }
                model.Enum.AddRangeDistinct <JToken>(schema.Enum, JToken.EqualityComparer);
            }
            JsonSchemaModel jsonSchemaModel4 = model;
            JsonSchemaType  jsonSchemaType   = model.Disallow;

            disallow = schema.Disallow;
            jsonSchemaModel4.Disallow = jsonSchemaType | (disallow.HasValue ? disallow.GetValueOrDefault() : JsonSchemaType.None);
            if (schema.Pattern != null)
            {
                if (model.Patterns == null)
                {
                    model.Patterns = new List <string>();
                }
                model.Patterns.AddDistinct <string>(schema.Pattern);
            }
        }
        private JsonSchemaType GetJsonSchemaType(Type type, Required valueRequired)
        {
            JsonSchemaType schemaType = JsonSchemaType.None;

            if (valueRequired != Required.Always && ReflectionUtils.IsNullable(type))
            {
                schemaType = JsonSchemaType.Null;
                if (ReflectionUtils.IsNullableType(type))
                {
                    type = Nullable.GetUnderlyingType(type);
                }
            }

            PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(type);

            switch (typeCode)
            {
            case PrimitiveTypeCode.Empty:
            case PrimitiveTypeCode.Object:
                return(schemaType | JsonSchemaType.String);

#if HAVE_DB_NULL_TYPE_CODE
            case PrimitiveTypeCode.DBNull:
                return(schemaType | JsonSchemaType.Null);
#endif
            case PrimitiveTypeCode.Boolean:
                return(schemaType | JsonSchemaType.Boolean);

            case PrimitiveTypeCode.Char:
                return(schemaType | JsonSchemaType.String);

            case PrimitiveTypeCode.SByte:
            case PrimitiveTypeCode.Byte:
            case PrimitiveTypeCode.Int16:
            case PrimitiveTypeCode.UInt16:
            case PrimitiveTypeCode.Int32:
            case PrimitiveTypeCode.UInt32:
            case PrimitiveTypeCode.Int64:
            case PrimitiveTypeCode.UInt64:
#if HAVE_BIG_INTEGER
            case PrimitiveTypeCode.BigInteger:
#endif
                return(schemaType | JsonSchemaType.Integer);

            case PrimitiveTypeCode.Single:
            case PrimitiveTypeCode.Double:
            case PrimitiveTypeCode.Decimal:
                return(schemaType | JsonSchemaType.Float);

            // convert to string?
            case PrimitiveTypeCode.DateTime:
#if HAVE_DATE_TIME_OFFSET
            case PrimitiveTypeCode.DateTimeOffset:
#endif
                return(schemaType | JsonSchemaType.String);

            case PrimitiveTypeCode.String:
            case PrimitiveTypeCode.Uri:
            case PrimitiveTypeCode.Guid:
            case PrimitiveTypeCode.TimeSpan:
            case PrimitiveTypeCode.Bytes:
                return(schemaType | JsonSchemaType.String);

            default:
                throw new JsonException("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type));
            }
        }
        /// <summary>
        /// Add a <code>type</code> keyword to the schema.
        /// </summary>
        public static JsonSchema Type(this JsonSchema schema, JsonSchemaType type)
        {
            schema.Add(new TypeKeyword(type));

            return(schema);
        }
 internal static string MapType(JsonSchemaType type)
 {
   return JsonSchemaConstants.JsonSchemaTypeMapping.Single(kv => kv.Value == type).Key;
 }
		public static string Map(JsonSchemaType? type)
		{
			return type == null || !typeConversion.ContainsKey(type.Value) ? null : typeConversion[type.Value];
		}
Example #38
0
        private bool TestType(JsonSchemaModel currentSchema, JsonSchemaType currentType)
        {
            if (!JsonSchemaGenerator.HasFlag(currentSchema.Type, currentType))
            {
                RaiseError("Invalid type. Expected {0} but got {1}.".FormatWith(CultureInfo.InvariantCulture, currentSchema.Type, currentType), currentSchema);
                return false;
            }

            return true;
        }
 internal static string MapType(JsonSchemaType type) =>
 JsonSchemaConstants.JsonSchemaTypeMapping.Single <KeyValuePair <string, JsonSchemaType> >(kv => (((JsonSchemaType)kv.Value) == type)).Key;
Example #40
0
        private JsonSchemaType GetJsonSchemaType(Type type, Required valueRequired)
        {
            JsonSchemaType jsonSchemaType = JsonSchemaType.None;

            if (valueRequired != Required.Always && ReflectionUtils.IsNullable(type))
            {
                jsonSchemaType = JsonSchemaType.Null;
                if (ReflectionUtils.IsNullableType(type))
                {
                    type = Nullable.GetUnderlyingType(type);
                }
            }
            PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(type);

            switch (typeCode)
            {
            case PrimitiveTypeCode.Empty:
            case PrimitiveTypeCode.Object:
            {
                return(jsonSchemaType | JsonSchemaType.String);
            }

            case PrimitiveTypeCode.Char:
            {
                return(jsonSchemaType | JsonSchemaType.String);
            }

            case PrimitiveTypeCode.CharNullable:
            case PrimitiveTypeCode.BooleanNullable:
            case PrimitiveTypeCode.SByteNullable:
            case PrimitiveTypeCode.Int16Nullable:
            case PrimitiveTypeCode.UInt16Nullable:
            case PrimitiveTypeCode.Int32Nullable:
            case PrimitiveTypeCode.ByteNullable:
            case PrimitiveTypeCode.UInt32Nullable:
            case PrimitiveTypeCode.Int64Nullable:
            case PrimitiveTypeCode.UInt64Nullable:
            case PrimitiveTypeCode.SingleNullable:
            case PrimitiveTypeCode.DoubleNullable:
            case PrimitiveTypeCode.DateTimeNullable:
            case PrimitiveTypeCode.DateTimeOffsetNullable:
            case PrimitiveTypeCode.DecimalNullable:
            case PrimitiveTypeCode.GuidNullable:
            case PrimitiveTypeCode.TimeSpanNullable:
            case PrimitiveTypeCode.BigIntegerNullable:
            {
                throw new JsonException("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type));
            }

            case PrimitiveTypeCode.Boolean:
            {
                return(jsonSchemaType | JsonSchemaType.Boolean);
            }

            case PrimitiveTypeCode.SByte:
            case PrimitiveTypeCode.Int16:
            case PrimitiveTypeCode.UInt16:
            case PrimitiveTypeCode.Int32:
            case PrimitiveTypeCode.Byte:
            case PrimitiveTypeCode.UInt32:
            case PrimitiveTypeCode.Int64:
            case PrimitiveTypeCode.UInt64:
            case PrimitiveTypeCode.BigInteger:
            {
                return(jsonSchemaType | JsonSchemaType.Integer);
            }

            case PrimitiveTypeCode.Single:
            case PrimitiveTypeCode.Double:
            case PrimitiveTypeCode.Decimal:
            {
                return(jsonSchemaType | JsonSchemaType.Float);
            }

            case PrimitiveTypeCode.DateTime:
            case PrimitiveTypeCode.DateTimeOffset:
            {
                return(jsonSchemaType | JsonSchemaType.String);
            }

            case PrimitiveTypeCode.Guid:
            case PrimitiveTypeCode.TimeSpan:
            case PrimitiveTypeCode.Uri:
            case PrimitiveTypeCode.String:
            case PrimitiveTypeCode.Bytes:
            {
                return(jsonSchemaType | JsonSchemaType.String);
            }

            case PrimitiveTypeCode.DBNull:
            {
                return(jsonSchemaType | JsonSchemaType.Null);
            }

            default:
            {
                throw new JsonException("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type));
            }
            }
        }
        private string JsonSchemaTypesToLabel(JsonSchemaType type)
        {
            var types = new string[6];

            int index = 0;

            if ((type & JsonSchemaType.String) == JsonSchemaType.String)
                types[index++] = "string";
            if ((type & JsonSchemaType.Float) == JsonSchemaType.Float || (type & JsonSchemaType.Integer) == JsonSchemaType.Integer)
                types[index++] = "number";
            if ((type & JsonSchemaType.Boolean) == JsonSchemaType.Boolean)
                types[index++] = "boolean";
            if ((type & JsonSchemaType.Object) == JsonSchemaType.Object)
                types[index++] = "object";
            if ((type & JsonSchemaType.Array) == JsonSchemaType.Array)
                types[index++] = "array";
            if ((type & JsonSchemaType.Null) == JsonSchemaType.Null)
                types[index++] = "null";

            if (index == 0)
                return "";
            else if (index == 1)
                return types[0];
            else if (index == 2)
                return types[0] + " or " + types[1];
            else
                return String.Join(", ", types, 0, index - 1) + ", or " + types[index - 1];
        }
    internal static bool HasFlag(JsonSchemaType? value, JsonSchemaType flag)
    {
      // default value is Any
      if (value == null)
        return true;

      bool match = ((value & flag) == flag);
      if (match)
        return true;

      // integer is a subset of float
      if (value == JsonSchemaType.Float && flag == JsonSchemaType.Integer)
        return true;

      return false;
    }
Example #43
0
        internal static string MapType(JsonSchemaType type)
        {
            KeyValuePair <string, JsonSchemaType> keyValuePair = JsonSchemaConstants.JsonSchemaTypeMapping.Single <KeyValuePair <string, JsonSchemaType> >((KeyValuePair <string, JsonSchemaType> kv) => kv.Value == type);

            return(keyValuePair.Key);
        }
Example #44
0
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            ValidationUtils.ArgumentNotNull((object)type, nameof(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;
                        jsonSchema.Type = type1.HasValue ? new JsonSchemaType?(type1.GetValueOrDefault() | JsonSchemaType.Null) : new JsonSchemaType?();
                    }
                    if (required)
                    {
                        bool?required1 = schema.Required;
                        bool flag      = true;
                        if ((required1.GetValueOrDefault() == flag ? (!required1.HasValue ? 1 : 0) : 1) != 0)
                        {
                            schema.Required = new bool?(true);
                        }
                    }
                    return(schema);
                }
            }
            if (this._stack.Any <JsonSchemaGenerator.TypeSchema>((Func <JsonSchemaGenerator.TypeSchema, bool>)(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((IFormatProvider)CultureInfo.InvariantCulture, (object)type));
            }
            JsonContract  jsonContract  = this.ContractResolver.ResolveContract(type);
            JsonConverter jsonConverter = jsonContract.Converter ?? jsonContract.InternalConverter;

            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 cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonArrayAttribute>((object)type);
                    bool flag1 = cachedAttribute == null || cachedAttribute.AllowNullItems;
                    Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                    if (collectionItemType != null)
                    {
                        this.CurrentSchema.Items = (IList <JsonSchema>) new List <JsonSchema>();
                        this.CurrentSchema.Items.Add(this.GenerateInternal(collectionItemType, !flag1 ? Required.Always : Required.Default, false));
                        break;
                    }
                    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.HasValue ? 1 : 0) : 0) != 0 && type.IsEnum() && !type.IsDefined(typeof(FlagsAttribute), true))
                    {
                        this.CurrentSchema.Enum = (IList <JToken>) new List <JToken>();
                        using (IEnumerator <EnumValue <long> > enumerator = EnumUtils.GetNamesAndValues <long>(type).GetEnumerator())
                        {
                            while (enumerator.MoveNext())
                            {
                                this.CurrentSchema.Enum.Add(JToken.FromObject((object)enumerator.Current.Value));
                            }
                            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 && this.ContractResolver.ResolveContract(keyType).ContractType == JsonContractType.Primitive)
                    {
                        this.CurrentSchema.AdditionalProperties = this.GenerateInternal(valueType, Required.Default, false);
                        break;
                    }
                    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("Unexpected contract type: {0}".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)jsonContract));
                }
            }
            return(this.Pop().Schema);
        }
Example #45
0
        private static bool PropertyValueIsLeaf(Property property)
        {
            JsonSchemaType jsonTypes = property.ValueType.JsonTypes;

            return(!jsonTypes.HasFlag(JsonSchemaType.Object));
        }
Example #46
0
 internal static string MapType(JsonSchemaType type)
 {
     return(JsonSchemaConstants.JsonSchemaTypeMapping.Single(kv => kv.Value == type).Key);
 }
Example #47
0
 internal static bool HasFlag(JsonSchemaType? value, JsonSchemaType flag)
 {
   if (!value.HasValue)
     return true;
   JsonSchemaType? nullable1 = value;
   JsonSchemaType jsonSchemaType1 = flag;
   JsonSchemaType? nullable2 = nullable1.HasValue ? new JsonSchemaType?(nullable1.GetValueOrDefault() & jsonSchemaType1) : new JsonSchemaType?();
   JsonSchemaType jsonSchemaType2 = flag;
   if (nullable2.GetValueOrDefault() == jsonSchemaType2 && nullable2.HasValue)
     return true;
   JsonSchemaType? nullable3 = value;
   return (nullable3.GetValueOrDefault() != JsonSchemaType.Float ? 0 : (nullable3.HasValue ? 1 : 0)) != 0 && flag == JsonSchemaType.Integer;
 }