-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConverter.cs
More file actions
38 lines (37 loc) · 1.16 KB
/
Converter.cs
File metadata and controls
38 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using CsvHelper;
using Newtonsoft.Json;
namespace Csv2Json
{
class Converter
{
/// <summary>
/// Parses a csv file at csvPath and saves the json serialized data to a file at jsonPath
/// </summary>
/// <param name="csvPath"></param>
/// <param name="jsonPath"></param>
public static void Convert(string csvPath, string jsonPath)
{
File.WriteAllText(jsonPath, Convert(csvPath));
}
/// <summary>
/// Parses a csv file at csvPath and returns the json serialized data as a string
/// </summary>
/// <param name="csvPath"></param>
public static string Convert(string csvPath)
{
using (var reader = new StreamReader(csvPath))
{
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var csvRecords = csv.GetRecords<dynamic>();
return JsonConvert.SerializeObject(csvRecords.ToList());
}
}
}
}
}