JsonProperty
A description of a JSON property of a JSON object.
using Newtonsoft.Json;
using System.ComponentModel;
namespace NJsonSchema
{
public class JsonProperty : JsonSchema4
{
private object _parent;
[JsonIgnore]
public string Name { get; set; }
[JsonIgnore]
public override object Parent {
get {
return _parent;
}
set {
bool num = _parent == null;
_parent = value;
if (num && InitialIsRequired)
IsRequired = InitialIsRequired;
}
}
[JsonIgnore]
public bool IsRequired {
get {
return base.ParentSchema.RequiredProperties.Contains(Name);
}
set {
if (base.ParentSchema == null)
InitialIsRequired = value;
else if (value) {
if (!base.ParentSchema.RequiredProperties.Contains(Name))
base.ParentSchema.RequiredProperties.Add(Name);
} else if (base.ParentSchema.RequiredProperties.Contains(Name)) {
base.ParentSchema.RequiredProperties.Remove(Name);
}
}
}
[JsonIgnore]
internal bool InitialIsRequired { get; set; }
[DefaultValue(false)]
[JsonProperty("x-readOnly", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool IsReadOnly { get; set; }
[DefaultValue(false)]
[JsonProperty("x-writeOnly", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool IsWriteOnly { get; set; }
[JsonIgnore]
public bool IsInheritanceDiscriminator {
get {
return base.ParentSchema.ActualDiscriminator == Name;
}
}
internal static JsonProperty FromJsonSchema(string name, JsonSchema4 type)
{
JsonProperty jsonProperty = JsonConvert.DeserializeObject<JsonProperty>(JsonConvert.SerializeObject(type));
jsonProperty.Name = name;
return jsonProperty;
}
public override bool IsNullable(SchemaType schemaType)
{
if (base.IsEnumeration && base.Enumeration.Contains(null))
return true;
if (schemaType == SchemaType.Swagger2)
return !IsRequired;
return base.IsNullable(schemaType);
}
}
}