private string GenJsonProperties(JsonProperty prop) { string re = "\"" + prop.Name + "\":"; if (prop.Type == JsonObjectType.String) { re += "\"" + prop.Type + "\""; } else if (prop.Type == JsonObjectType.Number || prop.Type == JsonObjectType.Integer) { re += "0"; } else if (prop.Type == JsonObjectType.Array) { re += "[" + GenJsonArrayProp(prop.Item) + "]"; } else if (prop.Type == JsonObjectType.Boolean) { re += "true"; } else if (prop.Type == JsonObjectType.None) { re += ToSampleJson(prop.ActualSchema); } else { re += "null"; } return re; }
public static async Task <JsonSchema4> GenerateSchemaAsync(Type type) { var schema = await JsonSchema4.FromTypeAsync(type, s_JsonSchemaGeneratorSettings).ConfigureAwait(false); var keyProperties = type.GetKeyFromUriProperties(); foreach (var keyProperty in keyProperties) { RemoveProperty(schema, keyProperty.Property.Name); } foreach (var propertyGroup in keyProperties.GroupBy(p => p.SchemaPropertyName)) { var schemaPropertyName = propertyGroup.Key; if (schema.Properties.ContainsKey(schemaPropertyName)) { throw new JsonSchemaGenerationException($"Key property '{propertyGroup.First().Property.Name}' maps to property '{schemaPropertyName}' that already exists on type {type.BeautifulName()}"); } var isRequired = propertyGroup.Any(p => p.Property.GetCustomAttribute <RequiredAttribute>() != null); var property = new NJsonSchema.JsonProperty { Type = JsonObjectType.String, Format = JsonFormatStrings.Uri }; //schema factory sets minlegth of required uri properties, so do it here as well if (isRequired) { property.MinLength = 1; } AddProperty(schema, schemaPropertyName, property, isRequired); } return(schema); }
/// <summary>Initializes a new instance of the <see cref="JsonPropertyModel"/> class. </summary> /// <param name="name">The name of the property. </param> /// <param name="parent">The parent object. </param> /// <param name="schema">The property type as schema object. </param> public JsonPropertyModel(string name, JsonObjectModel parent, JsonProperty schema) { Name = name; Parent = parent; Schema = schema; Parent.PropertyChanged += (sender, args) => RaisePropertyChanged(() => Value); }
static void AddProperty(JsonSchema4 schema, string propertyName, NJsonSchema.JsonProperty property, bool isRequired) { schema.Properties.Add(propertyName, property); if (isRequired) { schema.RequiredProperties.Add(propertyName); } }