fix: address code review issues in converter tool
This commit is contained in:
@@ -74,7 +74,14 @@ foreach (var jsonFile in jsonFiles)
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Total: {totalOriginalSize:N0} -> {totalNewSize:N0} bytes ({(double)totalNewSize / totalOriginalSize * 100:F1}%)");
|
||||
if (totalOriginalSize > 0)
|
||||
{
|
||||
Console.WriteLine($"Total: {totalOriginalSize:N0} -> {totalNewSize:N0} bytes ({(double)totalNewSize / totalOriginalSize * 100:F1}%)");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("No files were converted.");
|
||||
}
|
||||
return 0;
|
||||
|
||||
static DataTable CreateDataTable(List<Dictionary<string, JsonElement>> records)
|
||||
@@ -112,6 +119,7 @@ static DataTable CreateDataTable(List<Dictionary<string, JsonElement>> records)
|
||||
|
||||
static Type InferType(JsonElement element) => element.ValueKind switch
|
||||
{
|
||||
JsonValueKind.String when element.GetString() is string s && DateTime.TryParse(s, out _) => typeof(DateTime),
|
||||
JsonValueKind.String => typeof(string),
|
||||
JsonValueKind.Number when element.TryGetInt64(out _) => typeof(long),
|
||||
JsonValueKind.Number => typeof(decimal),
|
||||
@@ -126,12 +134,14 @@ static object ConvertValue(JsonElement element, Type targetType)
|
||||
return DBNull.Value;
|
||||
|
||||
if (targetType == typeof(string))
|
||||
return (object?)element.GetString() ?? DBNull.Value;
|
||||
|
||||
if (targetType == typeof(DateTime))
|
||||
{
|
||||
var str = element.GetString();
|
||||
// Try to parse as DateTime if it looks like one
|
||||
if (str != null && DateTime.TryParse(str, out var dt))
|
||||
return dt;
|
||||
return (object?)str ?? DBNull.Value;
|
||||
return DBNull.Value;
|
||||
}
|
||||
|
||||
if (targetType == typeof(long))
|
||||
|
||||
Reference in New Issue
Block a user