zad 7
This commit is contained in:
parent
737e013ecf
commit
095527ea81
9 changed files with 65278 additions and 0 deletions
13
IS_Lab2_JSON_CS/Assets/basic_config.yaml
Normal file
13
IS_Lab2_JSON_CS/Assets/basic_config.yaml
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
paths:
|
||||||
|
source_folder: 'Assets/'
|
||||||
|
json_source_file: 'data.json'
|
||||||
|
yaml_source_file: 'data.yaml'
|
||||||
|
json_destination_file: 'data_mod2.json'
|
||||||
|
yaml_destination_file: 'data_ymod2.yaml'
|
||||||
|
|
||||||
|
launch:
|
||||||
|
operations:
|
||||||
|
- 'deserialize'
|
||||||
|
- 'serialize'
|
||||||
|
- 'convert'
|
||||||
|
source: 'file'
|
||||||
65023
IS_Lab2_JSON_CS/Assets/data.json
Normal file
65023
IS_Lab2_JSON_CS/Assets/data.json
Normal file
File diff suppressed because it is too large
Load diff
18
IS_Lab2_JSON_CS/ConvertJsonToYaml.cs
Normal file
18
IS_Lab2_JSON_CS/ConvertJsonToYaml.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
using YamlDotNet.System.Text.Json;
|
||||||
|
|
||||||
|
namespace IS_Lab2_JSON_CS;
|
||||||
|
|
||||||
|
public static class ConvertJsonToYaml
|
||||||
|
{
|
||||||
|
public static void Run(DeserializeJson deserializeJson, string dest)
|
||||||
|
{
|
||||||
|
var yaml = YamlConverter.Serialize(deserializeJson.Data.RootElement);
|
||||||
|
File.WriteAllText(dest, yaml);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Run(string source, string dest)
|
||||||
|
{
|
||||||
|
var deserialized = new DeserializeJson(source);
|
||||||
|
Run(deserialized, dest);
|
||||||
|
}
|
||||||
|
}
|
||||||
54
IS_Lab2_JSON_CS/DeserializeJson.cs
Normal file
54
IS_Lab2_JSON_CS/DeserializeJson.cs
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace IS_Lab2_JSON_CS;
|
||||||
|
|
||||||
|
public class DeserializeJson
|
||||||
|
{
|
||||||
|
public JsonDocument Data { get; set; }
|
||||||
|
|
||||||
|
public DeserializeJson(string filename)
|
||||||
|
{
|
||||||
|
var text = File.ReadAllText(filename);
|
||||||
|
Data = JsonDocument.Parse(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SomeStats()
|
||||||
|
{
|
||||||
|
var exampleStat = 0;
|
||||||
|
var voivodeships = new Dictionary<string, Dictionary<string, int>>();
|
||||||
|
|
||||||
|
foreach (var obj in Data.RootElement.EnumerateArray())
|
||||||
|
{
|
||||||
|
var voivodeshipName = obj.GetProperty("Województwo")
|
||||||
|
.GetString()!;
|
||||||
|
|
||||||
|
var jstType = obj.GetProperty("typ_JST")
|
||||||
|
.GetString()!;
|
||||||
|
|
||||||
|
if (!voivodeships.TryGetValue(voivodeshipName, out var voivodeshipDict))
|
||||||
|
{
|
||||||
|
voivodeshipDict = new Dictionary<string, int>();
|
||||||
|
voivodeships[voivodeshipName] = voivodeshipDict;
|
||||||
|
}
|
||||||
|
|
||||||
|
voivodeshipDict.TryGetValue(jstType, out var amount);
|
||||||
|
voivodeshipDict[jstType] = amount + 1;
|
||||||
|
|
||||||
|
if (jstType == "GM" &&
|
||||||
|
voivodeshipName.Equals("dolnośląskie", StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
exampleStat += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"Liczba urzędów miejskich w województwie dolnośląskim: {exampleStat}");
|
||||||
|
foreach (var voivodeshipPair in voivodeships)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Województwo {voivodeshipPair.Key}: ");
|
||||||
|
foreach (var typePair in voivodeshipPair.Value)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"\t- {typePair.Key}: {typePair.Value}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
IS_Lab2_JSON_CS/IS_Lab2_JSON_CS.csproj
Normal file
28
IS_Lab2_JSON_CS/IS_Lab2_JSON_CS.csproj
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="YamlDotNet" Version="16.3.0" />
|
||||||
|
<PackageReference Include="YamlDotNet.System.Text.Json" Version="1.6.5" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Assets\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="Assets\basic_config.yaml">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
<None Update="Assets\data.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
16
IS_Lab2_JSON_CS/IS_Lab2_JSON_CS.sln
Normal file
16
IS_Lab2_JSON_CS/IS_Lab2_JSON_CS.sln
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IS_Lab2_JSON_CS", "IS_Lab2_JSON_CS.csproj", "{149E3136-E2CA-441B-B4C5-2F260E5DF5E2}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{149E3136-E2CA-441B-B4C5-2F260E5DF5E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{149E3136-E2CA-441B-B4C5-2F260E5DF5E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{149E3136-E2CA-441B-B4C5-2F260E5DF5E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{149E3136-E2CA-441B-B4C5-2F260E5DF5E2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
66
IS_Lab2_JSON_CS/Program.cs
Normal file
66
IS_Lab2_JSON_CS/Program.cs
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
// See https://aka.ms/new-console-template for more information
|
||||||
|
|
||||||
|
using IS_Lab2_JSON_CS;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
using YamlDotNet.Serialization.NamingConventions;
|
||||||
|
|
||||||
|
var serializer = new DeserializerBuilder()
|
||||||
|
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
DeserializeJson newDeserializer = null!;
|
||||||
|
var config = serializer.Deserialize<YamlConfig>(File.ReadAllText("Assets/basic_config.yaml"));
|
||||||
|
var operations = new Dictionary<string, Action<string>>()
|
||||||
|
{
|
||||||
|
{ "deserialize", Deserialize },
|
||||||
|
{ "serialize", Serialize },
|
||||||
|
{ "convert", Convert }
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var op in config.Launch.Operations)
|
||||||
|
{
|
||||||
|
if (!operations.TryGetValue(op, out var callback))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
callback(config.Launch.Source);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Deserialize(string src)
|
||||||
|
{
|
||||||
|
newDeserializer = new DeserializeJson($"{config.Paths.SourceFolder}{config.Paths.JsonSourceFile}");
|
||||||
|
newDeserializer.SomeStats();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Serialize(string src)
|
||||||
|
{
|
||||||
|
switch (src)
|
||||||
|
{
|
||||||
|
case "object":
|
||||||
|
SerializeJson.Run(newDeserializer, $"{config.Paths.SourceFolder}{config.Paths.JsonDestinationFile}");
|
||||||
|
return;
|
||||||
|
|
||||||
|
case "file":
|
||||||
|
SerializeJson.Run($"{config.Paths.SourceFolder}{config.Paths.JsonSourceFile}", $"{config.Paths.SourceFolder}{config.Paths.JsonDestinationFile}");
|
||||||
|
return;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(src));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Convert(string src)
|
||||||
|
{
|
||||||
|
switch (src)
|
||||||
|
{
|
||||||
|
case "object":
|
||||||
|
ConvertJsonToYaml.Run(newDeserializer, $"{config.Paths.SourceFolder}{config.Paths.YamlDestinationFile}");
|
||||||
|
return;
|
||||||
|
|
||||||
|
case "file":
|
||||||
|
ConvertJsonToYaml.Run($"{config.Paths.SourceFolder}{config.Paths.JsonSourceFile}", $"{config.Paths.SourceFolder}{config.Paths.YamlDestinationFile}");
|
||||||
|
return;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(src));
|
||||||
|
}
|
||||||
|
}
|
||||||
38
IS_Lab2_JSON_CS/SerializeJson.cs
Normal file
38
IS_Lab2_JSON_CS/SerializeJson.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Unicode;
|
||||||
|
|
||||||
|
namespace IS_Lab2_JSON_CS;
|
||||||
|
|
||||||
|
public static class SerializeJson
|
||||||
|
{
|
||||||
|
public static void Run(DeserializeJson deserializeJson, string fileLocation)
|
||||||
|
{
|
||||||
|
var list = new List<object>();
|
||||||
|
foreach (var obj in deserializeJson.Data.RootElement.EnumerateArray())
|
||||||
|
{
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
Kod_TERYT = obj.GetProperty("Kod_TERYT"),
|
||||||
|
Województwo = obj.GetProperty("Województwo"),
|
||||||
|
Powiat = obj.GetProperty("Powiat"),
|
||||||
|
typ_JST = obj.GetProperty("typ_JST"),
|
||||||
|
nazwa_urzędu_JST = obj.GetProperty("nazwa_urzędu_JST"),
|
||||||
|
miejscowość = obj.GetProperty("miejscowość"),
|
||||||
|
telefon_z_numerem_kierunkowym = $"{obj.GetProperty("telefonkierunkowy").ToString().Trim()}{obj.GetProperty("telefon").ToString().Trim()}"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
File.WriteAllText(fileLocation, JsonSerializer.Serialize(list, new JsonSerializerOptions(JsonSerializerOptions.Default)
|
||||||
|
{
|
||||||
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
||||||
|
}), Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Run(string origin, string fileLocation)
|
||||||
|
{
|
||||||
|
var deserialized = new DeserializeJson(origin);
|
||||||
|
Run(deserialized, fileLocation);
|
||||||
|
}
|
||||||
|
}
|
||||||
22
IS_Lab2_JSON_CS/YamlConfig.cs
Normal file
22
IS_Lab2_JSON_CS/YamlConfig.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
namespace IS_Lab2_JSON_CS;
|
||||||
|
|
||||||
|
public class YamlConfig
|
||||||
|
{
|
||||||
|
public class PathsConfig
|
||||||
|
{
|
||||||
|
public string SourceFolder { get; set; } = null!;
|
||||||
|
public string JsonSourceFile { get; set; } = null!;
|
||||||
|
public string YamlSourceFile { get; set; } = null!;
|
||||||
|
public string JsonDestinationFile { get; set; } = null!;
|
||||||
|
public string YamlDestinationFile { get; set; } = null!;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LaunchConfig
|
||||||
|
{
|
||||||
|
public string[] Operations { get; set; } = null!;
|
||||||
|
public string Source { get; set; } = null!;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PathsConfig Paths { get; set; } = null!;
|
||||||
|
public LaunchConfig Launch { get; set; } = null!;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue