Skip to content

Commit 0dfd312

Browse files
committed
- Json added
1 parent 43994da commit 0dfd312

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

Core.Json/FileSystemExtension.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.IO;
2+
using KY.Core.DataAccess;
3+
4+
namespace KY.Core;
5+
6+
public static class FileSystemExtension
7+
{
8+
public static T DeserializeJson<T>(this IFileSystem fileSystem, string path)
9+
{
10+
if (!fileSystem.FileExists(path))
11+
{
12+
return default;
13+
}
14+
using Stream stream = fileSystem.OpenRead(path);
15+
return Json.Deserialize<T>(path);
16+
}
17+
18+
public static void SerializeJson<T>(this IFileSystem fileSystem, T data, string path)
19+
{
20+
using Stream stream = fileSystem.OpenWrite(path);
21+
Json.Serialize(data, path);
22+
}
23+
24+
public static void SerializeJsonIndented<T>(this IFileSystem fileSystem, T data, string path)
25+
{
26+
Json.SerializeIndented(data, path);
27+
}
28+
}

Core.Json/Json.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using KY.Core.DataAccess;
4+
using Newtonsoft.Json;
5+
6+
namespace KY.Core;
7+
8+
public static class Json
9+
{
10+
public static T Deserialize<T>(string path)
11+
{
12+
if (!FileSystem.FileExists(path))
13+
{
14+
return default;
15+
}
16+
using Stream stream = FileSystem.OpenRead(path);
17+
return Deserialize<T>(stream);
18+
}
19+
20+
public static T Deserialize<T>(Stream stream)
21+
{
22+
using StreamReader reader = new(stream);
23+
using JsonTextReader jsonReader = new(reader);
24+
JsonSerializer serializer = new();
25+
return serializer.Deserialize<T>(jsonReader);
26+
}
27+
28+
public static void Serialize<T>(T data, string path, Action<JsonSerializer> hook = null)
29+
{
30+
using Stream stream = FileSystem.OpenWrite(path);
31+
Serialize(data, stream, hook);
32+
}
33+
34+
public static void Serialize<T>(T data, Stream stream, Action<JsonSerializer> hook = null)
35+
{
36+
using StreamWriter writer = new(stream);
37+
using JsonTextWriter jsonWriter = new(writer);
38+
JsonSerializer serializer = new();
39+
hook?.Invoke(serializer);
40+
serializer.Serialize(jsonWriter, data);
41+
jsonWriter.Flush();
42+
}
43+
44+
public static void SerializeIndented<T>(T data, string path)
45+
{
46+
Serialize(data, path, serializer => serializer.Formatting = Formatting.Indented);
47+
}
48+
49+
public static void SerializeIndented<T>(T data, Stream stream)
50+
{
51+
Serialize(data, stream, serializer => serializer.Formatting = Formatting.Indented);
52+
}
53+
}

Core.Json/KY.Core.Json.csproj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<RootNamespace>KY.Core</RootNamespace>
6+
<Version>4.1.0</Version>
7+
<Authors>KY-Programming</Authors>
8+
<Company>KY-Programmingp</Company>
9+
<Product>KY.Core</Product>
10+
<Copyright>2022 - KY-Programming</Copyright>
11+
<Description>KY.Core.Json for .net Standard
12+
Contains Json serialization and deserialization helpers</Description>
13+
<PackageLicenseExpression>GPL-3.0-or-later</PackageLicenseExpression>
14+
<PackageProjectUrl>https://github.com/KY-Programming/core</PackageProjectUrl>
15+
<PackageIconUrl>https://ky-programming.de/images/logos/128.png</PackageIconUrl>
16+
<RepositoryUrl>https://github.com/KY-Programming/core</RepositoryUrl>
17+
<PackageTags>KY Core Json</PackageTags>
18+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
19+
<LangVersion>latest</LangVersion>
20+
</PropertyGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="KY.Core.Common" Version="4.13.0" />
24+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)