Skip to content

Commit

Permalink
消除部分程序集对Newtonsoft.Json的直接依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
zsh2401 committed May 16, 2020
1 parent 84d648a commit f0d0c49
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/AutumnBox.Core/AutumnBox.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="JsonHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
39 changes: 39 additions & 0 deletions src/AutumnBox.Core/JsonHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* ==============================================================================
*
* Filename: JsonHelper
* Description: 
*
* Version: 1.0
* Created: 2020/5/16 21:03:40
* Compiler: Visual Studio 2019
*
* Author: zsh2401
*
* ==============================================================================
*/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutumnBox.Core
{
/// <summary>
/// 具体的Json实现方案
/// </summary>
internal static class JsonHelper
{
public static T DeserializeObject<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
public static string SerializeObject(object jsonObject)
{
return JsonConvert.SerializeObject(jsonObject);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using AutumnBox.Leafx.Container.Support;
using AutumnBox.Core;
using AutumnBox.Leafx.Container.Support;
using AutumnBox.OpenFramework.Open;
using Newtonsoft.Json;
using System;
using System.IO;
namespace AutumnBox.OpenFramework.Implementation
{
Expand Down Expand Up @@ -84,26 +83,18 @@ public FileStream OpenFile(string fileId, bool createIfNotExist = true)

public TResult ReadJsonObject<TResult>(string jsonId)
{
using (var fs = OpenFile(jsonId, false))
{
using (var sr = new StreamReader(fs))
{
var json = sr.ReadToEnd();
return JsonConvert.DeserializeObject<TResult>(json);
}
}
using var fs = OpenFile(jsonId, false);
using var sr = new StreamReader(fs);
var json = sr.ReadToEnd();
return JsonHelper.DeserializeObject<TResult>(json);
}

public void SaveJsonObject(string jsonId, object jsonObject)
{
using (var fs = OpenFile(jsonId))
{
using (var sw = new StreamWriter(fs))
{
var json = JsonConvert.SerializeObject(jsonObject);
sw.Write(json);
}
}
using var fs = OpenFile(jsonId);
using var sw = new StreamWriter(fs);
var json = JsonHelper.SerializeObject(jsonObject);
sw.Write(json);
}

public void Restore()
Expand Down
2 changes: 2 additions & 0 deletions src/AutumnBox.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutumnBox.Core", "AutumnBox
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{B7A3E560-D3F6-4600-A222-2B4B15064381}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnixApp", "UnixApp", "{678DE614-FE81-4ABD-AB53-6AFAE17A2939}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
AutumnBox.Leafx.Shared\AutumnBox.Leafx.Shared.projitems*{2cff3b20-4778-47ef-8d8b-b106f7617954}*SharedItemsImports = 4
Expand Down

0 comments on commit f0d0c49

Please sign in to comment.