扩展序列化接口

This commit is contained in:
falcon 2022-04-24 11:42:24 +08:00
parent ddfd5cd0ef
commit d90af87edd
3 changed files with 56 additions and 6 deletions

View File

@ -1,4 +1,6 @@
namespace Falcon.SugarApi
using System;
namespace Falcon.SugarApi
{
/// <summary>
/// 序列化对象到字符串
@ -20,5 +22,22 @@
/// <param name="str">序列化字符串</param>
/// <returns>对象实例</returns>
public T? Deserialize<T>(string str) where T : class;
/// <summary>
/// 反序列化json字符串
/// </summary>
/// <param name="str">json字符串</param>
/// <param name="returnType">返回类型</param>
/// <returns>json对象</returns>
public object? Deserialize(string str, Type returnType);
/// <summary>
/// 序列化json对象
/// </summary>
/// <param name="obj">json对象</param>
/// <param name="inputType">输入类型</param>
/// <returns>json字符串</returns>
public string Serialize(object obj, Type inputType);
}
}

View File

@ -1,4 +1,5 @@
using System.Text.Json;
using System;
using System.Text.Json;
namespace Falcon.SugarApi.JsonSerialize
{
@ -23,5 +24,22 @@ namespace Falcon.SugarApi.JsonSerialize
/// <returns>json字符串</returns>
public string Serialize<T>(T obj) => JsonSerializer.Serialize(obj);
/// <summary>
/// 反序列化json字符串
/// </summary>
/// <param name="str">json字符串</param>
/// <param name="returnType">返回类型</param>
/// <returns>json对象</returns>
public object? Deserialize(string str, Type returnType) => JsonSerializer.Deserialize(str, returnType);
/// <summary>
/// 序列化json对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">json对象</param>
/// <param name="inputType">输入类型</param>
/// <returns>json字符串</returns>
public string Serialize(object obj, Type inputType) => JsonSerializer.Serialize(obj, inputType);
}
}

View File

@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
@ -47,7 +48,19 @@ namespace Falcon.SugarApi.XmlSerialize
/// <param name="obj">对象</param>
/// <returns>序列化字符串</returns>
public string Serialize<T>(T obj) {
var xmlSerializer = new XmlSerializer(typeof(T));
return Serialize(obj, typeof(T));
}
/// <inheritdoc/>
public object? Deserialize(string str, Type returnType) {
var serializer = new XmlSerializer(returnType);
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(str));
return serializer.Deserialize(ms);
}
/// <inheritdoc/>
public string Serialize(object obj, Type inputType) {
var xmlSerializer = new XmlSerializer(inputType);
using var ms = new MemoryStream();
XmlSerializerNamespaces xmlSerializerNamespaces = new();
if (this.Settings.Namespaces.Count > 0) {
@ -60,11 +73,11 @@ namespace Falcon.SugarApi.XmlSerialize
}
var settings = new XmlWriterSettings {
OmitXmlDeclaration = this.Settings.OmitXmlDeclaration,
Encoding =this.Settings.Encoding,
Encoding = this.Settings.Encoding,
Indent = this.Settings.Indent,
IndentChars = this.Settings.IndentChars,
};
using XmlWriter xmlWriter = new MyXmlWriter(XmlWriter.Create(ms, settings),this.Settings);
using XmlWriter xmlWriter = new MyXmlWriter(XmlWriter.Create(ms, settings), this.Settings);
xmlSerializer.Serialize(xmlWriter, obj, xmlSerializerNamespaces);
ms.Position = 0;
using var sr = new StreamReader(ms, Encoding.UTF8);