private Uri GetTypeId(Type type, bool explicitOnly) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>(type); Uri typeId; if (!string.IsNullOrEmpty(containerAttribute?.Id)) { typeId = new Uri(containerAttribute.Id, UriKind.RelativeOrAbsolute); } else { if (explicitOnly) { return(null); } switch (_generator.SchemaIdGenerationHandling) { case SchemaIdGenerationHandling.TypeName: typeId = new Uri(type.Name, UriKind.RelativeOrAbsolute); break; case SchemaIdGenerationHandling.FullTypeName: typeId = new Uri(type.FullName, UriKind.RelativeOrAbsolute); break; case SchemaIdGenerationHandling.AssemblyQualifiedName: typeId = new Uri(type.AssemblyQualifiedName, UriKind.RelativeOrAbsolute); break; default: return(null); } } // avoid id conflicts Uri resolvedTypeId = typeId; int i = 1; while (_typeSchemas.Any(s => s.Schema.Id == resolvedTypeId)) { resolvedTypeId = new Uri(typeId.OriginalString + "-" + i, UriKind.RelativeOrAbsolute); i++; } return(resolvedTypeId); }
private string GetDescription(Type type) { JsonContainerAttribute jsonContainerAttribute = JsonTypeReflector.GetJsonContainerAttribute(type); if (jsonContainerAttribute != null && !string.IsNullOrEmpty(jsonContainerAttribute.Description)) { return(jsonContainerAttribute.Description); } DescriptionAttribute attribute = ReflectionUtils.GetAttribute <DescriptionAttribute>(type); if (attribute != null) { return(attribute.get_Description()); } return(null); }
// Token: 0x0600118C RID: 4492 RVA: 0x000616A0 File Offset: 0x0005F8A0 private string GetDescription(Type type) { JsonContainerAttribute cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>(type); if (!StringUtils.IsNullOrEmpty((cachedAttribute != null) ? cachedAttribute.Description : null)) { return(cachedAttribute.Description); } DescriptionAttribute attribute = ReflectionUtils.GetAttribute <DescriptionAttribute>(type); if (attribute == null) { return(null); } return(attribute.Description); }
private string GetDescription(Type type) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>(type); if (!string.IsNullOrEmpty(containerAttribute?.Description)) { return(containerAttribute.Description); } #if !(NETFX_CORE || PORTABLE40 || PORTABLE) DescriptionAttribute descriptionAttribute = ReflectionUtils.GetAttribute <DescriptionAttribute>(type); return(descriptionAttribute?.Description); #else return(null); #endif }
private string GetDescription(Type type, JsonProperty memberProperty) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>(type); if (!string.IsNullOrEmpty(containerAttribute?.Description)) { return(containerAttribute.Description); } #if !(PORTABLE40 || PORTABLE) DescriptionAttribute descriptionAttribute = GetAttributeFromTypeOrProperty <DescriptionAttribute>(type, memberProperty); return(descriptionAttribute?.Description); #else return(null); #endif }
private string GetTitle(Type type, JsonProperty memberProperty) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>(type); if (!string.IsNullOrEmpty(containerAttribute?.Title)) { return(containerAttribute.Title); } #if !(PORTABLE40 || PORTABLE) DisplayNameAttribute displayNameAttribute = GetAttributeFromTypeOrProperty <DisplayNameAttribute>(type, memberProperty); return(displayNameAttribute?.DisplayName); #else return(null); #endif }
public void Deserialize() { string json = @"{ ""first_name"": ""FirstName!"", ""LastName"": ""LastName!"", ""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"", ""PersonRoles"": [ { ""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"", ""RoleId"": ""67ea92b7-4bd3-4718-bd75-3c7edf800b34"", ""PersonRoleId"": ""b012dd41-71df-4839-b8d5-d1333fb886bc"", ""Role"": { ""Name"": ""Role1"", ""RoleId"": ""t5LqZ9NLGEe9dTx+34ALNA=="" } } ], ""Department"": { ""DepartmentId"": ""08f68bf9-929b-4434-bc47-c9489d22112b"", ""Name"": ""!emaN"" } }"; Person person = JsonConvert.DeserializeObject <Person>(json); Assert.IsNotNull(person); Assert.AreEqual(new Guid("7AA027AA-C995-4986-908D-999D8063599F"), person.PersonId); Assert.AreEqual("FirstName!", person.FirstName); Assert.AreEqual("LastName!", person.LastName); Assert.AreEqual(1, person.PersonRoles.Count); Assert.AreEqual(person.PersonId, person.PersonRoles[0].PersonId); Assert.AreEqual(new Guid("67EA92B7-4BD3-4718-BD75-3C7EDF800B34"), person.PersonRoles[0].RoleId); Assert.IsNotNull(person.PersonRoles[0].Role); Assert.AreEqual(1, person.PersonRoles[0].Role.PersonRoles.Count); Assert.AreEqual("Name!", person.Department.Name); TableAttribute tableAttribute = JsonTypeReflector.GetAttribute <TableAttribute>(typeof(Person)); Assert.AreEqual("", tableAttribute.Name); ColumnAttribute columnAttribute = JsonTypeReflector.GetAttribute <ColumnAttribute>(typeof(Person).GetProperty("FirstName")); Assert.AreEqual("_FirstName", columnAttribute.Storage); }
private string GetDescription(Type type) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(type); if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Description)) { return(containerAttribute.Description); } #if !PocketPC DescriptionAttribute descriptionAttribute = ReflectionUtils.GetAttribute <DescriptionAttribute>(type); if (descriptionAttribute != null) { return(descriptionAttribute.Description); } #endif return(null); }
private string GetTitle(Type type) { string title; JsonContainerAttribute cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>(type); if (cachedAttribute != null) { title = cachedAttribute.Title; } else { title = null; } if (string.IsNullOrEmpty(title)) { return(null); } return(cachedAttribute.Title); }
private string GetDescription(Type type) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(type); if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Description)) { return(containerAttribute.Description); } DescriptionAttribute attribute = ReflectionUtils.GetAttribute <DescriptionAttribute>((ICustomAttributeProvider)type); if (attribute != null) { return(attribute.Description); } else { return((string)null); } }
private string GetDescription(Type type) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>(type); if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Description)) { return(containerAttribute.Description); } #if !(DOTNET || PORTABLE40 || PORTABLE) DescriptionAttribute descriptionAttribute = ReflectionUtils.GetAttribute <DescriptionAttribute>(type); if (descriptionAttribute != null) { return(descriptionAttribute.Description); } #endif return(null); }
private string GetTypeId(Type type, bool explicitOnly) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(type); if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Id)) return containerAttribute.Id; if (explicitOnly) return null; switch (UndefinedSchemaIdHandling) { case UndefinedSchemaIdHandling.UseTypeName: return type.FullName; case UndefinedSchemaIdHandling.UseAssemblyQualifiedName: return type.AssemblyQualifiedName; default: return null; } }
private TAttribute GetAttributeFromTypeOrProperty <TAttribute>(Type type, JsonProperty memberProperty) where TAttribute : Attribute { TAttribute attribute = null; // check for property attribute first if (memberProperty != null) { attribute = memberProperty.AttributeProvider .GetAttributes(true) .OfType <TAttribute>() .FirstOrDefault(); } // fall back to type attribute if (attribute == null) { attribute = JsonTypeReflector.GetCachedAttribute <TAttribute>(type); } return(attribute); }
private string GetTypeId(Type type, bool explicitOnly) { JsonContainerAttribute jsonContainerAttribute = JsonTypeReflector.GetJsonContainerAttribute(type); if (jsonContainerAttribute != null && !string.IsNullOrEmpty(jsonContainerAttribute.Id)) { return(jsonContainerAttribute.Id); } if (explicitOnly) { return(null); } Newtonsoft.Json.Schema.UndefinedSchemaIdHandling undefinedSchemaIdHandling = this.UndefinedSchemaIdHandling; if (undefinedSchemaIdHandling == Newtonsoft.Json.Schema.UndefinedSchemaIdHandling.UseTypeName) { return(type.FullName); } if (undefinedSchemaIdHandling != Newtonsoft.Json.Schema.UndefinedSchemaIdHandling.UseAssemblyQualifiedName) { return(null); } return(type.AssemblyQualifiedName); }
private string GetTypeId(Type type, bool explicitOnly) { JsonContainerAttribute jsonContainerAttribute = JsonTypeReflector.GetJsonContainerAttribute(type); if (jsonContainerAttribute != null && !string.IsNullOrEmpty(jsonContainerAttribute.Id)) { return(jsonContainerAttribute.Id); } if (!explicitOnly) { switch (UndefinedSchemaIdHandling) { case UndefinedSchemaIdHandling.UseTypeName: return(type.FullName); case UndefinedSchemaIdHandling.UseAssemblyQualifiedName: return(type.AssemblyQualifiedName); default: return(null); } } return(null); }
// Token: 0x0600118D RID: 4493 RVA: 0x000616F0 File Offset: 0x0005F8F0 private string GetTypeId(Type type, bool explicitOnly) { JsonContainerAttribute cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>(type); if (!StringUtils.IsNullOrEmpty((cachedAttribute != null) ? cachedAttribute.Id : null)) { return(cachedAttribute.Id); } if (explicitOnly) { return(null); } UndefinedSchemaIdHandling undefinedSchemaIdHandling = this.UndefinedSchemaIdHandling; if (undefinedSchemaIdHandling == UndefinedSchemaIdHandling.UseTypeName) { return(type.FullName); } if (undefinedSchemaIdHandling != UndefinedSchemaIdHandling.UseAssemblyQualifiedName) { return(null); } return(type.AssemblyQualifiedName); }
private string GetTypeId(Type type, bool explicitOnly) { JsonContainerAttribute cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonContainerAttribute>((object)type); if (!string.IsNullOrEmpty(cachedAttribute?.Id)) { return(cachedAttribute.Id); } if (explicitOnly) { return((string)null); } switch (this.UndefinedSchemaIdHandling) { case UndefinedSchemaIdHandling.UseTypeName: return(type.FullName); case UndefinedSchemaIdHandling.UseAssemblyQualifiedName: return(type.AssemblyQualifiedName); default: return((string)null); } }
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) { 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 = 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); }
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); }
// 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); }
protected override List <MemberInfo> GetSerializableMembers(Type objectType) { bool ignoreSerializableAttribute = this.IgnoreSerializableAttribute; MemberSerialization objectMemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute); List <MemberInfo> allMembers = ( from m in ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where !ReflectionUtils.IsIndexedProperty(m) select m).ToList(); List <MemberInfo> serializableMembers = new List <MemberInfo>(); if (objectMemberSerialization != MemberSerialization.Fields) { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); List <MemberInfo> defaultMembers = ( from m in ReflectionUtils.GetFieldsAndProperties(objectType, this.DefaultMembersSearchFlags) where !ReflectionUtils.IsIndexedProperty(m) select m).ToList(); foreach (MemberInfo memberInfo in allMembers) { //DataTransObjectBase if (memberInfo.DeclaringType == typeof(DataTransObjectBase)) { continue; } //contextdto if (memberInfo.DeclaringType != null && memberInfo.DeclaringType.IsSubclassOf(typeof(ProxyBase))) { var memberName = memberInfo.Name.ToLower(CultureInfo.CurrentCulture); if (memberName == "contextdto" || memberName == "targetorgcode" || memberName == "targetorgname" || memberName.EndsWith("_skey")) { continue; } } //BusinessEntity.EntityKey if (memberInfo.DeclaringType != null && (memberInfo.DeclaringType == typeof(ObjectKeyX) || memberInfo.DeclaringType.IsSubclassOf(typeof(ObjectKeyX)))) { var memberName = memberInfo.Name.ToLower(CultureInfo.CurrentCulture); if (memberName == "entitytype" || memberName == "serializablekeys") { continue; } } var isDTOType = false; switch (memberInfo.MemberType()) { case MemberTypes.Field: var fieldInfo = (FieldInfo)memberInfo; if (fieldInfo.DeclaringType != null && fieldInfo.FieldType.IsSubclassOf(typeof(DataTransObjectBase))) { isDTOType = true; } break; case MemberTypes.Property: var propertyInfo = (PropertyInfo)memberInfo; if (propertyInfo.DeclaringType != null && propertyInfo.DeclaringType.IsSubclassOf(typeof(DataTransObjectBase))) { isDTOType = true; } break; } if (isDTOType) { var memberName = memberInfo.Name.ToLower(CultureInfo.CurrentCulture); if (memberName == "combinename" || memberName.StartsWith("multi_") || memberName.EndsWith("_skey")) { continue; } } if (!ReflectionUtils.CanSetMemberValue(memberInfo, true, true)) { continue; } if (this.SerializeCompilerGeneratedMembers || !memberInfo.IsDefined(typeof(CompilerGeneratedAttribute), true)) { if (defaultMembers.Contains(memberInfo)) { serializableMembers.Add(memberInfo); } else { if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(memberInfo) != null) { serializableMembers.Add(memberInfo); } else { if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(memberInfo) != null) { serializableMembers.Add(memberInfo); } else { if (objectMemberSerialization == MemberSerialization.Fields && memberInfo.MemberType() == MemberTypes.Field) { serializableMembers.Add(memberInfo); } } } } } } Type type; if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out type)) { serializableMembers = serializableMembers.Where(this.ShouldSerializeEntityMember).ToList(); } } else { foreach (MemberInfo member in allMembers) { var fieldInfo = member as FieldInfo; if (fieldInfo != null && !fieldInfo.IsStatic) { var memberName = fieldInfo.Name.ToLower(CultureInfo.CurrentCulture); if (memberName == "sysstate" || memberName.StartsWith("multi_") || memberName.EndsWith("_skey")) { continue; } serializableMembers.Add(member); } } } return(serializableMembers); }
private JsonSchema GenerateInternal(Type type, Required valueRequired) { 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) { 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)); } Push(new TypeSchema(type, new JsonSchema())); if (explicitId != null) { CurrentSchema.Id = explicitId; } CurrentSchema.Title = GetTitle(type); CurrentSchema.Description = GetDescription(type); if (CollectionUtils.IsDictionaryType(type)) { // TODO: include null CurrentSchema.Type = JsonSchemaType.Object; 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); } } } else if (CollectionUtils.IsCollectionType(type)) { // TODO: include null CurrentSchema.Type = JsonSchemaType.Array; CurrentSchema.Id = GetTypeId(type, false); JsonArrayAttribute arrayAttribute = JsonTypeReflector.GetJsonContainerAttribute(type) as JsonArrayAttribute; bool allowNullItem = (arrayAttribute != null) ? arrayAttribute.AllowNullItems : false; Type collectionItemType = ReflectionUtils.GetCollectionItemType(type); if (collectionItemType != null) { CurrentSchema.Items = new List <JsonSchema>(); CurrentSchema.Items.Add(GenerateInternal(collectionItemType, (!allowNullItem) ? Required.Always : Required.Default)); } } else { CurrentSchema.Type = GetJsonSchemaType(type, valueRequired); if (HasFlag(CurrentSchema.Type, JsonSchemaType.Object)) { CurrentSchema.Id = GetTypeId(type, false); JsonObjectContract contract = ContractResolver.ResolveContract(type) as JsonObjectContract; if (contract == null) { throw new Exception("Could not resolve contract for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type)); } CurrentSchema.Properties = new Dictionary <string, JsonSchema>(); foreach (JsonProperty property in contract.Properties) { if (!property.Ignored) { JsonSchema propertySchema = GenerateInternal(property.PropertyType, property.Required); if (property.DefaultValue != null) { propertySchema.Default = JToken.FromObject(property.DefaultValue); } CurrentSchema.Properties.Add(property.PropertyName, propertySchema); } } if (type.IsSealed) { CurrentSchema.AllowAdditionalProperties = false; } } else 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); } } } return(Pop().Schema); }
private JSchema PopulateSchema(JSchema schema, JsonContract contract, JsonProperty memberProperty, Required valueRequired) { schema.Title = GetTitle(contract.NonNullableUnderlyingType); schema.Description = GetDescription(contract.NonNullableUnderlyingType); JsonConverter converter = contract.Converter ?? contract.InternalConverter; if (converter != null) { // todo: Add GetSchema to JsonConverter and use here? schema.Type = JSchemaType.Any; } else { switch (contract.ContractType) { case JsonContractType.Object: if (schema.Id == null) { schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false); } schema.Type = AddNullType(JSchemaType.Object, valueRequired); GenerateObjectSchema(schema, contract.NonNullableUnderlyingType, (JsonObjectContract)contract); break; case JsonContractType.Array: if (schema.Id == null) { schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false); } schema.Type = AddNullType(JSchemaType.Array, valueRequired); schema.MinimumItems = DataAnnotationHelpers.GetMinLength(memberProperty); schema.MaximumItems = DataAnnotationHelpers.GetMaxLength(memberProperty); JsonArrayAttribute arrayAttribute = JsonTypeReflector.GetCachedAttribute <JsonArrayAttribute>(contract.NonNullableUnderlyingType); bool allowNullItem = (arrayAttribute == null || arrayAttribute.AllowNullItems); Type collectionItemType = ReflectionUtils.GetCollectionItemType(contract.NonNullableUnderlyingType); if (collectionItemType != null) { schema.Items.Add(GenerateInternal(collectionItemType, (!allowNullItem) ? Required.Always : Required.Default, null, (JsonArrayContract)contract)); } break; case JsonContractType.Primitive: schema.Type = GetJSchemaType(contract.UnderlyingType, valueRequired); if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.String)) { int minimumLength; int maximumLength; if (DataAnnotationHelpers.GetStringLength(memberProperty, out minimumLength, out maximumLength)) { schema.MinimumLength = minimumLength; schema.MaximumLength = maximumLength; } else { schema.MinimumLength = DataAnnotationHelpers.GetMinLength(memberProperty); schema.MaximumLength = DataAnnotationHelpers.GetMaxLength(memberProperty); } schema.Pattern = DataAnnotationHelpers.GetPattern(memberProperty); schema.Format = DataAnnotationHelpers.GetFormat(memberProperty); } if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Float) || JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Integer)) { double minimum; double maximum; if (DataAnnotationHelpers.GetRange(memberProperty, out minimum, out maximum)) { schema.Minimum = minimum; schema.Maximum = maximum; } } if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Integer) && contract.NonNullableUnderlyingType.IsEnum() && ReflectionUtils.GetAttribute <FlagsAttribute>(contract.NonNullableUnderlyingType) == null) { IList <EnumValue <long> > enumValues = EnumUtils.GetNamesAndValues <long>(contract.NonNullableUnderlyingType); foreach (EnumValue <long> enumValue in enumValues) { JToken value = JToken.FromObject(enumValue.Value); schema.Enum.Add(value); } } Type enumDataType = DataAnnotationHelpers.GetEnumDataType(memberProperty); if (enumDataType != null && CollectionUtils.IsNullOrEmpty(schema._enum)) { IList <EnumValue <long> > enumValues = EnumUtils.GetNamesAndValues <long>(enumDataType); foreach (EnumValue <long> enumValue in enumValues) { JToken value = (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.String)) ? enumValue.Name : JToken.FromObject(enumValue.Value); schema.Enum.Add(value); } } break; case JsonContractType.String: JSchemaType schemaType = (!ReflectionUtils.IsNullable(contract.UnderlyingType)) ? JSchemaType.String : AddNullType(JSchemaType.String, valueRequired); schema.Type = schemaType; schema.MinimumLength = DataAnnotationHelpers.GetMinLength(memberProperty); schema.MaximumLength = DataAnnotationHelpers.GetMaxLength(memberProperty); break; case JsonContractType.Dictionary: schema.Type = AddNullType(JSchemaType.Object, valueRequired); schema.MinimumProperties = DataAnnotationHelpers.GetMinLength(memberProperty); schema.MaximumProperties = DataAnnotationHelpers.GetMaxLength(memberProperty); Type keyType; Type valueType; ReflectionUtils.GetDictionaryKeyValueTypes(contract.NonNullableUnderlyingType, out keyType, out valueType); if (keyType != null) { JsonContract keyContract = ContractResolver.ResolveContract(keyType); // can be converted to a string if (keyContract.ContractType == JsonContractType.Primitive) { schema.AdditionalProperties = GenerateInternal(valueType, Required.Default, null, (JsonDictionaryContract)contract); } } break; case JsonContractType.Serializable: if (schema.Id == null) { schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false); } schema.Type = AddNullType(JSchemaType.Object, valueRequired); schema.AllowAdditionalProperties = true; break; case JsonContractType.Dynamic: case JsonContractType.Linq: schema.Type = JSchemaType.Any; break; default: throw new JsonException("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, contract)); } } return(schema); }
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); }
private void PopulateSchema(JSchema schema, JsonContract contract, JsonProperty memberProperty, Required valueRequired) { schema.Title = GetTitle(contract.NonNullableUnderlyingType); schema.Description = GetDescription(contract.NonNullableUnderlyingType); JsonConverter converter; if (contract.Converter != null && contract.Converter.CanWrite) { converter = contract.Converter; } else if (contract.InternalConverter != null && contract.InternalConverter.CanWrite) { converter = contract.InternalConverter; } else { converter = null; } if (converter != null) { schema.Type = null; } else { switch (contract.ContractType) { case JsonContractType.Object: if (contract.NonNullableUnderlyingType == typeof(object)) { PopulatePrimativeSchema(schema, contract, memberProperty, valueRequired); } else { if (schema.Id == null) { schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false); } schema.Type = AddNullType(JSchemaType.Object, valueRequired); GenerateObjectSchema(schema, contract.NonNullableUnderlyingType, (JsonObjectContract)contract); } break; case JsonContractType.Array: if (schema.Id == null) { schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false); } schema.Type = AddNullType(JSchemaType.Array, valueRequired); schema.MinimumItems = DataAnnotationHelpers.GetMinLength(memberProperty); schema.MaximumItems = DataAnnotationHelpers.GetMaxLength(memberProperty); JsonArrayAttribute arrayAttribute = JsonTypeReflector.GetCachedAttribute <JsonArrayAttribute>(contract.NonNullableUnderlyingType); Required?required = null; if (arrayAttribute != null && !arrayAttribute.AllowNullItems) { required = Required.Always; } Type collectionItemType = ReflectionUtils.GetCollectionItemType(contract.NonNullableUnderlyingType); if (collectionItemType != null) { schema.Items.Add(GenerateInternal(collectionItemType, required, null, (JsonArrayContract)contract, null)); } break; case JsonContractType.Primitive: PopulatePrimativeSchema(schema, contract, memberProperty, valueRequired); break; case JsonContractType.String: JSchemaType schemaType = (!ReflectionUtils.IsNullable(contract.UnderlyingType)) ? JSchemaType.String : AddNullType(JSchemaType.String, valueRequired); schema.Type = schemaType; schema.MinimumLength = DataAnnotationHelpers.GetMinLength(memberProperty); schema.MaximumLength = DataAnnotationHelpers.GetMaxLength(memberProperty); break; case JsonContractType.Dictionary: schema.Type = AddNullType(JSchemaType.Object, valueRequired); schema.MinimumProperties = DataAnnotationHelpers.GetMinLength(memberProperty); schema.MaximumProperties = DataAnnotationHelpers.GetMaxLength(memberProperty); Type keyType; Type valueType; ReflectionUtils.GetDictionaryKeyValueTypes(contract.NonNullableUnderlyingType, out keyType, out valueType); if (keyType != null) { JsonContract keyContract = _generator.ContractResolver.ResolveContract(keyType); // can be converted to a string if (keyContract.ContractType == JsonContractType.Primitive) { schema.AdditionalProperties = GenerateInternal(valueType, _generator.DefaultRequired, null, (JsonDictionaryContract)contract, null); } } break; case JsonContractType.Serializable: if (schema.Id == null) { schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false); } schema.Type = AddNullType(JSchemaType.Object, valueRequired); schema.AllowAdditionalProperties = true; break; case JsonContractType.Dynamic: case JsonContractType.Linq: schema.Type = null; break; default: throw new JSchemaException("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, contract)); } } }
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); }
internal static TypeConverter GetConverter(Type t) { return(JsonTypeReflector.GetTypeConverter(t)); }
internal static TypeConverter GetConverter(Type t) => JsonTypeReflector.GetTypeConverter(t);