Пример #1
0
        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);
                }
            }
        }
Пример #2
0
 public void WriteFileEnd(IJsonClassConfig config, TextWriter sw)
 {
     if (config.UseNestedClasses)
     {
         sw.WriteLine("    }");
     }
 }
Пример #3
0
        /// <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");
            }
        }
Пример #4
0
 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();
 }
Пример #5
0
 internal JsonType MaybeMakeNullable(IJsonClassConfig generator)
 {
     if (!generator.AlwaysUseNullableValues)
     {
         return(this);
     }
     return(this.GetCommonType(JsonType.GetNull(generator)));
 }
Пример #6
0
        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();
        }
Пример #7
0
 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();
 }
Пример #8
0
 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;
 }
Пример #9
0
        public JsonType(IJsonClassConfig generator, JToken token)
            : this(generator)
        {
            Type = GetFirstTypeEnum(token);

            if (Type == JsonTypeEnum.Array)
            {
                var array = (JArray)token;
                InternalType = GetCommonType(generator, array.ToArray());
            }
        }
Пример #10
0
        public JsonType(IJsonClassConfig generator, JToken token)
            : this(generator)
        {
            Type = GetFirstTypeEnum(token);

            if (Type == JsonTypeEnum.Array)
            {
                var array = (JArray)token;
                InternalType = GetCommonType(generator, array.ToArray());
            }
        }
Пример #11
0
 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;
 }
Пример #12
0
        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);
        }
Пример #13
0
        /// <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");
            }
        }
Пример #14
0
        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("    {");
            }
        }
Пример #15
0
        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("    {");
            }
        }
Пример #16
0
 public void WriteFileEnd(IJsonClassConfig config, TextWriter sw)
 {
     if (config.UseNestedClasses)
     {
         sw.WriteLine("    }");
     }
 }
Пример #17
0
 internal JsonType(IJsonClassConfig generator, JsonTypeEnum type)
     : this(generator)
 {
     this.Type = type;
 }
Пример #18
0
        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();
        }
Пример #19
0
 internal static JsonType GetNull(IJsonClassConfig generator)
 {
     return(new JsonType(generator, JsonTypeEnum.NullableSomething));
 }
Пример #20
0
        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);
                }
            }
        }
Пример #21
0
 private bool ShouldApplyNoPruneAttribute(IJsonClassConfig config)
 {
     return config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && config.UseProperties;
 }
Пример #22
0
 internal static JsonType GetNull(IJsonClassConfig generator)
 {
     return new JsonType(generator, JsonTypeEnum.NullableSomething);
 }
Пример #23
0
 private JsonType(IJsonClassConfig generator)
 {
     this.generator = generator;
 }
Пример #24
0
 private bool ShouldApplyNoPruneAttribute(IJsonClassConfig config)
 {
     return(config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && config.UseProperties);
 }
Пример #25
0
 /// <summary>
 /// 还原驼峰结构
 /// </summary>
 private bool ShouldApplyNoRenamingAttribute(IJsonClassConfig config)
 {
     return(config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && !config.UsePascalCase);
 }
Пример #26
0
 public void WriteNamespaceEnd(IJsonClassConfig config, TextWriter sw, bool root)
 {
     sw.WriteLine("}");
 }
Пример #27
0
 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();
 }
Пример #28
0
 internal JsonType MaybeMakeNullable(IJsonClassConfig generator)
 {
     if (!generator.AlwaysUseNullableValues) return this;
     return this.GetCommonType(JsonType.GetNull(generator));
 }
Пример #29
0
 /// <summary>
 /// 还原驼峰结构
 /// </summary>
 private bool ShouldApplyNoRenamingAttribute(IJsonClassConfig config)
 {
     return config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && !config.UsePascalCase;
 }
Пример #30
0
 internal JsonType(IJsonClassConfig generator, JsonTypeEnum type)
     : this(generator)
 {
     this.Type = type;
 }
Пример #31
0
 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();
 }
Пример #32
0
 private JsonType(IJsonClassConfig generator)
 {
     this.generator = generator;
 }
Пример #33
0
        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;
        }
Пример #34
0
 public void WriteNamespaceEnd(IJsonClassConfig config, TextWriter sw, bool root)
 {
     sw.WriteLine("}");
 }