Add project files.
This commit is contained in:
21
BuildMultiObjectJson/BuildMultiObjectJson.csproj
Normal file
21
BuildMultiObjectJson/BuildMultiObjectJson.csproj
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.6" />
|
||||||
|
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
7
BuildMultiObjectJson/IJsonConvertService.cs
Normal file
7
BuildMultiObjectJson/IJsonConvertService.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace BuildMultiObjectJson
|
||||||
|
{
|
||||||
|
public interface IJsonConvertService
|
||||||
|
{
|
||||||
|
void Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
150
BuildMultiObjectJson/JsonConvertService.cs
Normal file
150
BuildMultiObjectJson/JsonConvertService.cs
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
using BuildMultiObjectJson.Models;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Serilog;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
// DI, Serilog, Settings
|
||||||
|
|
||||||
|
namespace BuildMultiObjectJson
|
||||||
|
{
|
||||||
|
public class JsonConvertService : IJsonConvertService
|
||||||
|
{
|
||||||
|
private readonly ILogger<JsonConvertService> _log;
|
||||||
|
private readonly IConfiguration _config;
|
||||||
|
|
||||||
|
private List<PersonTmp> _personTmps = new List<PersonTmp>();
|
||||||
|
private List<AddressTmp> _addressTmps = new List<AddressTmp>();
|
||||||
|
private List<EmailAddressTmp> _emailAddressTmps = new List<EmailAddressTmp>();
|
||||||
|
private List<Person> _personList = new List<Person>();
|
||||||
|
int personRecs = 0;
|
||||||
|
int addressRecs = 0;
|
||||||
|
int nextAddressNr = 0;
|
||||||
|
int emailAddressRecs = 0;
|
||||||
|
int nextEmailAddrNr = 0;
|
||||||
|
Random _random = null;
|
||||||
|
|
||||||
|
public JsonConvertService(ILogger<JsonConvertService> log, IConfiguration config)
|
||||||
|
{
|
||||||
|
_log = log;
|
||||||
|
_config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run()
|
||||||
|
{
|
||||||
|
LoadObjectLists();
|
||||||
|
InitPeopleList();
|
||||||
|
AddRandomEmailAddresses(3);
|
||||||
|
AddRandomAddresses(4);
|
||||||
|
RecreatePersonJsonFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RecreatePersonJsonFile()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var json = JsonConvert.SerializeObject(_personList);
|
||||||
|
var newFile = _config.GetValue<string>("PathToFiles") + _config.GetValue<string>("GeneratedFile");
|
||||||
|
File.WriteAllText(newFile, json);
|
||||||
|
_log.LogInformation($"{newFile} successfully written ({json.Length} characters)");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogError(ex, ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddRandomAddresses(int maxAnt)
|
||||||
|
{
|
||||||
|
_personList.ForEach(x =>
|
||||||
|
{
|
||||||
|
if (nextAddressNr < addressRecs)
|
||||||
|
{
|
||||||
|
var Addrs = rndGetNext(maxAnt);
|
||||||
|
x.Addresses.AddRange(_addressTmps.GetRange(nextAddressNr, Addrs));
|
||||||
|
nextAddressNr += Addrs;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
_log.LogInformation($"{nextAddressNr - 1} addresses distributed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddRandomEmailAddresses(int maxAnt)
|
||||||
|
{
|
||||||
|
_personList.ForEach(x =>
|
||||||
|
{
|
||||||
|
if (nextEmailAddrNr < emailAddressRecs)
|
||||||
|
{
|
||||||
|
var emailAddrs = rndGetNext(maxAnt);
|
||||||
|
x.EmailAddresses.AddRange(_emailAddressTmps.GetRange(nextEmailAddrNr, emailAddrs));
|
||||||
|
nextEmailAddrNr += emailAddrs;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
_log.LogInformation($"{nextEmailAddrNr-1} email addresses distributed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitPeopleList()
|
||||||
|
{
|
||||||
|
foreach(var pers in _personTmps)
|
||||||
|
{
|
||||||
|
var person = new Person();
|
||||||
|
CopyAll<PersonTmp, Person>(pers, person);
|
||||||
|
_personList.Add(person);
|
||||||
|
}
|
||||||
|
_log.LogInformation($"{_personList.Count} persons created from tmp-class-list");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CopyAll<T,U>(T source, U target)
|
||||||
|
{
|
||||||
|
var sourceProperties = source.GetType().GetProperties();
|
||||||
|
var targetProperties = target.GetType().GetProperties();
|
||||||
|
|
||||||
|
foreach (var sourceProperty in sourceProperties)
|
||||||
|
{
|
||||||
|
foreach (var targetProperty in targetProperties)
|
||||||
|
{
|
||||||
|
if (targetProperty.Name == sourceProperty.Name && targetProperty.PropertyType == sourceProperty.PropertyType)
|
||||||
|
{
|
||||||
|
targetProperty.SetValue(target, sourceProperty.GetValue(source));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LoadObjectLists()
|
||||||
|
{
|
||||||
|
_personTmps = Jconvert<PersonTmp>(_config.GetValue<string>("PathToFiles") + _config.GetValue<string>("PeopleFile"));
|
||||||
|
personRecs = _personTmps.Count;
|
||||||
|
_log.LogInformation($"{personRecs} records converted");
|
||||||
|
_addressTmps = Jconvert<AddressTmp>(_config.GetValue<string>("PathToFiles") + _config.GetValue<string>("AddressFile"));
|
||||||
|
addressRecs = _addressTmps.Count;
|
||||||
|
_log.LogInformation($"{addressRecs} records converted");
|
||||||
|
_emailAddressTmps = Jconvert<EmailAddressTmp>(_config.GetValue<string>("PathToFiles") + _config.GetValue<string>("EmailAddressFile"));
|
||||||
|
emailAddressRecs = _emailAddressTmps.Count;
|
||||||
|
_log.LogInformation($"{emailAddressRecs} records converted");
|
||||||
|
}
|
||||||
|
|
||||||
|
static List<T> Jconvert<T>(string fName)
|
||||||
|
{
|
||||||
|
using (StreamReader r = new StreamReader(fName))
|
||||||
|
{
|
||||||
|
string json = r.ReadToEnd();
|
||||||
|
return JsonConvert.DeserializeObject<List<T>>(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int rndGetNext(int max)
|
||||||
|
{
|
||||||
|
if(_random == null)
|
||||||
|
{
|
||||||
|
_random = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
return _random.Next(0, max + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
BuildMultiObjectJson/Models/AddressTmp.cs
Normal file
17
BuildMultiObjectJson/Models/AddressTmp.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BuildMultiObjectJson.Models
|
||||||
|
{
|
||||||
|
public class AddressTmp
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string StreetAddress { get; set; }
|
||||||
|
public string City { get; set; }
|
||||||
|
public string State { get; set; }
|
||||||
|
public int ZipCode { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
BuildMultiObjectJson/Models/EmailAddressTmp.cs
Normal file
14
BuildMultiObjectJson/Models/EmailAddressTmp.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BuildMultiObjectJson.Models
|
||||||
|
{
|
||||||
|
public class EmailAddressTmp
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string EmailAddress { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
19
BuildMultiObjectJson/Models/Person.cs
Normal file
19
BuildMultiObjectJson/Models/Person.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BuildMultiObjectJson.Models
|
||||||
|
{
|
||||||
|
public class Person
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
public int Age { get; set; }
|
||||||
|
public List<AddressTmp> Addresses { get; set; } = new List<AddressTmp>();
|
||||||
|
public List<EmailAddressTmp> EmailAddresses { get; set; } = new List<EmailAddressTmp>();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
18
BuildMultiObjectJson/Models/PersonTmp.cs
Normal file
18
BuildMultiObjectJson/Models/PersonTmp.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BuildMultiObjectJson.Models
|
||||||
|
{
|
||||||
|
public class PersonTmp
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
public int Age { get; set; }
|
||||||
|
public string Addresses { get; set; }
|
||||||
|
public string EmailAddresses { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
48
BuildMultiObjectJson/Program.cs
Normal file
48
BuildMultiObjectJson/Program.cs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Serilog;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
// DI, Serilog, Settings
|
||||||
|
|
||||||
|
namespace BuildMultiObjectJson
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
var builder = new ConfigurationBuilder();
|
||||||
|
BuildConfig(builder);
|
||||||
|
|
||||||
|
Log.Logger = new LoggerConfiguration()
|
||||||
|
.ReadFrom.Configuration(builder.Build())
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.WriteTo.Console()
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
|
Log.Logger.Information("Application starting");
|
||||||
|
|
||||||
|
var host = Host.CreateDefaultBuilder()
|
||||||
|
.ConfigureServices((context, services) =>
|
||||||
|
{
|
||||||
|
services.AddTransient<IJsonConvertService, JsonConvertService>();
|
||||||
|
})
|
||||||
|
.UseSerilog()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var svc = ActivatorUtilities.CreateInstance<JsonConvertService>(host.Services);
|
||||||
|
svc.Run();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void BuildConfig(IConfigurationBuilder builder)
|
||||||
|
{
|
||||||
|
builder.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||||
|
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
|
||||||
|
.AddEnvironmentVariables();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
BuildMultiObjectJson/appsettings.json
Normal file
17
BuildMultiObjectJson/appsettings.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"LoopTimes": 15,
|
||||||
|
"PeopleFile": "People.json",
|
||||||
|
"GeneratedFile": "Generated.json",
|
||||||
|
"AddressFile": "Addresses.json",
|
||||||
|
"EmailAddressFile": "EmailAddresses.json",
|
||||||
|
"PathToFiles": "D:\\CSharpDocuments\\JsonData\\",
|
||||||
|
"Serilog": {
|
||||||
|
"MinimumLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Override": {
|
||||||
|
"Microsoft": "Information",
|
||||||
|
"System": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
BuildMultiObjectJsonApp.sln
Normal file
25
BuildMultiObjectJsonApp.sln
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 16
|
||||||
|
VisualStudioVersion = 16.0.30320.27
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuildMultiObjectJson", "BuildMultiObjectJson\BuildMultiObjectJson.csproj", "{A4B7B04A-8836-4BBD-B2FF-811B7C80D94D}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{A4B7B04A-8836-4BBD-B2FF-811B7C80D94D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{A4B7B04A-8836-4BBD-B2FF-811B7C80D94D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{A4B7B04A-8836-4BBD-B2FF-811B7C80D94D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{A4B7B04A-8836-4BBD-B2FF-811B7C80D94D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {12212D25-38B0-44E2-A456-5DBE39BABC4A}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
Reference in New Issue
Block a user