Esempio n. 1
0
 public override string Append(string name, List <string> classes, ConverterOptions options)
 {
     if ((Format != null) && Formats.TryGetValue(Format, out var fresult))
     {
         return(fresult);
     }
     if (Types.TryGetValue(Type, out var result))
     {
         return(result);
     }
     throw new ArgumentException("Cannot create class using type: " + Type);
 }
Esempio n. 2
0
        private static string Append(JToken token, string name, Dictionary <TypeDefinition, string> classes,
                                     ConverterOptions options)
        {
            switch (token)
            {
            case JArray arr:
                return(AppendArray(arr, name, classes, options));

            case JObject obj:
                return(AppendObject(obj, name, classes, options));

            case JValue val:
                return(AppendValue(val, options));

            default:
                throw new NotImplementedException();
            }
        }
Esempio n. 3
0
        public string ToString(ConverterOptions options)
        {
            var str = new StringBuilder();

            if (options.AddNamespace)
            {
                str.Append("namespace\r\n{\r\n");
            }
            var classes = new List <string>();

            Append("RootObject", classes, options);

            str.Append(string.Join("\r\n\r\n", classes));
            if (options.AddNamespace)
            {
                str.Append("}\r\n");
            }

            return(str.ToString());
        }
Esempio n. 4
0
        public override string Append(string name, List <string> classes, ConverterOptions options)
        {
            var classname = name.ToSingular();
            var classstr  = new StringBuilder();

            classstr.AppendFormat(CultureInfo.InvariantCulture, "public class {0}\r\n", classname);
            classstr.Append("{\r\n");
            var firstprop = true;

            foreach (var prop in Properties)
            {
                if (firstprop)
                {
                    firstprop = false;
                }
                else
                {
                    classstr.Append("\r\n");
                }

                if (Description != null)
                {
                    classstr.AppendFormat("/// <summary>\r\n/// {0}\r\n/// </summary>\r\n", Description);
                }

                if (options.AddJsonProperty)
                {
                    classstr.AppendFormat("[JsonProperty(\"{0}\")]\r\n", prop.Key);
                }

                var propname = prop.Key.ToCamel();
                var cl       = prop.Value.Append(propname, classes, options);
                classstr.AppendFormat("public {0} {1} {{ get; set; }}\r\n", cl, propname);
            }

            classstr.Append("}\r\n");
            classes.Add(classstr.ToString());

            return(classname);
        }
Esempio n. 5
0
        public static string Convert(JToken obj, ConverterOptions options = null)
        {
            options = options ?? new ConverterOptions();

            var str = new StringBuilder();

            if (options.AddNamespace)
            {
                str.Append("namespace\r\n{\r\n");
            }
            var classes = new Dictionary <TypeDefinition, string>();

            Append(obj, "RootObject", classes, options);

            str.Append(string.Join("\r\n\r\n", classes.Select(p => p.Key.ToString(options))));
            if (options.AddNamespace)
            {
                str.Append("}\r\n");
            }

            return(str.ToString());
        }
Esempio n. 6
0
        private static string AppendValue(JValue val, ConverterOptions options)
        {
            switch (val.Type)
            {
            case JTokenType.Integer:
                return("int");

            case JTokenType.Float:
                return("double");

            case JTokenType.String:
                return("string");

            case JTokenType.Boolean:
                return("bool");

            case JTokenType.Null:
                return("object");

            case JTokenType.Date:
                return("DateTime");

            case JTokenType.Bytes:
                return("byte[]");

            case JTokenType.Guid:
                return("Guid");

            case JTokenType.Uri:
                return("Uri");

            case JTokenType.TimeSpan:
                return("TimeSpan");

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 7
0
        private static string AppendObject(JObject token, string name, Dictionary <TypeDefinition, string> classes,
                                           ConverterOptions options)
        {
            var classname = name.ToSingular();
            var dict      = new Dictionary <string, string>();

            foreach (var prop in token.Properties())
            {
                var propname = options.AddJsonProperty ? prop.Name.ToCamel() : prop.Name;
                var cl       = Append(prop.Value, propname, classes, options);
                dict.Add(prop.Name, cl);
            }

            var typeDef = new TypeDefinition(classname, dict);

            if (classes.TryGetValue(typeDef, out var clname))
            {
                return(clname);
            }

            classes.Add(typeDef, classname);
            return(classname);
        }
Esempio n. 8
0
        private static string AppendArray(JArray token, string name, Dictionary <TypeDefinition, string> classes,
                                          ConverterOptions options)
        {
            string lastType   = null;
            string commonType = null;

            foreach (var item in token.Children <JToken>())
            {
                var type = Append(item, name, classes, options);
                if ((lastType != null) && (type != lastType))
                {
                    commonType = "object";
                }
                lastType = type;
            }

            if (commonType == null)
            {
                commonType = lastType;
            }

            return($"IList<{commonType}>");
        }
Esempio n. 9
0
        public string ToString(ConverterOptions options)
        {
            var classstr = new StringBuilder();

            classstr.AppendFormat(CultureInfo.InvariantCulture, "public class {0}\r\n", TypeName);
            classstr.Append("{\r\n");
            var firstprop = true;

            foreach (var prop in Properties.OrderBy(p => p.Key))
            {
                if (firstprop)
                {
                    firstprop = false;
                }
                else
                {
                    classstr.Append("\r\n");
                }

                if (options.AddJsonProperty)
                {
                    classstr.AppendFormat("[JsonProperty(\"{0}\")]\r\n", prop.Key);
                }

                var propname = options.AddJsonProperty ? prop.Key.ToCamel() : prop.Key;
                var cl       = prop.Value;
                classstr.AppendFormat("public {0} {1} {{ get; set; }}", cl, propname);
                if (options.CreateLists && cl.StartsWith("IList"))
                {
                    classstr.AppendFormat(" = new {0}();", cl.Substring(1));
                }
                classstr.Append("\r\n");
            }

            classstr.Append("}\r\n");
            return(classstr.ToString());
        }
Esempio n. 10
0
 public abstract string Append(string name, List <string> classes, ConverterOptions options);
Esempio n. 11
0
 public override string Append(string name, List <string> classes, ConverterOptions options)
 {
     return("List<" + Items.Append(name, classes, options) + ">");
 }