Exemplo n.º 1
0
        /// <summary>
        /// Json2Class
        /// 自动分析字段
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="json"></param>
        /// <param name="statements"></param>
        private static void Json2Class(string fileName, string json, string NameSpace, List <object> statements, List <object> fieldTypes)
        {
            string clsName = "";

            clsName = Path.GetFileNameWithoutExtension(fileName);
            //输出目录控制
            string outputFile = "Assets/Code/Game@hotfix/Table/" + NameSpace;

            if (Directory.Exists(outputFile) == false)
            {
                Directory.CreateDirectory(outputFile);
            }
            //输出目录
            outputFile = Path.Combine(outputFile, clsName + ".cs");
            //生成类服务
            GenCodeTool genCodeTool = new GenCodeTool(clsName);
            var         jsonData    = JsonMapper.ToObject(json)[0];
            int         i           = 0;

            foreach (var key in jsonData.Keys)
            {
                //字段
                string attribute = "";
                if (key.ToLower() == "id" && key != "Id")
                {
                    Debug.LogErrorFormat("<color=yellow>表格{0}字段必须为Id[大小写区分],请修改后生成</color>", clsName);
                    break;
                }
                else if (key == "Id")
                {
                    //增加一个sqlite主键
                    attribute = "PrimaryKey";
                }

                string type = null;
                if (fieldTypes != null && fieldTypes.Count >= jsonData.Count)
                {
                    type = fieldTypes[i].ToString();
                }

                //添加字段
                genCodeTool.AddField(statements[i].ToString(), attribute, type, key);
                i++;
            }

            //生成代码
            genCodeTool.Save(outputFile, NameSpace);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Json2Class
        /// 自动分析字段
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="json"></param>
        /// <param name="localOrServer"></param>
        /// <param name="statements"></param>
        /// <param name="fieldTypes"></param>
        /// <returns></returns>
        private static string Json2Class(string fileName, string json, string localOrServer, List <object> statements, List <object> fieldTypes)
        {
            string clsName = "";

            clsName = Path.GetFileNameWithoutExtension(fileName);
            //生成类服务
            GenCodeTool genCodeTool = new GenCodeTool(clsName);
            var         jsonData    = JsonMapper.ToObject(json)[0];
            int         i           = 0;

            foreach (var key in jsonData.Keys)
            {
                //字段
                string attribute = "";
                if (key.ToLower() == "id" && key != "Id")
                {
                    Debug.LogErrorFormat("<color=yellow>表格{0}字段必须为Id[大小写区分],请修改后生成</color>", clsName);
                    break;
                }
                else if (key == "Id")
                {
                    //增加一个sqlite主键
                    attribute = "PrimaryKey";
                }

                string type = null;
                if (fieldTypes != null && fieldTypes.Count >= jsonData.Count)
                {
                    type = fieldTypes[i].ToString();
                }

                //添加字段
                genCodeTool.AddField(statements[i].ToString(), attribute, type, key);
                i++;
            }

            //生成代码
            return(genCodeTool.GenClass(localOrServer));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Json2Class
        /// 自动分析字段
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="json"></param>
        /// <param name="statements"></param>
        private static void Json2Class(string fileName,
                                       string json,
                                       List <object> statements,
                                       List <object> fieldTypes = null)
        {
            string clsName = "";

            clsName = Path.GetFileNameWithoutExtension(fileName);
            //输出目录控制
            string outputFile = "Assets/Code/Game@hotfix/Table";

            if (Directory.Exists(outputFile) == false)
            {
                Directory.CreateDirectory(outputFile);
            }

            //输出目录
            outputFile = Path.Combine(outputFile, clsName + ".cs");
            //生成类服务
            GenCodeTool genCodeTool = new GenCodeTool(clsName);
            //
            var jsonData = JsonMapper.ToObject(json)[0];
            int i        = 0;

            foreach (var key in jsonData.Keys)
            {
                //字段
                string attribute = "";
                if (key.ToLower() == "id" && key != "Id")
                {
                    Debug.LogErrorFormat("<color=yellow>表格{0}字段必须为Id[大小写区分],请修改后生成</color>", clsName);
                    break;
                }
                else if (key == "Id")
                {
                    //增加一个sqlite主键
                    attribute = "PrimaryKey";
                }

                string type = null;
                if (fieldTypes != null && fieldTypes.Count >= jsonData.Count)
                {
                    type = fieldTypes[i].ToString();
                }
                else
                {
                    //自动推测字段类型
                    var value = jsonData[key];
                    if (value.IsArray)
                    {
                        var str = value.ToJson();
                        if (str.IndexOf("\"") > 0)
                        {
                            type = "List<string>";
                        }
                        else
                        {
                            type = "List<double>";
                        }
                    }
                    else if (value.IsInt)
                    {
                        type = "int";
                    }
                    else if (value.IsDouble || value.IsLong)
                    {
                        type = "double";
                    }
                    else if (value.IsBoolean)
                    {
                        type = "bool";
                    }
                    else if (value.IsString)
                    {
                        type = "string";
                    }
                }

                //添加字段
                genCodeTool.AddField(statements[i].ToString(), attribute, type, key);
                i++;
            }

            //生成代码
            genCodeTool.Save(outputFile);
        }