首次提交:本地项目同步到Gitea
This commit is contained in:
30
LibShapes/Core/Serialize/AbstractSerialize.cs
Normal file
30
LibShapes/Core/Serialize/AbstractSerialize.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Serialize
|
||||
{
|
||||
public abstract class AbstractSerialize : ISerialize
|
||||
{
|
||||
// 实现了如下的两个方法。
|
||||
|
||||
public void SerializeObjectToFile(object obj, string file_path)
|
||||
{
|
||||
System.IO.File.WriteAllText(file_path, SerializeObject(obj));
|
||||
}
|
||||
|
||||
public T DeserializeObjectFromFile<T>(string file_path)
|
||||
{
|
||||
return DeserializeObject<T>(System.IO.File.ReadAllText(file_path));
|
||||
}
|
||||
|
||||
|
||||
// 如下的等待具体的类去实现。
|
||||
|
||||
public abstract T DeserializeObject<T>(string value);
|
||||
|
||||
public abstract string SerializeObject(object obj);
|
||||
|
||||
}
|
||||
}
|
||||
40
LibShapes/Core/Serialize/ISerialize.cs
Normal file
40
LibShapes/Core/Serialize/ISerialize.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Serialize
|
||||
{
|
||||
/// <summary>
|
||||
/// 序列化接口
|
||||
/// </summary>
|
||||
public interface ISerialize
|
||||
{
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
string SerializeObject(Object obj);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
T DeserializeObject<T>(string value);
|
||||
|
||||
// 这里表示从文件中序列化和反序列化
|
||||
|
||||
/// <summary>
|
||||
/// 序列化到文件
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="file_path"></param>
|
||||
void SerializeObjectToFile(Object obj, string file_path);
|
||||
|
||||
|
||||
T DeserializeObjectFromFile<T>(string file_path);
|
||||
}
|
||||
}
|
||||
41
LibShapes/Core/Serialize/JsonSerialize.cs
Normal file
41
LibShapes/Core/Serialize/JsonSerialize.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Serialize
|
||||
{
|
||||
/// <summary>
|
||||
/// json的序列化
|
||||
/// </summary>
|
||||
public class JsonSerialize : AbstractSerialize
|
||||
{
|
||||
public override T DeserializeObject<T>(string value)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(value,jsonSerializerSettings);
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override string SerializeObject(object obj)
|
||||
{
|
||||
|
||||
return JsonConvert.SerializeObject(obj, Formatting.Indented, jsonSerializerSettings);
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 有这个是确保反序列化到正确的类型。
|
||||
/// </summary>
|
||||
private JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Auto, // 自动的,All的话会有问题。
|
||||
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
|
||||
DateFormatString = "yyyy-MM-dd HH:mm:ss", //空值处理
|
||||
//NullValueHandling = NullValueHandling.Ignore, //高级用法九中的`Bool`类型转换设置
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize, // 循环引用的的解决方式,如下如下两种设置。
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.Objects, //
|
||||
Formatting = Formatting.Indented, // 缩进的
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user