public void WriteClassMembers(IJsonClassGeneratorConfig config, StringBuilder sw, JsonType type, string indentMembers) { bool first = true; foreach (FieldInfo field in type.Fields) { string classPropertyName = this.GetCSharpPascalCaseName(field.MemberName); string propertyAttribute = config.GetCSharpJsonAttributeCode(field); if (!first && (propertyAttribute.Length > 0 || config.ExamplesInDocumentation)) { // If rendering examples/XML comments - or property attributes - then add a newline before the property for readability's sake (except if it's the first property in the class) sw.AppendLine(); } if (config.ExamplesInDocumentation) { sw.AppendFormat(indentMembers + "/// <summary>"); sw.AppendFormat(indentMembers + "/// Examples: " + field.GetExamplesText()); sw.AppendFormat(indentMembers + "/// </summary>"); sw.AppendLine(); } if (propertyAttribute.Length > 0) { sw.Append(indentMembers); sw.AppendLine(propertyAttribute); } if (config.UseFields) { sw.AppendFormat(indentMembers + "public {0}{1} {2};{3}", config.ImmutableClasses ? "readonly " : "", field.Type.GetTypeName(), classPropertyName, Environment.NewLine); } else if (config.ImmutableClasses) { if (field.Type.Type == JsonTypeEnum.Array) { sw.AppendFormat(indentMembers + "public IReadOnlyList<{0}> {1} {{ get; }}{2}", this.GetTypeName(field.Type.InternalType, config), classPropertyName, Environment.NewLine); } else { sw.AppendFormat(indentMembers + "public {0} {1} {{ get; }}{2}", field.Type.GetTypeName(), classPropertyName, Environment.NewLine); } } else { var getterSetterPart = "{ get; set; }"; if (config.NoSettersForCollections && (field.Type.IsCollectionType() && (config.ArrayAsList() && field.Type.Type == JsonTypeEnum.Array))) { getterSetterPart = "{ get; } = new " + field.Type.GetTypeName() + "();"; } sw.AppendFormat(indentMembers + "public {0} {1} {2}{3}", field.Type.GetTypeName(), classPropertyName, getterSetterPart, Environment.NewLine); } first = false; } }
public string GetTypeName(JsonType type, IJsonClassGeneratorConfig config) { var arraysAsLists = config.ArrayAsList(); switch (type.Type) { case JsonTypeEnum.Anything: return("object"); case JsonTypeEnum.Array: return(arraysAsLists ? "List<" + this.GetTypeName(type.InternalType, config) + ">" : this.GetTypeName(type.InternalType, config) + "[]"); case JsonTypeEnum.Dictionary: return("Dictionary<string, " + this.GetTypeName(type.InternalType, config) + ">"); case JsonTypeEnum.Boolean: return("bool"); case JsonTypeEnum.Float: return("double"); case JsonTypeEnum.Integer: return("int"); case JsonTypeEnum.Long: return("long"); case JsonTypeEnum.Date: return("DateTime"); case JsonTypeEnum.NonConstrained: return("object"); case JsonTypeEnum.NullableBoolean: return("bool?"); case JsonTypeEnum.NullableFloat: return("double?"); case JsonTypeEnum.NullableInteger: return("int?"); case JsonTypeEnum.NullableLong: return("long?"); case JsonTypeEnum.NullableDate: return("DateTime?"); case JsonTypeEnum.NullableSomething: return("object"); case JsonTypeEnum.Object: return(type.NewAssignedName); case JsonTypeEnum.String: return("string"); default: throw new NotSupportedException("Unsupported json type: " + type.Type); } }