1
2
3
4
5
6 using System;
7 using System.Collections;
8 using System.Dom;
9
10 namespace System.Xml.Serialization
11 {
12 public concept XmlExportableClassType<T>
13 {
14 Element* T.ToXml(const string& fieldName, XmlSerializationContext& ctx);
15 }
16
17 public concept XmlExportableScalarType<T>
18 {
19 where
20 T is bool or
21 T is sbyte or
22 T is byte or
23 T is short or
24 T is ushort or
25 T is int or
26 T is uint or
27 T is long or
28 T is ulong or
29 T is float or
30 T is double or
31 T is char or
32 T is wchar or
33 T is uchar or
34 T is System.Uuid;
35 }
36
37 public concept XmlExportableEnumeratedType<T>
38 {
39 where System.Meta.IsEnumeratedType<T>();
40 }
41
42 public concept XmlExportableTimeType<T>
43 {
44 where T is System.Date or T is System.DateTime or T is System.Timestamp;
45 }
46
47 public concept XmlExportable<T>
48 {
49 where
50 T is XmlExportableClassType or
51 T is XmlExportableScalarType or
52 T is XmlExportableEnumeratedType or
53 T is XmlExportableTimeType or
54 T is string or
55 T is wstring or
56 T is ustring or
57 T is System.TimePoint or
58 T is System.Duration;
59 }
60
61 public UniquePtr<Element> ToXml(const System.Uuid& value, const string& fieldName, XmlSerializationContext& ctx)
62 {
63 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
64 element->SetAttribute(u"value", ToUtf32(ToString(value)));
65 return element;
66 }
67
68 public UniquePtr<Element> ToXml<T>(const T& object, const string& fieldName, XmlSerializationContext& ctx)
69 where T is XmlExportableClassType
70 {
71 UniquePtr<Element> element = object.ToXml(fieldName, ctx);
72 if (!ctx.GetFlag(System.Xml.Serialization.XmlSerializationFlags.suppressMetadata))
73 {
74 XmlSerializable intf = object;
75 int classId = intf.ClassId();
76 element->SetAttribute(u"classId", ToUtf32(ToString(classId)));
77 }
78 return element;
79 }
80
81 public UniquePtr<Element> ToXml<T>(const UniquePtr<T>& objectPtr, const string& fieldName, XmlSerializationContext& ctx)
82 where T is XmlExportableClassType
83 {
84 if (objectPtr.IsNull())
85 {
86 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
87 element->SetAttribute(u"value", u"null");
88 return element;
89 }
90 else
91 {
92 UniquePtr<Element> element = objectPtr->ToXml(fieldName, ctx);
93 if (!ctx.GetFlag(System.Xml.Serialization.XmlSerializationFlags.suppressMetadata))
94 {
95 T* p = objectPtr.Get();
96 XmlSerializable intf = *p;
97 int classId = intf.ClassId();
98 element->SetAttribute(u"classId", ToUtf32(ToString(classId)));
99 }
100 return element;
101 }
102 }
103
104 public UniquePtr<Element> ToXml<T>(const SharedPtr<T>& objectPtr, const string& fieldName, XmlSerializationContext& ctx)
105 where T is XmlExportableClassType
106 {
107 if (objectPtr.IsNull())
108 {
109 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
110 element->SetAttribute(u"value", u"null");
111 return element;
112 }
113 else
114 {
115 UniquePtr<Element> element = objectPtr->ToXml(fieldName, ctx);
116 if (!ctx.GetFlag(System.Xml.Serialization.XmlSerializationFlags.suppressMetadata))
117 {
118 T* p = objectPtr.Get();
119 XmlSerializable intf = *p;
120 int classId = intf.ClassId();
121 element->SetAttribute(u"classId", ToUtf32(ToString(classId)));
122 }
123 return element;
124 }
125 }
126
127 public UniquePtr<Element> ToXml<T>(const XmlPtr<T>& xmlPtr, const string& fieldName, XmlSerializationContext& ctx)
128 where T is XmlExportableClassType
129 {
130 if (xmlPtr.IsNull())
131 {
132 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
133 element->SetAttribute(u"value", u"null");
134 return element;
135 }
136 else
137 {
138 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
139 element->SetAttribute(u"objectId", ToUtf32(ToString(xmlPtr.TargetObjectId())));
140 return element;
141 }
142 }
143
144 public UniquePtr<Element> ToXml<T>(const UniqueXmlPtr<T>& xmlPtr, const string& fieldName, XmlSerializationContext& ctx)
145 where T is XmlExportableClassType
146 {
147 if (xmlPtr.IsNull())
148 {
149 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
150 element->SetAttribute(u"value", u"null");
151 return element;
152 }
153 else
154 {
155 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
156 element->SetAttribute(u"objectId", ToUtf32(ToString(xmlPtr.TargetObjectId())));
157 return element;
158 }
159 }
160
161 public UniquePtr<Element> ToXml<T>(const List<T>& list, const string& fieldName, XmlSerializationContext& ctx)
162 where T is XmlExportable
163 {
164 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
165 for (const T& item : list)
166 {
167 element->AppendChild(UniquePtr<Node>(ToXml(item, "item", ctx).Release()));
168 }
169 return element;
170 }
171
172 public UniquePtr<Element> ToXml<T>(const List<UniquePtr<T>>& list, const string& fieldName, XmlSerializationContext& ctx)
173 where T is XmlExportableClassType
174 {
175 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
176 for (const UniquePtr<T>& item : list)
177 {
178 element->AppendChild(UniquePtr<Node>(ToXml(item, "item", ctx).Release()));
179 }
180 return element;
181 }
182
183 public UniquePtr<Element> ToXml<T>(const List<SharedPtr<T>>& list, const string& fieldName, XmlSerializationContext& ctx)
184 where T is XmlExportableClassType
185 {
186 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
187 for (const SharedPtr<T>& item : list)
188 {
189 element->AppendChild(UniquePtr<Node>(ToXml(item, "item", ctx).Release()));
190 }
191 return element;
192 }
193
194 public UniquePtr<Element> ToXml<T>(const List<XmlPtr<T>>& list, const string& fieldName, XmlSerializationContext& ctx)
195 where T is XmlExportableClassType
196 {
197 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
198 for (const XmlPtr<T>& item : list)
199 {
200 element->AppendChild(UniquePtr<Node>(ToXml(item, "item", ctx).Release()));
201 }
202 return element;
203 }
204
205 public UniquePtr<Element> ToXml<T>(const List<UniqueXmlPtr<T>>& list, const string& fieldName, XmlSerializationContext& ctx)
206 where T is XmlExportableClassType
207 {
208 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
209 for (const UniqueXmlPtr<T>& item : list)
210 {
211 element->AppendChild(UniquePtr<Node>(ToXml(item, "item", ctx).Release()));
212 }
213 return element;
214 }
215
216 public UniquePtr<Element> ToXml(const string& value, const string& fieldName, XmlSerializationContext& ctx)
217 {
218 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
219 element->SetAttribute(u"value", ToUtf32(value));
220 return element;
221 }
222
223 public UniquePtr<Element> ToXml(const wstring& value, const string& fieldName, XmlSerializationContext& ctx)
224 {
225 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
226 element->SetAttribute(u"value", ToUtf32(value));
227 return element;
228 }
229
230 public UniquePtr<Element> ToXml(const ustring& value, const string& fieldName, XmlSerializationContext& ctx)
231 {
232 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
233 element->SetAttribute(u"value", value);
234 return element;
235 }
236
237 public UniquePtr<Element> ToXml(const System.TimePoint& value, const string& fieldName, XmlSerializationContext& ctx)
238 {
239 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
240 element->SetAttribute(u"value", ToUtf32(ToString(value.Rep())));
241 return element;
242 }
243
244 public UniquePtr<Element> ToXml(const System.Duration& value, const string& fieldName, XmlSerializationContext& ctx)
245 {
246 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
247 element->SetAttribute(u"value", ToUtf32(ToString(value.Rep())));
248 return element;
249 }
250
251 public UniquePtr<Element> ToXml<T>(const T& value, const string& fieldName, XmlSerializationContext& ctx)
252 where T is XmlExportableScalarType
253 {
254 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
255 element->SetAttribute(u"value", ToUtf32(ToString(value)));
256 return element;
257 }
258
259 public UniquePtr<Element> ToXml<T>(T value, const string& fieldName, XmlSerializationContext& ctx)
260 where T is XmlExportableEnumeratedType
261 {
262 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
263 T.UnderlyingType x = cast<T.UnderlyingType>(value);
264 element->SetAttribute(u"value", ToUtf32(ToString(cast<long>(x))));
265 return element;
266 }
267
268 public UniquePtr<Element> ToXml(const System.Date& value, const string& fieldName, XmlSerializationContext& ctx)
269 {
270 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
271 element->SetAttribute(u"value", ToUtf32(value.ToString()));
272 return element;
273 }
274
275 public UniquePtr<Element> ToXml(const System.DateTime& value, const string& fieldName, XmlSerializationContext& ctx)
276 {
277 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
278 element->SetAttribute(u"value", ToUtf32(value.ToString()));
279 return element;
280 }
281
282 public UniquePtr<Element> ToXml(const System.Timestamp& value, const string& fieldName, XmlSerializationContext& ctx)
283 {
284 UniquePtr<Element> element(new Element(ToUtf32(fieldName)));
285 element->SetAttribute(u"value", ToUtf32(value.ToString()));
286 return element;
287 }
288 }
289