Exemplo n.º 1
0
        public String Convert(DataItem data, Format format)
        {
            IConverter <DataItem> converter = null;
            String converted = String.Empty;

            if (data != null)
            {
                data = PerformOperations(data);

                switch (format)
                {
                case Format.JSON:
                    converter = new ParsingServices.Models.JsonConverter();
                    break;

                case Format.CSV:
                    converter = new ParsingServices.Models.CSVConverter();
                    break;

                default:
                    break;
                }

                if (converter.IsValid(data))
                {
                    converted = converter.ToFile(data);
                }
            }

            return(converted);
        }
        public InfoData Save(InputData value)
        {
            var id = Guid.NewGuid();

            Directory.CreateDirectory($"{Location}{Path.DirectorySeparatorChar}{id}{Path.DirectorySeparatorChar}");
            IConverter <DataItem> converter = null;
            DataItem data = new DataItem();

            switch (value.Format)
            {
            case Format.JSON:
                converter = new ParsingServices.Models.JsonConverter();
                break;

            case Format.CSV:
                converter = new ParsingServices.Models.CSVConverter();
                break;

            default:
                break;
            }

            using (FileStream fs = new FileStream($"{Location}{Path.DirectorySeparatorChar}{id}{Path.DirectorySeparatorChar}file.{value.Format}", FileMode.OpenOrCreate)) {
                value.File.CopyTo(fs);
            }

            using (FileStream fs = new FileStream($"{Location}{Path.DirectorySeparatorChar}{id}{Path.DirectorySeparatorChar}file.{value.Format}", FileMode.Open)) {
                data = converter.FromFile(fs);
            }
            var infoData = new InfoData()
            {
                Id           = id,
                Format       = value.Format,
                LocationFile = $"{Location}{Path.DirectorySeparatorChar}{id}{Path.DirectorySeparatorChar}file.{value.Format}",
                Data         = data
            };

            JsonSerializerSettings settings = new JsonSerializerSettings();

            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            settings.Formatting            = Formatting.Indented;

            System.IO.File.WriteAllText($"{Location}{Path.DirectorySeparatorChar}{id}{Path.DirectorySeparatorChar}data.json", JsonConvert.SerializeObject(infoData, settings));

            return(infoData);
        }