扩展序列化接口
This commit is contained in:
parent
ddfd5cd0ef
commit
d90af87edd
|
@ -1,4 +1,6 @@
|
||||||
namespace Falcon.SugarApi
|
using System;
|
||||||
|
|
||||||
|
namespace Falcon.SugarApi
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 序列化对象到字符串
|
/// 序列化对象到字符串
|
||||||
|
@ -20,5 +22,22 @@
|
||||||
/// <param name="str">序列化字符串</param>
|
/// <param name="str">序列化字符串</param>
|
||||||
/// <returns>对象实例</returns>
|
/// <returns>对象实例</returns>
|
||||||
public T? Deserialize<T>(string str) where T : class;
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Text.Json;
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace Falcon.SugarApi.JsonSerialize
|
namespace Falcon.SugarApi.JsonSerialize
|
||||||
{
|
{
|
||||||
|
@ -23,5 +24,22 @@ namespace Falcon.SugarApi.JsonSerialize
|
||||||
/// <returns>json字符串</returns>
|
/// <returns>json字符串</returns>
|
||||||
public string Serialize<T>(T obj) => JsonSerializer.Serialize(obj);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.IO;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
|
@ -47,7 +48,19 @@ namespace Falcon.SugarApi.XmlSerialize
|
||||||
/// <param name="obj">对象</param>
|
/// <param name="obj">对象</param>
|
||||||
/// <returns>序列化字符串</returns>
|
/// <returns>序列化字符串</returns>
|
||||||
public string Serialize<T>(T obj) {
|
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();
|
using var ms = new MemoryStream();
|
||||||
XmlSerializerNamespaces xmlSerializerNamespaces = new();
|
XmlSerializerNamespaces xmlSerializerNamespaces = new();
|
||||||
if (this.Settings.Namespaces.Count > 0) {
|
if (this.Settings.Namespaces.Count > 0) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user