/// <summary>Generates the type.</summary> /// <param name="typeNameHint">The type name hint.</param> /// <returns>The code.</returns> public override string GenerateType(string typeNameHint) { if (_schema.Type == JsonObjectType.String && _schema.Enumeration.Count > 0) { var template = LoadTemplate("Enum"); template.Add("name", !string.IsNullOrEmpty(_schema.TypeName) ? _schema.TypeName : typeNameHint); template.Add("enums", _schema.Enumeration); return(template.Render()); } else { var properties = _schema.Properties.Values.Select(property => new { Name = property.Name, PropertyName = ConvertToUpperStartIdentifier(property.Name), FieldName = ConvertToLowerStartIdentifier(property.Name), Required = property.IsRequired ? "Required.Always" : "Required.Default", Type = _resolver.Resolve(property, property.IsRequired, property.Name) }).ToList(); var template = LoadTemplate("Class"); template.Add("namespace", Namespace); template.Add("class", _schema.TypeName); template.Add("inheritance", _schema.AllOf.Count == 1 ? _resolver.Resolve(_schema.AllOf.First(), true, string.Empty) + ", " : string.Empty); template.Add("properties", properties); return(template.Render()); } }
/// <summary>Generates the type.</summary> /// <param name="typeNameHint">The type name hint.</param> /// <returns>The code.</returns> public override TypeGeneratorResult GenerateType(string typeNameHint) { var typeName = !string.IsNullOrEmpty(_schema.TypeName) ? _schema.TypeName : typeNameHint; if (_schema.IsEnumeration) { var template = LoadTemplate("Enum"); template.Add("name", typeName); template.Add("enums", GetEnumeration()); template.Add("hasDescription", !(_schema is JsonProperty) && !string.IsNullOrEmpty(_schema.Description)); template.Add("description", RemoveLineBreaks(_schema.Description)); return(new TypeGeneratorResult { TypeName = typeName, Code = template.Render() }); } else { var properties = _schema.Properties.Values.Select(property => new { Name = property.Name, HasDescription = !string.IsNullOrEmpty(property.Description), Description = RemoveLineBreaks(property.Description), PropertyName = ConvertToUpperStartIdentifier(property.Name), FieldName = ConvertToLowerStartIdentifier(property.Name), Required = property.IsRequired && Settings.RequiredPropertiesMustBeDefined ? "Required.Always" : "Required.Default", IsStringEnum = property.ActualSchema.IsEnumeration && property.ActualSchema.Type == JsonObjectType.String, Type = _resolver.Resolve(property, property.Type.HasFlag(JsonObjectType.Null), property.Name) }).ToList(); var template = LoadTemplate("Class"); template.Add("namespace", Settings.Namespace); template.Add("class", typeName); template.Add("hasDescription", !(_schema is JsonProperty) && !string.IsNullOrEmpty(_schema.Description)); template.Add("description", RemoveLineBreaks(_schema.Description)); template.Add("inpc", Settings.ClassStyle == CSharpClassStyle.Inpc); template.Add("inheritance", _schema.AllOf.Count == 1 ? ": " + _resolver.Resolve(_schema.AllOf.First(), false, string.Empty) + (Settings.ClassStyle == CSharpClassStyle.Inpc ? ", INotifyPropertyChanged" : "") : (Settings.ClassStyle == CSharpClassStyle.Inpc ? ": INotifyPropertyChanged" : "")); template.Add("properties", properties); return(new TypeGeneratorResult { TypeName = typeName, Code = template.Render() }); } }
/// <summary>Generates the file.</summary> /// <returns>The file contents.</returns> public override string GenerateFile() { _resolver.Resolve(_schema, false, string.Empty); // register root type var template = new FileTemplate() as ITemplate; template.Initialize(new FileTemplateModel { Toolchain = JsonSchema4.ToolchainVersion, Namespace = Settings.Namespace ?? string.Empty, Classes = ConversionUtilities.TrimWhiteSpaces(_resolver.GenerateTypes(null)) }); return(ConversionUtilities.TrimWhiteSpaces(template.Render())); }
/// <summary>Generates the file.</summary> /// <param name="rootTypeNameHint">The root type name hint.</param> /// <returns>The file contents.</returns> public override string GenerateFile(string rootTypeNameHint) { _resolver.Resolve(_schema, false, rootTypeNameHint); // register root type var model = new FileTemplateModel { Namespace = Settings.Namespace ?? string.Empty, Classes = ConversionUtilities.TrimWhiteSpaces(_resolver.GenerateClasses()) }; var template = Settings.TemplateFactory.CreateTemplate("CSharp", "File", model); return(ConversionUtilities.TrimWhiteSpaces(template.Render())); }