Exemple #1
0
        public void ToJsonFile(string destFileName = null)
        {
            var fName = string.IsNullOrEmpty(destFileName) ? new RaiFile(FullName) : new RaiFile(destFileName);

            fName.Ext = "json";
            var jsonFile = new TextFile(fName.FullName);

            jsonFile.rm();
            var fieldNames = FieldNames();
            Dictionary <string, string> item = null;
            string line;
            string value;
            long   l;
            double d;

            jsonFile.Append("[");
            for (int i = 1; i < Lines.Count; i++)
            {
                line = "{";
                item = this[i];
                foreach (string name in fieldNames)
                {
                    value = item[name];
                    if (!(long.TryParse(value, out l) || double.TryParse(value, out d)))                     // problem? => will parse long until '.' and ignore rest
                    {
                        value = "\"" + value + "\"";
                    }
                    line += $"\"{name}\": {value},";
                }
                jsonFile.Append(line.Substring(0, line.Length - 1) + "},");
            }
            int llNr     = jsonFile.Lines.Count - 1;
            var lastLine = jsonFile.Lines[llNr];

            jsonFile.Delete(llNr);
            jsonFile.Append(lastLine.Substring(0, lastLine.Length - 1) + "]");
            jsonFile.Save();
        }