-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathXmlSerializer.cs
186 lines (162 loc) · 6.15 KB
/
XmlSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using WebApiClientCore.Internals;
namespace WebApiClientCore.Serialization
{
/// <summary>
/// Xml序列化工具
/// </summary>
public static class XmlSerializer
{
private static readonly XmlReaderSettings readerSettings = new();
private static readonly XmlWriterSettings writerSettings = new();
/// <summary>
/// 将对象序列化为Xml文本
/// </summary>
/// <param name="obj">对象</param>
/// <param name="options">配置选项</param>
/// <returns></returns>
[RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public static string? Serialize(object? obj, XmlWriterSettings? options)
{
if (obj == null)
{
return null;
}
using var bufferWriter = new RecyclableBufferWriter<char>();
Serialize(obj, options, bufferWriter);
return bufferWriter.WrittenSpan.ToString();
}
/// <summary>
/// 将对象序列化为Xml文本
/// </summary>
/// <param name="bufferWriter">buffer写入器</param>
/// <param name="obj">对象</param>
/// <param name="options">配置选项</param>
[RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public static void Serialize(object? obj, XmlWriterSettings? options, IBufferWriter<char> bufferWriter)
{
if (obj == null)
{
return;
}
var settings = options ?? writerSettings;
using var writer = new XmlBufferWriter(bufferWriter, settings.Encoding);
using var xmlWriter = XmlWriter.Create(writer, settings);
var xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
xmlSerializer.Serialize(xmlWriter, obj);
}
/// <summary>
/// 将Xml文本反序列化对象
/// </summary>
/// <param name="xml">xml文本内容</param>
/// <param name="objType">对象类型</param>
/// <param name="options">配置选项</param>
/// <returns></returns>
[RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public static object? Deserialize(string? xml, Type objType, XmlReaderSettings? options)
{
if (objType == null)
{
throw new ArgumentNullException(nameof(objType));
}
if (string.IsNullOrEmpty(xml))
{
return objType.DefaultValue();
}
var settings = options ?? readerSettings;
using var reader = new StringReader(xml);
using var xmlReader = XmlReader.Create(reader, settings);
var xmlSerializer = new System.Xml.Serialization.XmlSerializer(objType);
return xmlSerializer.Deserialize(xmlReader);
}
private class XmlBufferWriter : TextWriter
{
private readonly IBufferWriter<char> bufferWriter;
public override Encoding Encoding { get; }
public XmlBufferWriter(IBufferWriter<char> bufferWriter, Encoding encoding)
{
this.bufferWriter = bufferWriter;
this.Encoding = encoding;
}
public override Task FlushAsync()
{
return Task.CompletedTask;
}
public override void Write(ReadOnlySpan<char> buffer)
{
this.bufferWriter.Write(buffer);
}
public override void Write(char value)
{
Span<char> buffer = [value];
this.Write(buffer);
}
public override void Write(char[] buffer, int index, int count)
{
this.Write(buffer.AsSpan(index, count));
}
public override void Write(string? value)
{
this.Write(value.AsSpan());
}
public override Task WriteAsync(string? value)
{
this.Write(value.AsSpan());
return Task.CompletedTask;
}
public override Task WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
{
this.Write(buffer.Span);
return Task.CompletedTask;
}
public override Task WriteAsync(char value)
{
Span<char> buffer = [value];
this.Write(buffer);
return Task.CompletedTask;
}
public override Task WriteAsync(char[] buffer, int index, int count)
{
this.Write(buffer.AsSpan(index, count));
return Task.CompletedTask;
}
public override void WriteLine(ReadOnlySpan<char> buffer)
{
this.Write(buffer);
WriteLine();
}
public override Task WriteLineAsync(string? value)
{
this.Write(value.AsSpan());
WriteLine();
return Task.CompletedTask;
}
public override Task WriteLineAsync(char value)
{
Span<char> buffer = [value];
this.Write(buffer);
WriteLine();
return Task.CompletedTask;
}
public override Task WriteLineAsync(char[] buffer, int index, int count)
{
this.Write(buffer.AsSpan(0, count));
WriteLine();
return Task.CompletedTask;
}
public override Task WriteLineAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
{
this.Write(buffer.Span);
WriteLine();
return Task.CompletedTask;
}
}
}
}