示例#1
0
        public static dynamic DeserializeFromFile(string filePath)
        {
            var json = "";

            try
            {
                if (!File.Exists(filePath))
                {
                    return(new JObject());
                }

                json = SharedFile.ReadAllText(filePath);
                if (string.IsNullOrEmpty(json))
                {
                    return(new JObject());
                }

                var idxNull = json.IndexOf('\0');
                if (idxNull != -1)
                {
                    json = json.Substring(0, idxNull);
                }

                dynamic data = JObject.Parse(json);
                return(data);
            }
            catch (Exception ex)
            {
                Log.Warn("Json parse error: {0}\r\n{1}\r\n{2}\r\n", ex.Message, filePath, json, ex);
                //throw new Exception(string.Format("Json parse error: {0}\r\n{1}\r\n", ex.Message, filePath));
                return(new JObject());
            }
        }
示例#2
0
 public static bool SerializeToFile <T>(T data, string filePath, bool formatted = true)
 {
     try
     {
         var json = Serialize(data, formatted);
         return(SharedFile.WriteAllText(filePath, json));
     }
     catch (Exception ex)
     {
         Log.Exception(filePath, ex);
         //throw new Exception(string.Format("Json parse error: {0}\r\n{1}\r\n", ex.Message, filePath));
     }
     return(false);
 }
        private void AddLine(string line)
        {
            if (Silent || PreprocessMode)
            {
                return;
            }

            var output = $"{Indent}{line}";

            Log.Verbose(output);
            if (!string.IsNullOrEmpty(OutputFile))
            {
                SharedFile.AppendLine(OutputFile, output);
            }
        }
示例#4
0
        public static T TryDeserializeFromFile <T>(string filePath) where T : class
        {
            var json = "";

            try
            {
                json = SharedFile.ReadAllText(filePath);
                return(Deserialize <T>(json));
            }
            catch (Exception ex)
            {
                Log.Warn("Json parse error: {0}\r\n{1}\r\n{2}\r\n", ex.Message, filePath, json, ex);
                return(null);
            }
        }
示例#5
0
        public static T DeserializeFromFile <T>(string filePath)
        {
            var json = "";

            try
            {
                json = SharedFile.ReadAllText(filePath);
                return(Deserialize <T>(json));
            }
            catch (Exception ex)
            {
                Log.Error("Json parse error: {0}\r\n{1}\r\n{2}\r\n", ex.Message, filePath, json, ex);
                throw;
            }
        }
        private void CloseLevel(string line = "")
        {
            if (Silent || PreprocessMode)
            {
                return;
            }

            DecreaseLevel();
            var output = $@"{Indent}{line}{CloseBrace}
";

            Log.Verbose(output);
            if (!string.IsNullOrEmpty(OutputFile))
            {
                SharedFile.AppendLine(OutputFile, output);
            }
        }
        /// <summary>
        /// Process compulation unit to output file
        /// </summary>
        public static bool Process(string inputFile, string outputFile, bool discoverTypes)
        {
            var textCode = SharedFile.ReadAllText(inputFile);

            if (string.IsNullOrEmpty(textCode))
            {
                Log.Error($"empty c# input file: {inputFile}");
                return(false);
            }

            var tree = CSharpSyntaxTree.ParseText(textCode);
            var root = (CompilationUnitSyntax)tree.GetRoot();

            var info = new Pocoyo
            {
                DiscoverTypes = discoverTypes,
                OutputFile    = outputFile,
            };

            if (Log.VerbosMode)
            {
                info.AddLine($@"/*
==========================================
{inputFile}
==========================================
*/
");
            }

            info.Process(root.Members);

            if (Log.VerbosMode)
            {
                info.AddLine($@"/*
==========================================
==========================================
*/
");
            }
            return(true);
        }
        /// <summary>
        /// Pre-process compulation unit to output file
        /// </summary>
        public static bool PreProcess(string inputFile)
        {
            var textCode = SharedFile.ReadAllText(inputFile);

            if (string.IsNullOrEmpty(textCode))
            {
                Log.Error($"empty c# input file: {inputFile}");
                return(false);
            }

            var tree = CSharpSyntaxTree.ParseText(textCode);
            var root = (CompilationUnitSyntax)tree.GetRoot();

            var info = new Pocoyo
            {
                PreprocessMode = true,
                DiscoverTypes  = true,
                Silent         = !Debugger.IsAttached,
            };

            info.Process(root.Members);
            return(true);
        }