コード例 #1
0
        public bool PushOutput(IEnumerable <DataRecord> data, string outputLocation)
        {
            if (data == null || data.Count() == 0)
            {
                return(false);
            }

            var jsonDoc = new JArray();

            foreach (var record in data)
            {
                var jRecord = new JObject();

                foreach (var item in record.Items)
                {
                    if (item is SingleItem)
                    {
                        jRecord.Add(item.Name, JToken.FromObject(((SingleItem)item).Value));
                    }

                    if (item is ItemGroup)
                    {
                        var itemGroup = item as ItemGroup;

                        var jGroup = new JObject();

                        foreach (var groupedItem in itemGroup.Items)
                        {
                            jGroup.Add(groupedItem.Name, JToken.FromObject(((SingleItem)groupedItem).Value));
                        }

                        jRecord.Add(item.Name, jGroup);
                    }
                }

                jsonDoc.Add(jRecord);
            }

            var stream = _fileStreamProvider.GetFileStream(outputLocation);

            using (var writer = new StreamWriter(stream, leaveOpen: true))
            {
                writer.Write(jsonDoc.ToString());
                writer.Flush();
                writer.Close();
            }

            return(true);
        }
コード例 #2
0
        public bool GetInput(string inputLocation, out IEnumerable <DataRecord> inputData)
        {
            if (string.IsNullOrWhiteSpace(inputLocation))
            {
                inputData = null;
                return(false);
            }

            var       inputStream = _fileStreamProvider.GetFileStream(inputLocation);
            DataTable parsedData;

            using (var reader = new StreamReader(inputStream))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {
                    csv.Configuration.HasHeaderRecord = true;
                    using (var dr = new CsvDataReader(csv))
                    {
                        parsedData = new DataTable();
                        parsedData.Load(dr);
                    }
                }

            var data = new List <DataRecord>();

            foreach (DataRow row in parsedData.Rows)
            {
                var record = new DataRecord();
                foreach (DataColumn column in row.Table.Columns)
                {
                    if (column.ColumnName.Contains("_"))
                    {
                        var existingItem = (ItemGroup)record.Items.Where(i => i.Name == column.ColumnName.Split('_').First()).FirstOrDefault();
                        if (existingItem != null)
                        {
                            var item = new SingleItem
                            {
                                Name  = column.ColumnName.Split('_')[1],
                                Value = row.Field <string>(column)
                            };
                            existingItem.Items.Add(item);
                        }
                        else
                        {
                            var group = new ItemGroup()
                            {
                                Name = column.ColumnName.Split('_').First()
                            };
                            var item = new SingleItem
                            {
                                Name  = column.ColumnName.Split('_')[1],
                                Value = row.Field <string>(column)
                            };
                            group.Items.Add(item);
                            record.Items.Add(group);
                        }
                    }
                    else
                    {
                        var item = new SingleItem
                        {
                            Name  = column.ColumnName,
                            Value = row.Field <string>(column)
                        };
                        record.Items.Add(item);
                    }
                }

                data.Add(record);
            }

            inputData = data;

            return(true);
        }