private void WriteClassMembers(IJsonClassConfig config, TextWriter sw, JsonType type, string prefix) { foreach (var field in type.Fields) { if (config.UsePascalCase || config.ExamplesInDocumentation) { sw.WriteLine(); } if (config.ExamplesInDocumentation) { sw.WriteLine(prefix + "/// <summary>"); sw.WriteLine(prefix + "/// Examples: " + field.GetExamplesText()); sw.WriteLine(prefix + "/// </summary>"); } if (config.UsePascalCase) { sw.WriteLine(prefix + "[JsonProperty(\"{0}\")]", field.JsonMemberName); } if (config.UseProperties) { sw.WriteLine(prefix + "public {0} {1} {{ get; set; }}", field.Type.GetTypeName(), field.MemberName); } else { sw.WriteLine(prefix + "public {0} {1};", field.Type.GetTypeName(), field.MemberName); } } }
public void WriteFileEnd(IJsonClassConfig config, TextWriter sw) { if (config.UseNestedClasses) { sw.WriteLine(" }"); } }
/// <summary> /// 类型推断处理 /// </summary> public string GetTypeName(JsonType type, IJsonClassConfig config) { var arraysAsLists = !config.ExplicitDeserialization; switch (type.Type) { case JsonTypeEnum.Anything: return "object"; case JsonTypeEnum.Array: return arraysAsLists ? "IList<" + GetTypeName(type.InternalType, config) + ">" : GetTypeName(type.InternalType, config) + "[]"; case JsonTypeEnum.Dictionary: return "Dictionary<string, " + 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.AssignedName; case JsonTypeEnum.String: return "string"; default: throw new System.NotSupportedException("Unsupported json type"); } }
public void WriteNamespaceStart(IJsonClassConfig config, TextWriter sw, bool root) { sw.WriteLine(); sw.WriteLine("namespace {0}", root && !config.UseNestedClasses ? config.Namespace : (config.SecondaryNamespace ?? config.Namespace)); sw.WriteLine("{"); sw.WriteLine(); }
internal JsonType MaybeMakeNullable(IJsonClassConfig generator) { if (!generator.AlwaysUseNullableValues) { return(this); } return(this.GetCommonType(JsonType.GetNull(generator))); }
public void WriteClass(IJsonClassConfig config, TextWriter sw, JsonType type) { var visibility = config.InternalVisibility ? "internal" : "public"; if (config.UseNestedClasses) { if (!type.IsRoot) { if (ShouldApplyNoRenamingAttribute(config)) sw.WriteLine(" " + NoRenameAttribute); if (ShouldApplyNoPruneAttribute(config)) sw.WriteLine(" " + NoPruneAttribute); sw.WriteLine(" {0} class {1}", visibility, type.AssignedName); sw.WriteLine(" {"); } } else { if (ShouldApplyNoRenamingAttribute(config)) sw.WriteLine(" " + NoRenameAttribute); if (ShouldApplyNoPruneAttribute(config)) sw.WriteLine(" " + NoPruneAttribute); sw.WriteLine(" {0} class {1}", visibility, type.AssignedName); sw.WriteLine(" {"); } var prefix = config.UseNestedClasses && !type.IsRoot ? " " : " "; var shouldSuppressWarning = config.InternalVisibility && !config.UseProperties && !config.ExplicitDeserialization; if (shouldSuppressWarning) { sw.WriteLine("#pragma warning disable 0649"); if (!config.UsePascalCase) sw.WriteLine(); } if (type.IsRoot && config.ExplicitDeserialization) WriteStringConstructorExplicitDeserialization(config, sw, type, prefix); if (config.ExplicitDeserialization) { if (config.UseProperties) WriteClassWithPropertiesExplicitDeserialization(sw, type, prefix); else WriteClassWithFieldsExplicitDeserialization(sw, type, prefix); } else { WriteClassMembers(config, sw, type, prefix); } if (shouldSuppressWarning) { sw.WriteLine(); sw.WriteLine("#pragma warning restore 0649"); sw.WriteLine(); } if (config.UseNestedClasses && !type.IsRoot) sw.WriteLine(" }"); if (!config.UseNestedClasses) sw.WriteLine(" }"); sw.WriteLine(); }
private void WriteStringConstructorExplicitDeserialization(IJsonClassConfig config, TextWriter sw, JsonType type, string prefix) { sw.WriteLine(); sw.WriteLine(prefix + "public {1}(string json)", config.InternalVisibility ? "internal" : "public", type.AssignedName); sw.WriteLine(prefix + " : this(JObject.Parse(json))"); sw.WriteLine(prefix + "{"); sw.WriteLine(prefix + "}"); sw.WriteLine(); }
public FieldInfo(IJsonClassConfig generator, string jsonMemberName, JsonType type, bool usePascalCase, IList<object> Examples) { this.generator = generator; this.JsonMemberName = jsonMemberName; this.MemberName = jsonMemberName; if (usePascalCase) MemberName = JsonClassGenerator.ToTitleCase(MemberName); this.Type = type; this.Examples = Examples; }
public JsonType(IJsonClassConfig generator, JToken token) : this(generator) { Type = GetFirstTypeEnum(token); if (Type == JsonTypeEnum.Array) { var array = (JArray)token; InternalType = GetCommonType(generator, array.ToArray()); } }
public FieldInfo(IJsonClassConfig generator, string jsonMemberName, JsonType type, bool usePascalCase, IList <object> Examples) { this.generator = generator; this.JsonMemberName = jsonMemberName; this.MemberName = jsonMemberName; if (usePascalCase) { MemberName = JsonClassGenerator.ToTitleCase(MemberName); } this.Type = type; this.Examples = Examples; }
public static JsonType GetCommonType(IJsonClassConfig generator, JToken[] tokens) { if (tokens.Length == 0) { return(new JsonType(generator, JsonTypeEnum.NonConstrained)); } var common = new JsonType(generator, tokens[0]).MaybeMakeNullable(generator); for (int i = 1; i < tokens.Length; i++) { var current = new JsonType(generator, tokens[i]); common = common.GetCommonType(current); } return(common); }
/// <summary> /// 类型推断处理 /// </summary> public string GetTypeName(JsonType type, IJsonClassConfig config) { var arraysAsLists = !config.ExplicitDeserialization; switch (type.Type) { case JsonTypeEnum.Anything: return("object"); case JsonTypeEnum.Array: return(arraysAsLists ? "IList<" + GetTypeName(type.InternalType, config) + ">" : GetTypeName(type.InternalType, config) + "[]"); case JsonTypeEnum.Dictionary: return("Dictionary<string, " + 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.AssignedName); case JsonTypeEnum.String: return("string"); default: throw new System.NotSupportedException("Unsupported json type"); } }
public void WriteFileStart(IJsonClassConfig config, TextWriter sw) { if (config.UseNamespaces) { foreach (var line in JsonClassGenerator.FileHeader) { sw.WriteLine("/// " + line); } sw.WriteLine(); sw.WriteLine("using System;"); sw.WriteLine("using System.Collections.Generic;"); if (ShouldApplyNoPruneAttribute(config) || ShouldApplyNoRenamingAttribute(config)) { sw.WriteLine("using System.Reflection;"); } if (!config.ExplicitDeserialization && config.UsePascalCase) { sw.WriteLine("using Newtonsoft.Json;"); } sw.WriteLine("using Newtonsoft.Json.Linq;"); if (config.ExplicitDeserialization) { sw.WriteLine("using JsonCSharpClassGenerator;"); } if (config.SecondaryNamespace != null && config.HasSecondaryClasses && !config.UseNestedClasses) { sw.WriteLine("using {0};", config.SecondaryNamespace); } } if (config.UseNestedClasses) { sw.WriteLine(" {0} class {1}", config.InternalVisibility ? "internal" : "public", config.MainClass); sw.WriteLine(" {"); } }
public void WriteFileStart(IJsonClassConfig config, TextWriter sw) { if (config.UseNamespaces) { foreach (var line in JsonClassGenerator.FileHeader) { sw.WriteLine("/// " + line); } sw.WriteLine(); sw.WriteLine("using System;"); sw.WriteLine("using System.Collections.Generic;"); if (ShouldApplyNoPruneAttribute(config) || ShouldApplyNoRenamingAttribute(config)) sw.WriteLine("using System.Reflection;"); if (!config.ExplicitDeserialization && config.UsePascalCase) sw.WriteLine("using Newtonsoft.Json;"); sw.WriteLine("using Newtonsoft.Json.Linq;"); if (config.ExplicitDeserialization) sw.WriteLine("using JsonCSharpClassGenerator;"); if (config.SecondaryNamespace != null && config.HasSecondaryClasses && !config.UseNestedClasses) { sw.WriteLine("using {0};", config.SecondaryNamespace); } } if (config.UseNestedClasses) { sw.WriteLine(" {0} class {1}", config.InternalVisibility ? "internal" : "public", config.MainClass); sw.WriteLine(" {"); } }
internal JsonType(IJsonClassConfig generator, JsonTypeEnum type) : this(generator) { this.Type = type; }
public void WriteClass(IJsonClassConfig config, TextWriter sw, JsonType type) { var visibility = config.InternalVisibility ? "internal" : "public"; if (config.UseNestedClasses) { if (!type.IsRoot) { if (ShouldApplyNoRenamingAttribute(config)) { sw.WriteLine(" " + NoRenameAttribute); } if (ShouldApplyNoPruneAttribute(config)) { sw.WriteLine(" " + NoPruneAttribute); } sw.WriteLine(" {0} class {1}", visibility, type.AssignedName); sw.WriteLine(" {"); } } else { if (ShouldApplyNoRenamingAttribute(config)) { sw.WriteLine(" " + NoRenameAttribute); } if (ShouldApplyNoPruneAttribute(config)) { sw.WriteLine(" " + NoPruneAttribute); } sw.WriteLine(" {0} class {1}", visibility, type.AssignedName); sw.WriteLine(" {"); } var prefix = config.UseNestedClasses && !type.IsRoot ? " " : " "; var shouldSuppressWarning = config.InternalVisibility && !config.UseProperties && !config.ExplicitDeserialization; if (shouldSuppressWarning) { sw.WriteLine("#pragma warning disable 0649"); if (!config.UsePascalCase) { sw.WriteLine(); } } if (type.IsRoot && config.ExplicitDeserialization) { WriteStringConstructorExplicitDeserialization(config, sw, type, prefix); } if (config.ExplicitDeserialization) { if (config.UseProperties) { WriteClassWithPropertiesExplicitDeserialization(sw, type, prefix); } else { WriteClassWithFieldsExplicitDeserialization(sw, type, prefix); } } else { WriteClassMembers(config, sw, type, prefix); } if (shouldSuppressWarning) { sw.WriteLine(); sw.WriteLine("#pragma warning restore 0649"); sw.WriteLine(); } if (config.UseNestedClasses && !type.IsRoot) { sw.WriteLine(" }"); } if (!config.UseNestedClasses) { sw.WriteLine(" }"); } sw.WriteLine(); }
internal static JsonType GetNull(IJsonClassConfig generator) { return(new JsonType(generator, JsonTypeEnum.NullableSomething)); }
private void WriteClassMembers(IJsonClassConfig config, TextWriter sw, JsonType type, string prefix) { foreach (var field in type.Fields) { if (config.UsePascalCase || config.ExamplesInDocumentation) sw.WriteLine(); if (config.ExamplesInDocumentation) { sw.WriteLine(prefix + "/// <summary>"); sw.WriteLine(prefix + "/// Examples: " + field.GetExamplesText()); sw.WriteLine(prefix + "/// </summary>"); } if (config.UsePascalCase) { sw.WriteLine(prefix + "[JsonProperty(\"{0}\")]", field.JsonMemberName); } if (config.UseProperties) { sw.WriteLine(prefix + "public {0} {1} {{ get; set; }}", field.Type.GetTypeName(), field.MemberName); } else { sw.WriteLine(prefix + "public {0} {1};", field.Type.GetTypeName(), field.MemberName); } } }
private bool ShouldApplyNoPruneAttribute(IJsonClassConfig config) { return config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && config.UseProperties; }
internal static JsonType GetNull(IJsonClassConfig generator) { return new JsonType(generator, JsonTypeEnum.NullableSomething); }
private JsonType(IJsonClassConfig generator) { this.generator = generator; }
private bool ShouldApplyNoPruneAttribute(IJsonClassConfig config) { return(config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && config.UseProperties); }
/// <summary> /// 还原驼峰结构 /// </summary> private bool ShouldApplyNoRenamingAttribute(IJsonClassConfig config) { return(config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && !config.UsePascalCase); }
public void WriteNamespaceEnd(IJsonClassConfig config, TextWriter sw, bool root) { sw.WriteLine("}"); }
internal JsonType MaybeMakeNullable(IJsonClassConfig generator) { if (!generator.AlwaysUseNullableValues) return this; return this.GetCommonType(JsonType.GetNull(generator)); }
/// <summary> /// 还原驼峰结构 /// </summary> private bool ShouldApplyNoRenamingAttribute(IJsonClassConfig config) { return config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && !config.UsePascalCase; }
public static JsonType GetCommonType(IJsonClassConfig generator, JToken[] tokens) { if (tokens.Length == 0) return new JsonType(generator, JsonTypeEnum.NonConstrained); var common = new JsonType(generator, tokens[0]).MaybeMakeNullable(generator); for (int i = 1; i < tokens.Length; i++) { var current = new JsonType(generator, tokens[i]); common = common.GetCommonType(current); } return common; }