コード例 #1
0
ファイル: Json.cs プロジェクト: CarlosOnline/PocoToTypescript
        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
ファイル: Json.cs プロジェクト: CarlosOnline/PocoToTypescript
 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);
 }
コード例 #3
0
        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
ファイル: Json.cs プロジェクト: CarlosOnline/PocoToTypescript
        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
ファイル: Json.cs プロジェクト: CarlosOnline/PocoToTypescript
        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;
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
        /// <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);
        }