Beispiel #1
0
        /// <summary>
        /// Converts the data grid specified to a CSV new data string
        /// </summary>
        /// <param name="grid">The data grid binder to convert</param>
        /// <returns>A string that represents the data grid in CSV format</returns>
        public string Serialize
        (
            IDataGrid grid
        )
        {
            if (grid == null || grid.Count() == 0)
            {
                return(String.Empty);
            }

            var configuration = new CsvConfiguration()
            {
                Delimiter       = ",",
                HasHeaderRecord = true,
                Quote           = '"'
            };

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var csv = new CsvWriter(writer, configuration))
                    {
                        // Create the CSV headings
                        grid.GetColumnNames().ToList().ForEach
                        (
                            m => csv.WriteField(m)
                        );

                        csv.NextRecord();

                        // Create a CSV record for every row in the data grid
                        foreach (var row in grid)
                        {
                            foreach (var cell in row)
                            {
                                // Get a string representation of the cells value
                                var value =
                                    (
                                        cell.Value == null ? String.Empty : cell.Value.ToString()
                                    );

                                csv.WriteField(value);
                            }

                            csv.NextRecord();
                        }

                        // Reset the memory stream and writers
                        writer.Flush();
                        stream.Position = 0;

                        return(new StreamReader(stream).ReadToEnd());
                    }
        }
Beispiel #2
0
        /// <summary>
        /// Serializes a data grid to a JSON document
        /// </summary>
        /// <param name="grid">The data grid</param>
        /// <returns>A JSON string containing the grids data</returns>
        public string Serialize
        (
            IDataGrid grid
        )
        {
            if (grid == null || grid.Count() == 0)
            {
                return(String.Empty);
            }

            var jsonBuilder = new StringBuilder();
            var rowCount    = grid.Count();
            var columnCount = grid.GetColumnNames().Length;
            var rowNumber   = 1;

            jsonBuilder.Append("{\n");

            jsonBuilder.Append
            (
                "\t\"{0}\": [\n".With
                (
                    grid.Name
                )
            );

            foreach (var row in grid)
            {
                var columnNumber = 1;

                jsonBuilder.Append("\t{\n");

                foreach (var cell in row)
                {
                    // Get a string representation of the cells value
                    var value =
                        (
                            cell.Value == null ? String.Empty : cell.Value.ToString()
                        );

                    // Create a JSON property template
                    var template =
                        (
                            (columnNumber < columnCount)
                            ? "\t\t\"{0}\": \"{1}\",\n"
                            : "\t\t\"{0}\": \"{1}\"\n"
                        );

                    // Add the column name and value to the JSON items properties
                    jsonBuilder.Append
                    (
                        template.With(cell.Key, value)
                    );

                    columnNumber++;
                }

                jsonBuilder.Append
                (
                    (rowNumber < rowCount)
                        ? "\t},\n"
                        : "\t}\n"
                );

                rowNumber++;
            }

            jsonBuilder.Append("]}");

            return(jsonBuilder.ToString());
        }