Auto status change to "Under Review"
@@ -0,0 +1,20 | |||||
|
1 | using System.Xml.Serialization; | |||
|
2 | ||||
|
3 | namespace Implab.Test.Model { | |||
|
4 | ||||
|
5 | [XmlRoot(Namespace="urn:implab:test:model")] | |||
|
6 | public class Person { | |||
|
7 | public string FirstName { get; set; } | |||
|
8 | ||||
|
9 | public string LastName { get; set; } | |||
|
10 | ||||
|
11 | public int Age { get; set; } | |||
|
12 | ||||
|
13 | [XmlIgnore] | |||
|
14 | public bool AgeSpecified { get; set; } | |||
|
15 | ||||
|
16 | ||||
|
17 | [XmlElement("Tag")] | |||
|
18 | public string[] Tags { get; set; } | |||
|
19 | } | |||
|
20 | } No newline at end of file |
@@ -0,0 +1,33 | |||||
|
1 | using System; | |||
|
2 | using System.IO; | |||
|
3 | using System.Reflection; | |||
|
4 | using System.Xml; | |||
|
5 | using System.Xml.Xsl; | |||
|
6 | using Implab.Components; | |||
|
7 | using Implab.Formats.Json; | |||
|
8 | ||||
|
9 | namespace Implab.Xml { | |||
|
10 | public class XmlToJson { | |||
|
11 | const string XmlToJsonTransformId = "Implab.Xml.json.xsl"; | |||
|
12 | ||||
|
13 | static LazyAndWeak<XslCompiledTransform> m_default = new LazyAndWeak<XslCompiledTransform>(CreateTransform, true); | |||
|
14 | ||||
|
15 | public static XslCompiledTransform Default { | |||
|
16 | get { return m_default.Value; } | |||
|
17 | } | |||
|
18 | ||||
|
19 | protected static XslCompiledTransform CreateTransform() { | |||
|
20 | var transform = new XslCompiledTransform(); | |||
|
21 | using(var reader = XmlReader.Create(GetDefaultTransform())) { | |||
|
22 | transform.Load(reader); | |||
|
23 | } | |||
|
24 | return transform; | |||
|
25 | } | |||
|
26 | ||||
|
27 | protected static Stream GetDefaultTransform() { | |||
|
28 | return Assembly.GetExecutingAssembly().GetManifestResourceStream(XmlToJsonTransformId); | |||
|
29 | } | |||
|
30 | ||||
|
31 | ||||
|
32 | } | |||
|
33 | } No newline at end of file |
@@ -0,0 +1,252 | |||||
|
1 | <?xml version="1.0" encoding="UTF-8"?> | |||
|
2 | <xsl:stylesheet version="1.0" | |||
|
3 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |||
|
4 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
|
5 | xmlns:exsl="http://exslt.org/common"> | |||
|
6 | <xsl:output method="text" /> | |||
|
7 | ||||
|
8 | <xsl:template match="/"> | |||
|
9 | <xsl:apply-templates mode="json-value" /> | |||
|
10 | </xsl:template> | |||
|
11 | ||||
|
12 | ||||
|
13 | <!-- handle json-object --> | |||
|
14 | ||||
|
15 | <xsl:template match="*" mode="json-object"> | |||
|
16 | <xsl:call-template name="write-members"/> | |||
|
17 | </xsl:template> | |||
|
18 | ||||
|
19 | <xsl:template match="*|@*" mode="json-member"> | |||
|
20 | <xsl:param name="values" select="."/> | |||
|
21 | <xsl:call-template name="write-string"> | |||
|
22 | <xsl:with-param name="text"><xsl:apply-templates select="." mode="json-member-name"/></xsl:with-param> | |||
|
23 | </xsl:call-template> | |||
|
24 | <xsl:text> : </xsl:text> | |||
|
25 | <xsl:apply-templates select="." mode="json-member-value"> | |||
|
26 | <xsl:with-param name="values" select="$values"/> | |||
|
27 | </xsl:apply-templates> | |||
|
28 | </xsl:template> | |||
|
29 | ||||
|
30 | <xsl:template match="*" mode="json-member-name"> | |||
|
31 | <xsl:value-of select="local-name(.)"/> | |||
|
32 | </xsl:template> | |||
|
33 | ||||
|
34 | <xsl:template match="@*" mode="json-member-name"> | |||
|
35 | <xsl:value-of select="concat('_',local-name(.))"/> | |||
|
36 | </xsl:template> | |||
|
37 | ||||
|
38 | <xsl:template match="*|@*" mode="json-member-value"> | |||
|
39 | <xsl:param name="values" select="."/> | |||
|
40 | <xsl:choose> | |||
|
41 | <xsl:when test="count($values) > 1"> | |||
|
42 | <xsl:call-template name="write-array"> | |||
|
43 | <xsl:with-param name="values" select="$values"/> | |||
|
44 | </xsl:call-template> | |||
|
45 | </xsl:when> | |||
|
46 | <xsl:otherwise> | |||
|
47 | <xsl:apply-templates select="$values" mode="json-value"/> | |||
|
48 | </xsl:otherwise> | |||
|
49 | </xsl:choose> | |||
|
50 | </xsl:template> | |||
|
51 | ||||
|
52 | <xsl:template match="*|@*" mode="json-array-item"> | |||
|
53 | <xsl:apply-templates select="." mode="json-value"/> | |||
|
54 | <xsl:if test="position() != last()"> | |||
|
55 | <xsl:text>, </xsl:text> | |||
|
56 | </xsl:if> | |||
|
57 | </xsl:template> | |||
|
58 | ||||
|
59 | <!-- handle json-value --> | |||
|
60 | ||||
|
61 | <xsl:template match="text()[. = 'true'] | @*[. = 'true']" mode="json-value"> | |||
|
62 | <xsl:text>true</xsl:text> | |||
|
63 | </xsl:template> | |||
|
64 | ||||
|
65 | <xsl:template match="text()[. = 'false'] | @*[. = 'false']" | |||
|
66 | mode="json-value"> | |||
|
67 | <xsl:text>false</xsl:text> | |||
|
68 | </xsl:template> | |||
|
69 | ||||
|
70 | <xsl:template match="text()[string(number(.)) != 'NaN'] | @*[string(number(.)) != 'NaN']" | |||
|
71 | mode="json-value"> | |||
|
72 | <xsl:value-of select="number(.)" /> | |||
|
73 | </xsl:template> | |||
|
74 | ||||
|
75 | <xsl:template match="text()|@*" mode="json-value"> | |||
|
76 | <xsl:call-template name="write-string"> | |||
|
77 | <xsl:with-param name="text" select="."/> | |||
|
78 | </xsl:call-template> | |||
|
79 | </xsl:template> | |||
|
80 | ||||
|
81 | <xsl:template match="*[boolean(* | @*) or not(text())]" mode="json-value"> | |||
|
82 | <xsl:call-template name="write-object"/> | |||
|
83 | </xsl:template> | |||
|
84 | ||||
|
85 | <xsl:template match="*[@xsi:nil = 'true']" mode="json-value"> | |||
|
86 | <xsl:text>null</xsl:text> | |||
|
87 | </xsl:template> | |||
|
88 | ||||
|
89 | <!-- template traits --> | |||
|
90 | ||||
|
91 | <xsl:template name="write-value"> | |||
|
92 | <xsl:param name="value" select="."/> | |||
|
93 | <xsl:apply-templates select="$value" mode="json-value"/> | |||
|
94 | </xsl:template> | |||
|
95 | ||||
|
96 | <xsl:template name="write-member"> | |||
|
97 | <xsl:param name="name"/> | |||
|
98 | <xsl:param name="value"/> | |||
|
99 | <xsl:call-template name="write-string"> | |||
|
100 | <xsl:with-param name="text" select="$name"/> | |||
|
101 | </xsl:call-template> | |||
|
102 | <xsl:text> : </xsl:text> | |||
|
103 | <xsl:apply-templates select="$value" mode="json-value"/> | |||
|
104 | </xsl:template> | |||
|
105 | ||||
|
106 | <xsl:template name="write-member-string"> | |||
|
107 | <xsl:param name="name"/> | |||
|
108 | <xsl:param name="value"/> | |||
|
109 | <xsl:call-template name="write-string"> | |||
|
110 | <xsl:with-param name="text" select="$name"/> | |||
|
111 | </xsl:call-template> | |||
|
112 | <xsl:text> : </xsl:text> | |||
|
113 | <xsl:call-template name="write-string"> | |||
|
114 | <xsl:with-param name="text" select="$value"/> | |||
|
115 | </xsl:call-template> | |||
|
116 | </xsl:template> | |||
|
117 | ||||
|
118 | <xsl:template name="write-member-array"> | |||
|
119 | <xsl:param name="name"/> | |||
|
120 | <xsl:param name="values"/> | |||
|
121 | <xsl:call-template name="write-string"> | |||
|
122 | <xsl:with-param name="text" select="$name"/> | |||
|
123 | </xsl:call-template> | |||
|
124 | <xsl:text> : </xsl:text> | |||
|
125 | <xsl:call-template name="write-array"> | |||
|
126 | <xsl:with-param name="values" select="$values"/> | |||
|
127 | </xsl:call-template> | |||
|
128 | </xsl:template> | |||
|
129 | ||||
|
130 | <xsl:template name="write-separator"> | |||
|
131 | <xsl:text>, </xsl:text> | |||
|
132 | </xsl:template> | |||
|
133 | ||||
|
134 | <!-- specialized template traits --> | |||
|
135 | ||||
|
136 | <xsl:template name="write-string"> | |||
|
137 | <xsl:param name="text"/> | |||
|
138 | <xsl:text>"</xsl:text> | |||
|
139 | <xsl:call-template name="escape-bs-string"> | |||
|
140 | <xsl:with-param name="s" select="$text"/> | |||
|
141 | </xsl:call-template> | |||
|
142 | <xsl:text>"</xsl:text> | |||
|
143 | </xsl:template> | |||
|
144 | ||||
|
145 | <xsl:template name="write-object"> | |||
|
146 | <xsl:param name="value" select="."/> | |||
|
147 | <xsl:text>{ </xsl:text> | |||
|
148 | <xsl:apply-templates select="$value" mode="json-object"/> | |||
|
149 | <xsl:text> }</xsl:text> | |||
|
150 | </xsl:template> | |||
|
151 | ||||
|
152 | <xsl:template name="write-array"> | |||
|
153 | <xsl:param name="values"/> | |||
|
154 | ||||
|
155 | <xsl:text>[ </xsl:text> | |||
|
156 | <xsl:apply-templates select="$values" mode="json-array-item"/> | |||
|
157 | <xsl:text> ]</xsl:text> | |||
|
158 | </xsl:template> | |||
|
159 | ||||
|
160 | <xsl:template name="write-members"> | |||
|
161 | <xsl:param name="members" select="*"/> | |||
|
162 | ||||
|
163 | <xsl:for-each select="$members"> | |||
|
164 | <xsl:variable name="current" select="."/> | |||
|
165 | <xsl:variable name="values" select="$members[local-name(.) = local-name($current)]"/> | |||
|
166 | <xsl:if test="generate-id($current) = generate-id($values)"> | |||
|
167 | <xsl:if test="position()>1"> | |||
|
168 | <xsl:call-template name="write-separator"/> | |||
|
169 | </xsl:if> | |||
|
170 | <xsl:apply-templates select="$current" mode="json-member"> | |||
|
171 | <xsl:with-param name="values" select="$values"/> | |||
|
172 | </xsl:apply-templates> | |||
|
173 | </xsl:if> | |||
|
174 | </xsl:for-each> | |||
|
175 | </xsl:template> | |||
|
176 | ||||
|
177 | <!-- escape string --> | |||
|
178 | <!-- | |||
|
179 | Copyright (c) 2006,2008 Doeke Zanstra | |||
|
180 | All rights reserved. | |||
|
181 | https://github.com/doekman/xml2json-xslt/blob/master/xml2json.xslt | |||
|
182 | --> | |||
|
183 | <!-- Escape the backslash (\) before everything else. --> | |||
|
184 | <xsl:template name="escape-bs-string"> | |||
|
185 | <xsl:param name="s"/> | |||
|
186 | <xsl:choose> | |||
|
187 | <xsl:when test="contains($s,'\')"> | |||
|
188 | <xsl:call-template name="escape-quot-string"> | |||
|
189 | <xsl:with-param name="s" select="concat(substring-before($s,'\'),'\\')"/> | |||
|
190 | </xsl:call-template> | |||
|
191 | <xsl:call-template name="escape-bs-string"> | |||
|
192 | <xsl:with-param name="s" select="substring-after($s,'\')"/> | |||
|
193 | </xsl:call-template> | |||
|
194 | </xsl:when> | |||
|
195 | <xsl:otherwise> | |||
|
196 | <xsl:call-template name="escape-quot-string"> | |||
|
197 | <xsl:with-param name="s" select="$s"/> | |||
|
198 | </xsl:call-template> | |||
|
199 | </xsl:otherwise> | |||
|
200 | </xsl:choose> | |||
|
201 | </xsl:template> | |||
|
202 | ||||
|
203 | <!-- Escape the double quote ("). --> | |||
|
204 | <xsl:template name="escape-quot-string"> | |||
|
205 | <xsl:param name="s"/> | |||
|
206 | <xsl:choose> | |||
|
207 | <xsl:when test="contains($s,'"')"> | |||
|
208 | <xsl:call-template name="encode-string"> | |||
|
209 | <xsl:with-param name="s" select="concat(substring-before($s,'"'),'\"')"/> | |||
|
210 | </xsl:call-template> | |||
|
211 | <xsl:call-template name="escape-quot-string"> | |||
|
212 | <xsl:with-param name="s" select="substring-after($s,'"')"/> | |||
|
213 | </xsl:call-template> | |||
|
214 | </xsl:when> | |||
|
215 | <xsl:otherwise> | |||
|
216 | <xsl:call-template name="encode-string"> | |||
|
217 | <xsl:with-param name="s" select="$s"/> | |||
|
218 | </xsl:call-template> | |||
|
219 | </xsl:otherwise> | |||
|
220 | </xsl:choose> | |||
|
221 | </xsl:template> | |||
|
222 | ||||
|
223 | <!-- Replace tab, line feed and/or carriage return by its matching escape code. Can't escape backslash | |||
|
224 | or double quote here, because they don't replace characters (� becomes \t), but they prefix | |||
|
225 | characters (\ becomes \\). Besides, backslash should be seperate anyway, because it should be | |||
|
226 | processed first. This function can't do that. --> | |||
|
227 | <xsl:template name="encode-string"> | |||
|
228 | <xsl:param name="s"/> | |||
|
229 | <xsl:choose> | |||
|
230 | <!-- tab --> | |||
|
231 | <xsl:when test="contains($s,'	')"> | |||
|
232 | <xsl:call-template name="encode-string"> | |||
|
233 | <xsl:with-param name="s" select="concat(substring-before($s,'	'),'\t',substring-after($s,'	'))"/> | |||
|
234 | </xsl:call-template> | |||
|
235 | </xsl:when> | |||
|
236 | <!-- line feed --> | |||
|
237 | <xsl:when test="contains($s,'
')"> | |||
|
238 | <xsl:call-template name="encode-string"> | |||
|
239 | <xsl:with-param name="s" select="concat(substring-before($s,'
'),'\n',substring-after($s,'
'))"/> | |||
|
240 | </xsl:call-template> | |||
|
241 | </xsl:when> | |||
|
242 | <!-- carriage return --> | |||
|
243 | <xsl:when test="contains($s,'
')"> | |||
|
244 | <xsl:call-template name="encode-string"> | |||
|
245 | <xsl:with-param name="s" select="concat(substring-before($s,'
'),'\r',substring-after($s,'
'))"/> | |||
|
246 | </xsl:call-template> | |||
|
247 | </xsl:when> | |||
|
248 | <xsl:otherwise><xsl:value-of select="$s"/></xsl:otherwise> | |||
|
249 | </xsl:choose> | |||
|
250 | </xsl:template> | |||
|
251 | ||||
|
252 | </xsl:stylesheet> No newline at end of file |
@@ -0,0 +1,4 | |||||
|
1 | XML to JSON transform is taken from different project https://hg.implab.org/pub/ModelGenerator/ | |||
|
2 | run: | |||
|
3 | wget https://hg.implab.org/pub/ModelGenerator/raw-file/tip/xslt/json.xsl | |||
|
4 | to update to the latest version No newline at end of file |
@@ -1,152 +1,191 | |||||
1 | using Xunit; |
|
1 | using Xunit; | |
2 | using System; |
|
2 | using System; | |
3 | using Implab.Automaton; |
|
3 | using Implab.Automaton; | |
4 | using Implab.Xml; |
|
4 | using Implab.Xml; | |
5 | using System.Xml; |
|
5 | using System.Xml; | |
6 | using Implab.Formats; |
|
6 | using Implab.Formats; | |
7 | using Implab.Formats.Json; |
|
7 | using Implab.Formats.Json; | |
8 | using System.IO; |
|
8 | using System.IO; | |
|
9 | using Implab.Test.Model; | |||
9 |
|
10 | |||
10 | namespace Implab.Test { |
|
11 | namespace Implab.Test { | |
11 | public class JsonTests { |
|
12 | public class JsonTests { | |
12 |
|
13 | |||
13 | [Fact] |
|
14 | [Fact] | |
14 | public void TestScannerValidTokens() { |
|
15 | public void TestScannerValidTokens() { | |
15 | using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { |
|
16 | using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { | |
16 |
|
17 | |||
17 | Tuple<JsonTokenType, object>[] expexted = { |
|
18 | Tuple<JsonTokenType, object>[] expexted = { | |
18 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "9123"), |
|
19 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "9123"), | |
19 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
|
20 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | |
20 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-123"), |
|
21 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-123"), | |
21 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
|
22 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | |
22 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0"), |
|
23 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0"), | |
23 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
|
24 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | |
24 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0.1"), |
|
25 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0.1"), | |
25 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
|
26 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | |
26 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.2"), |
|
27 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.2"), | |
27 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
|
28 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | |
28 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.1e3"), |
|
29 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.1e3"), | |
29 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
|
30 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | |
30 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "1.3E-3"), |
|
31 | new Tuple<JsonTokenType,object>(JsonTokenType.Number, "1.3E-3"), | |
31 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
|
32 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | |
32 | new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"), |
|
33 | new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"), | |
33 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
|
34 | new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | |
34 | new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"), |
|
35 | new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"), | |
35 | new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, null), |
|
36 | new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, null), | |
36 | new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, null), |
|
37 | new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, null), | |
37 | new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, null), |
|
38 | new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, null), | |
38 | new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, null), |
|
39 | new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, null), | |
39 | new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, null) |
|
40 | new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, null) | |
40 | }; |
|
41 | }; | |
41 |
|
42 | |||
42 | string value; |
|
43 | string value; | |
43 | JsonTokenType tokenType; |
|
44 | JsonTokenType tokenType; | |
44 | for (var i = 0; i < expexted.Length; i++) { |
|
45 | for (var i = 0; i < expexted.Length; i++) { | |
45 |
|
46 | |||
46 | Assert.True(scanner.ReadToken(out value, out tokenType)); |
|
47 | Assert.True(scanner.ReadToken(out value, out tokenType)); | |
47 | Assert.Equal(expexted[i].Item1, tokenType); |
|
48 | Assert.Equal(expexted[i].Item1, tokenType); | |
48 | Assert.Equal(expexted[i].Item2, value); |
|
49 | Assert.Equal(expexted[i].Item2, value); | |
49 | } |
|
50 | } | |
50 |
|
51 | |||
51 | Assert.False(scanner.ReadToken(out value, out tokenType)); |
|
52 | Assert.False(scanner.ReadToken(out value, out tokenType)); | |
52 | } |
|
53 | } | |
53 | } |
|
54 | } | |
54 |
|
55 | |||
55 | [Fact] |
|
56 | [Fact] | |
56 | public void TestScannerBadTokens() { |
|
57 | public void TestScannerBadTokens() { | |
57 | var bad = new[] { |
|
58 | var bad = new[] { | |
58 | " 1", |
|
59 | " 1", | |
59 | " literal", |
|
60 | " literal", | |
60 | " \"", |
|
61 | " \"", | |
61 | "\"unclosed string", |
|
62 | "\"unclosed string", | |
62 | "1.bad", |
|
63 | "1.bad", | |
63 | "001", // should be read as three numbers |
|
64 | "001", // should be read as three numbers | |
64 | "--10", |
|
65 | "--10", | |
65 | "+10", |
|
66 | "+10", | |
66 | "1.0.0", |
|
67 | "1.0.0", | |
67 | "1e1.0", |
|
68 | "1e1.0", | |
68 | "l1teral0", |
|
69 | "l1teral0", | |
69 | ".123", |
|
70 | ".123", | |
70 | "-.123" |
|
71 | "-.123" | |
71 | }; |
|
72 | }; | |
72 |
|
73 | |||
73 | foreach (var json in bad) { |
|
74 | foreach (var json in bad) { | |
74 | using (var scanner = JsonStringScanner.Create(json)) { |
|
75 | using (var scanner = JsonStringScanner.Create(json)) { | |
75 | try { |
|
76 | try { | |
76 | string value; |
|
77 | string value; | |
77 | JsonTokenType token; |
|
78 | JsonTokenType token; | |
78 | scanner.ReadToken(out value, out token); |
|
79 | scanner.ReadToken(out value, out token); | |
79 | if (!Object.Equals(value, json)) { |
|
80 | if (!Object.Equals(value, json)) { | |
80 | Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value); |
|
81 | Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value); | |
81 | continue; |
|
82 | continue; | |
82 | } |
|
83 | } | |
83 | Assert.True(false, $"Token '{json}' shouldn't pass"); |
|
84 | Assert.True(false, $"Token '{json}' shouldn't pass"); | |
84 | } catch (ParserException e) { |
|
85 | } catch (ParserException e) { | |
85 | Console.WriteLine(e.Message); |
|
86 | Console.WriteLine(e.Message); | |
86 | } |
|
87 | } | |
87 | } |
|
88 | } | |
88 | } |
|
89 | } | |
89 | } |
|
90 | } | |
90 |
|
91 | |||
91 | [Fact] |
|
92 | [Fact] | |
92 | public void JsonXmlReaderSimpleTest() { |
|
93 | public void JsonXmlReaderSimpleTest() { | |
93 | var json = "\"some text\""; |
|
94 | var json = "\"some text\""; | |
94 | //Console.WriteLine($"JSON: {json}"); |
|
95 | //Console.WriteLine($"JSON: {json}"); | |
95 | //Console.WriteLine("XML"); |
|
96 | //Console.WriteLine("XML"); | |
96 | /*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) { |
|
97 | /*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) { | |
97 | Assert.AreEqual(xmlReader.ReadState, System.Xml.ReadState.Initial); |
|
98 | Assert.AreEqual(xmlReader.ReadState, System.Xml.ReadState.Initial); | |
98 |
|
99 | |||
99 | AssertRead(xmlReader, XmlNodeType.XmlDeclaration); |
|
100 | AssertRead(xmlReader, XmlNodeType.XmlDeclaration); | |
100 | AssertRead(xmlReader, XmlNodeType.Element); |
|
101 | AssertRead(xmlReader, XmlNodeType.Element); | |
101 | AssertRead(xmlReader, XmlNodeType.Text); |
|
102 | AssertRead(xmlReader, XmlNodeType.Text); | |
102 | AssertRead(xmlReader, XmlNodeType.EndElement); |
|
103 | AssertRead(xmlReader, XmlNodeType.EndElement); | |
103 | Assert.IsFalse(xmlReader.Read()); |
|
104 | Assert.IsFalse(xmlReader.Read()); | |
104 | }*/ |
|
105 | }*/ | |
105 |
|
106 | |||
106 | DumpJsonParse("\"text value\""); |
|
107 | DumpJsonParse("\"text value\""); | |
107 | DumpJsonParse("null"); |
|
108 | DumpJsonParse("null"); | |
108 | DumpJsonParse("true"); |
|
109 | DumpJsonParse("true"); | |
109 | DumpJsonParse("{}"); |
|
110 | DumpJsonParse("{}"); | |
110 | DumpJsonParse("[]"); |
|
111 | DumpJsonParse("[]"); | |
111 | DumpJsonParse("{\"one\":1, \"two\":2}"); |
|
112 | DumpJsonParse("{\"one\":1, \"two\":2}"); | |
112 | DumpJsonParse("[1,\"\",2,3]"); |
|
113 | DumpJsonParse("[1,\"\",2,3]"); | |
113 | DumpJsonParse("[{\"info\": [7,8,9]}]"); |
|
114 | DumpJsonParse("[{\"info\": [7,8,9]}]"); | |
114 | DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); |
|
115 | DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); | |
115 | } |
|
116 | } | |
116 |
|
117 | |||
|
118 | [Fact] | |||
|
119 | public void XmlToJsonTransform() { | |||
|
120 | var person = new Person { | |||
|
121 | FirstName = "Charlie", | |||
|
122 | LastName = "Brown", | |||
|
123 | Age = 19, | |||
|
124 | AgeSpecified = true | |||
|
125 | }; | |||
|
126 | ||||
|
127 | var doc = SerializationHelpers.SerializeAsXmlDocument(person); | |||
|
128 | ||||
|
129 | using (var writer = new StringWriter()) { | |||
|
130 | XmlToJson.Default.Transform(doc,null, writer); | |||
|
131 | Console.WriteLine(writer.ToString()); | |||
|
132 | } | |||
|
133 | } | |||
|
134 | ||||
|
135 | [Fact] | |||
|
136 | public void JsonSerialization() { | |||
|
137 | var person = new Person { | |||
|
138 | FirstName = "Charlie", | |||
|
139 | LastName = "Brown", | |||
|
140 | Age = 19, | |||
|
141 | AgeSpecified = true, | |||
|
142 | Tags = new [] { "brave", "stupid" } | |||
|
143 | }; | |||
|
144 | ||||
|
145 | var data = SerializationHelpers.SerializeJsonAsString(person); | |||
|
146 | Console.WriteLine(data); | |||
|
147 | var clone = SerializationHelpers.DeserializeJsonFromString<Person>(data); | |||
|
148 | ||||
|
149 | Assert.Equal(person.FirstName, clone.FirstName); | |||
|
150 | Assert.Equal(person.LastName, clone.LastName); | |||
|
151 | Assert.Equal(person.Age, clone.Age); | |||
|
152 | Assert.Equal(person.AgeSpecified, clone.AgeSpecified); | |||
|
153 | Assert.Equal(person.Tags, person.Tags); | |||
|
154 | } | |||
|
155 | ||||
117 | void AssertRead(XmlReader reader, XmlNodeType expected) { |
|
156 | void AssertRead(XmlReader reader, XmlNodeType expected) { | |
118 | Assert.True(reader.Read()); |
|
157 | Assert.True(reader.Read()); | |
119 | Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}"); |
|
158 | Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}"); | |
120 | Assert.Equal(expected, reader.NodeType); |
|
159 | Assert.Equal(expected, reader.NodeType); | |
121 | } |
|
160 | } | |
122 |
|
161 | |||
123 | void DumpJsonParse(string json) { |
|
162 | void DumpJsonParse(string json) { | |
124 | Console.WriteLine($"JSON: {json}"); |
|
163 | Console.WriteLine($"JSON: {json}"); | |
125 | Console.WriteLine("XML"); |
|
164 | Console.WriteLine("XML"); | |
126 | using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { |
|
165 | using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { | |
127 | Indent = true, |
|
166 | Indent = true, | |
128 | CloseOutput = false, |
|
167 | CloseOutput = false, | |
129 | ConformanceLevel = ConformanceLevel.Document |
|
168 | ConformanceLevel = ConformanceLevel.Document | |
130 | })) |
|
169 | })) | |
131 | using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { |
|
170 | using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { | |
132 | xmlWriter.WriteNode(xmlReader, false); |
|
171 | xmlWriter.WriteNode(xmlReader, false); | |
133 | } |
|
172 | } | |
134 | Console.WriteLine(); |
|
173 | Console.WriteLine(); | |
135 | } |
|
174 | } | |
136 |
|
175 | |||
137 | void DumpJsonFlatParse(string json) { |
|
176 | void DumpJsonFlatParse(string json) { | |
138 | Console.WriteLine($"JSON: {json}"); |
|
177 | Console.WriteLine($"JSON: {json}"); | |
139 | Console.WriteLine("XML"); |
|
178 | Console.WriteLine("XML"); | |
140 | using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { |
|
179 | using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { | |
141 | Indent = true, |
|
180 | Indent = true, | |
142 | CloseOutput = false, |
|
181 | CloseOutput = false, | |
143 | ConformanceLevel = ConformanceLevel.Document |
|
182 | ConformanceLevel = ConformanceLevel.Document | |
144 | })) |
|
183 | })) | |
145 | using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { |
|
184 | using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { | |
146 | xmlWriter.WriteNode(xmlReader, false); |
|
185 | xmlWriter.WriteNode(xmlReader, false); | |
147 | } |
|
186 | } | |
148 | Console.WriteLine(); |
|
187 | Console.WriteLine(); | |
149 | } |
|
188 | } | |
150 | } |
|
189 | } | |
151 | } |
|
190 | } | |
152 |
|
191 |
@@ -1,64 +1,65 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Threading; |
|
2 | using System.Threading; | |
3 |
|
3 | |||
4 | namespace Implab.Components { |
|
4 | namespace Implab.Components { | |
5 | /// <summary> |
|
5 | /// <summary> | |
6 | /// Creates an instace on-demand and allows it to be garbage collected. |
|
6 | /// Creates an instace on-demand and allows it to be garbage collected. | |
7 | /// </summary> |
|
7 | /// </summary> | |
8 | /// <remarks> |
|
8 | /// <remarks> | |
9 | /// Usefull when dealing with memory-intensive objects which are frequently used. |
|
9 | /// Usefull when dealing with memory-intensive objects which are frequently used. | |
10 | /// This class is similar to <see cref="ObjectPool{T}"/> except it is a singleton. |
|
10 | /// This class is similar to <see cref="ObjectPool{T}"/> except it is a singleton. | |
|
11 | /// This class can't be used to hold diposable objects. | |||
11 | /// </remarks> |
|
12 | /// </remarks> | |
12 | public class LazyAndWeak<T> where T : class { |
|
13 | public class LazyAndWeak<T> where T : class { | |
13 |
|
14 | |||
14 | readonly Func<T> m_factory; |
|
15 | readonly Func<T> m_factory; | |
15 | readonly object m_lock; |
|
16 | readonly object m_lock; | |
16 | WeakReference m_reference; |
|
17 | WeakReference m_reference; | |
17 |
|
18 | |||
18 |
|
19 | |||
19 | public LazyAndWeak(Func<T> factory, bool useLock) { |
|
20 | public LazyAndWeak(Func<T> factory, bool useLock) { | |
20 | Safe.ArgumentNotNull(factory, "factory"); |
|
21 | Safe.ArgumentNotNull(factory, "factory"); | |
21 | m_factory = factory; |
|
22 | m_factory = factory; | |
22 | m_lock = useLock ? new object() : null; |
|
23 | m_lock = useLock ? new object() : null; | |
23 | } |
|
24 | } | |
24 |
|
25 | |||
25 | public LazyAndWeak(Func<T> factory) : this(factory, false) { |
|
26 | public LazyAndWeak(Func<T> factory) : this(factory, false) { | |
26 | } |
|
27 | } | |
27 |
|
28 | |||
28 | public T Value { |
|
29 | public T Value { | |
29 | get { |
|
30 | get { | |
30 | while (true) { |
|
31 | while (true) { | |
31 | var weak = m_reference; |
|
32 | var weak = m_reference; | |
32 | T value; |
|
33 | T value; | |
33 | if (weak != null) { |
|
34 | if (weak != null) { | |
34 | value = weak.Target as T; |
|
35 | value = weak.Target as T; | |
35 | if (value != null) |
|
36 | if (value != null) | |
36 | return value; |
|
37 | return value; | |
37 | } |
|
38 | } | |
38 |
|
39 | |||
39 | if (m_lock == null) { |
|
40 | if (m_lock == null) { | |
40 | value = m_factory(); |
|
41 | value = m_factory(); | |
41 |
|
42 | |||
42 | if (Interlocked.CompareExchange(ref m_reference, new WeakReference(value), weak) == weak) |
|
43 | if (Interlocked.CompareExchange(ref m_reference, new WeakReference(value), weak) == weak) | |
43 | return value; |
|
44 | return value; | |
44 | } else { |
|
45 | } else { | |
45 | lock (m_lock) { |
|
46 | lock (m_lock) { | |
46 | // double check |
|
47 | // double check | |
47 | weak = m_reference; |
|
48 | weak = m_reference; | |
48 | if (weak != null) { |
|
49 | if (weak != null) { | |
49 | value = weak.Target as T; |
|
50 | value = weak.Target as T; | |
50 | if (value != null) |
|
51 | if (value != null) | |
51 | return value; |
|
52 | return value; | |
52 | } |
|
53 | } | |
53 | // we are safe to write |
|
54 | // we are safe to write | |
54 | value = m_factory(); |
|
55 | value = m_factory(); | |
55 | m_reference = new WeakReference(value); |
|
56 | m_reference = new WeakReference(value); | |
56 | return value; |
|
57 | return value; | |
57 | } |
|
58 | } | |
58 | } |
|
59 | } | |
59 | } |
|
60 | } | |
60 | } |
|
61 | } | |
61 | } |
|
62 | } | |
62 | } |
|
63 | } | |
63 | } |
|
64 | } | |
64 |
|
65 |
@@ -1,22 +1,26 | |||||
1 | <Project Sdk="Microsoft.NET.Sdk"> |
|
1 | <Project Sdk="Microsoft.NET.Sdk"> | |
2 |
|
2 | |||
3 | <PropertyGroup> |
|
3 | <PropertyGroup> | |
4 | <Authors>Sergey Smirnov</Authors> |
|
4 | <Authors>Sergey Smirnov</Authors> | |
5 | <Title>Implab library</Title> |
|
5 | <Title>Implab library</Title> | |
6 | <Description>Provides some helper clesses like XML serialization helpers, JSON XML reader, |
|
6 | <Description>Provides some helper clesses like XML serialization helpers, JSON XML reader, | |
7 | JSON pull-parser, ECMA-style promises, lightweight synchonization routines Signal |
|
7 | JSON pull-parser, ECMA-style promises, lightweight synchonization routines Signal | |
8 | and SharedLock, Trace helpers on top of System.Diagnostics, ObjectPool etc. |
|
8 | and SharedLock, Trace helpers on top of System.Diagnostics, ObjectPool etc. | |
9 | </Description> |
|
9 | </Description> | |
10 | <Copyright>2012-2018 Sergey Smirnov</Copyright> |
|
10 | <Copyright>2012-2018 Sergey Smirnov</Copyright> | |
11 | <Version>3.0.8</Version> |
|
11 | <Version>3.0.8</Version> | |
12 | <PackageLicenseUrl>https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt</PackageLicenseUrl> |
|
12 | <PackageLicenseUrl>https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt</PackageLicenseUrl> | |
13 | <PackageProjectUrl>https://implab.org</PackageProjectUrl> |
|
13 | <PackageProjectUrl>https://implab.org</PackageProjectUrl> | |
14 | <RepositoryUrl>https://hg.implab.org/pub/ImplabNet/</RepositoryUrl> |
|
14 | <RepositoryUrl>https://hg.implab.org/pub/ImplabNet/</RepositoryUrl> | |
15 | <RepositoryType>mercurial</RepositoryType> |
|
15 | <RepositoryType>mercurial</RepositoryType> | |
16 | <PackageTags>IMPLAB;Json pull-parser;Json Xml;async;diagnostics;serialization;</PackageTags> |
|
16 | <PackageTags>IMPLAB;Json pull-parser;Json Xml;async;diagnostics;serialization;</PackageTags> | |
17 | <TargetFrameworks>netstandard2.0;net46</TargetFrameworks> |
|
17 | <TargetFrameworks>netstandard2.0;net46</TargetFrameworks> | |
18 | <FrameworkPathOverride Condition="'$(TargetFramework)'=='net46' and '$(OSTYPE)'=='linux'">/usr/lib/mono/4.5/</FrameworkPathOverride> |
|
18 | <FrameworkPathOverride Condition="'$(TargetFramework)'=='net46' and '$(OSTYPE)'=='linux'">/usr/lib/mono/4.5/</FrameworkPathOverride> | |
19 | <DefineConstants Condition="'$(TargetFramework)'=='net46'">NETFX_TRACE_BUG;$(DefineConstants)</DefineConstants> |
|
19 | <DefineConstants Condition="'$(TargetFramework)'=='net46'">NETFX_TRACE_BUG;$(DefineConstants)</DefineConstants> | |
20 | </PropertyGroup> |
|
20 | </PropertyGroup> | |
21 |
|
21 | |||
|
22 | <ItemGroup> | |||
|
23 | <EmbeddedResource Include="Xml\json.xsl"/> | |||
|
24 | </ItemGroup> | |||
|
25 | ||||
22 | </Project> |
|
26 | </Project> |
@@ -1,610 +1,636 | |||||
1 | using Implab.Formats.Json; |
|
1 | using Implab.Formats.Json; | |
2 | using System; |
|
2 | using System; | |
3 | using System.Collections.Generic; |
|
3 | using System.Collections.Generic; | |
4 | using System.Globalization; |
|
4 | using System.Globalization; | |
|
5 | using System.IO; | |||
5 | using System.Linq; |
|
6 | using System.Linq; | |
6 | using System.Xml; |
|
7 | using System.Xml; | |
7 |
|
8 | |||
8 | namespace Implab.Xml { |
|
9 | namespace Implab.Xml { | |
9 | public class JsonXmlReader : XmlReader { |
|
10 | public class JsonXmlReader : XmlReader { | |
10 | struct JsonContext { |
|
11 | struct JsonContext { | |
11 | public string localName; |
|
12 | public string localName; | |
12 | public bool skip; |
|
13 | public bool skip; | |
13 | } |
|
14 | } | |
14 |
|
15 | |||
15 | JsonReader m_parser; |
|
16 | JsonReader m_parser; | |
16 | JsonXmlReaderOptions m_options; |
|
17 | JsonXmlReaderOptions m_options; | |
17 | JsonXmlReaderPosition m_position = JsonXmlReaderPosition.Initial; |
|
18 | JsonXmlReaderPosition m_position = JsonXmlReaderPosition.Initial; | |
18 | XmlNameTable m_nameTable; |
|
19 | XmlNameTable m_nameTable; | |
19 |
|
20 | |||
20 | readonly string m_jsonRootName; |
|
21 | readonly string m_jsonRootName; | |
21 | readonly string m_jsonNamespace; |
|
22 | readonly string m_jsonNamespace; | |
22 | readonly string m_jsonPrefix; |
|
23 | readonly string m_jsonPrefix; | |
23 | readonly bool m_jsonFlattenArrays; |
|
24 | readonly bool m_jsonFlattenArrays; | |
24 | readonly string m_jsonArrayItemName; |
|
25 | readonly string m_jsonArrayItemName; | |
25 |
|
26 | |||
26 | string m_jsonLocalName; |
|
27 | string m_jsonLocalName; | |
27 | string m_jsonValueName; |
|
28 | string m_jsonValueName; | |
28 | bool m_jsonSkip; // indicates wheather to generate closing tag for objects or arrays |
|
29 | bool m_jsonSkip; // indicates wheather to generate closing tag for objects or arrays | |
29 |
|
30 | |||
30 | readonly Stack<JsonContext> m_jsonNameStack = new Stack<JsonContext>(); |
|
31 | readonly Stack<JsonContext> m_jsonNameStack = new Stack<JsonContext>(); | |
31 |
|
32 | |||
32 | XmlQualifiedName m_elementQName; |
|
33 | XmlQualifiedName m_elementQName; | |
33 | string m_elementPrefix; |
|
34 | string m_elementPrefix; | |
34 | int m_elementDepth; |
|
35 | int m_elementDepth; | |
35 | bool m_elementIsEmpty; |
|
36 | bool m_elementIsEmpty; | |
36 |
|
37 | |||
37 | XmlQualifiedName m_qName; |
|
38 | XmlQualifiedName m_qName; | |
38 | string m_prefix; |
|
39 | string m_prefix; | |
39 | int m_xmlDepth; |
|
40 | int m_xmlDepth; | |
40 |
|
41 | |||
41 | XmlSimpleAttribute[] m_attributes; |
|
42 | XmlSimpleAttribute[] m_attributes; | |
42 | string m_value; |
|
43 | string m_value; | |
43 | bool m_isEmpty; |
|
44 | bool m_isEmpty; | |
44 |
|
45 | |||
45 | XmlNodeType m_nodeType = XmlNodeType.None; |
|
46 | XmlNodeType m_nodeType = XmlNodeType.None; | |
46 |
|
47 | |||
47 | bool m_isAttribute; // indicates that we are reading attribute nodes |
|
48 | bool m_isAttribute; // indicates that we are reading attribute nodes | |
48 | int m_currentAttribute; |
|
49 | int m_currentAttribute; | |
49 | bool m_currentAttributeRead; |
|
50 | bool m_currentAttributeRead; | |
50 |
|
51 | |||
51 |
|
52 | |||
52 | XmlNameContext m_context; |
|
53 | XmlNameContext m_context; | |
53 |
|
54 | |||
54 | readonly string m_xmlnsPrefix; |
|
55 | readonly string m_xmlnsPrefix; | |
55 | readonly string m_xmlnsNamespace; |
|
56 | readonly string m_xmlnsNamespace; | |
56 | readonly string m_xsiPrefix; |
|
57 | readonly string m_xsiPrefix; | |
57 | readonly string m_xsiNamespace; |
|
58 | readonly string m_xsiNamespace; | |
58 |
|
59 | |||
59 |
|
60 | |||
60 | public JsonXmlReader(JsonReader parser, JsonXmlReaderOptions options) { |
|
61 | public JsonXmlReader(JsonReader parser, JsonXmlReaderOptions options) { | |
61 | Safe.ArgumentNotNull(parser, nameof(parser)); |
|
62 | Safe.ArgumentNotNull(parser, nameof(parser)); | |
62 | m_parser = parser; |
|
63 | m_parser = parser; | |
63 |
|
64 | |||
64 | m_options = options ?? new JsonXmlReaderOptions(); |
|
65 | m_options = options ?? new JsonXmlReaderOptions(); | |
65 |
|
66 | |||
66 | m_jsonFlattenArrays = m_options.FlattenArrays; |
|
67 | m_jsonFlattenArrays = m_options.FlattenArrays; | |
67 | m_nameTable = m_options.NameTable ?? new NameTable(); |
|
68 | m_nameTable = m_options.NameTable ?? new NameTable(); | |
68 |
|
69 | |||
69 | m_jsonRootName = m_nameTable.Add(string.IsNullOrEmpty(m_options.RootName) ? "data" : m_options.RootName); |
|
70 | m_jsonRootName = m_nameTable.Add(string.IsNullOrEmpty(m_options.RootName) ? "data" : m_options.RootName); | |
70 | m_jsonArrayItemName = m_nameTable.Add(string.IsNullOrEmpty(m_options.ArrayItemName) ? "item" : m_options.ArrayItemName); |
|
71 | m_jsonArrayItemName = m_nameTable.Add(string.IsNullOrEmpty(m_options.ArrayItemName) ? "item" : m_options.ArrayItemName); | |
71 | m_jsonNamespace = m_nameTable.Add(m_options.NamespaceUri ?? string.Empty); |
|
72 | m_jsonNamespace = m_nameTable.Add(m_options.NamespaceUri ?? string.Empty); | |
72 | m_jsonPrefix = m_nameTable.Add(m_options.NodesPrefix ?? string.Empty); |
|
73 | m_jsonPrefix = m_nameTable.Add(m_options.NodesPrefix ?? string.Empty); | |
73 | m_xmlnsPrefix = m_nameTable.Add(XmlNameContext.XmlnsPrefix); |
|
74 | m_xmlnsPrefix = m_nameTable.Add(XmlNameContext.XmlnsPrefix); | |
74 | m_xmlnsNamespace = m_nameTable.Add(XmlNameContext.XmlnsNamespace); |
|
75 | m_xmlnsNamespace = m_nameTable.Add(XmlNameContext.XmlnsNamespace); | |
75 | m_xsiPrefix = m_nameTable.Add(XmlNameContext.XsiPrefix); |
|
76 | m_xsiPrefix = m_nameTable.Add(XmlNameContext.XsiPrefix); | |
76 | m_xsiNamespace = m_nameTable.Add(XmlNameContext.XsiNamespace); |
|
77 | m_xsiNamespace = m_nameTable.Add(XmlNameContext.XsiNamespace); | |
77 |
|
78 | |||
78 | // TODO validate m_jsonRootName, m_jsonArrayItemName |
|
79 | // TODO validate m_jsonRootName, m_jsonArrayItemName | |
79 |
|
80 | |||
80 | m_context = new XmlNameContext(null, 0); |
|
81 | m_context = new XmlNameContext(null, 0); | |
81 | } |
|
82 | } | |
82 |
|
83 | |||
83 | public override int AttributeCount { |
|
84 | public override int AttributeCount { | |
84 | get { |
|
85 | get { | |
85 | return m_attributes == null ? 0 : m_attributes.Length; |
|
86 | return m_attributes == null ? 0 : m_attributes.Length; | |
86 | } |
|
87 | } | |
87 | } |
|
88 | } | |
88 |
|
89 | |||
89 | public override string BaseURI { |
|
90 | public override string BaseURI { | |
90 | get { |
|
91 | get { | |
91 | return string.Empty; |
|
92 | return string.Empty; | |
92 | } |
|
93 | } | |
93 | } |
|
94 | } | |
94 |
|
95 | |||
95 | public override int Depth { |
|
96 | public override int Depth { | |
96 | get { |
|
97 | get { | |
97 | return m_xmlDepth; |
|
98 | return m_xmlDepth; | |
98 | } |
|
99 | } | |
99 | } |
|
100 | } | |
100 |
|
101 | |||
101 | public override bool EOF { |
|
102 | public override bool EOF { | |
102 | get { |
|
103 | get { | |
103 | return m_position == JsonXmlReaderPosition.Eof; |
|
104 | return m_position == JsonXmlReaderPosition.Eof; | |
104 | } |
|
105 | } | |
105 | } |
|
106 | } | |
106 |
|
107 | |||
107 | public override bool IsEmptyElement { |
|
108 | public override bool IsEmptyElement { | |
108 | get { return m_isEmpty; } |
|
109 | get { return m_isEmpty; } | |
109 | } |
|
110 | } | |
110 |
|
111 | |||
111 |
|
112 | |||
112 | public override string LocalName { |
|
113 | public override string LocalName { | |
113 | get { |
|
114 | get { | |
114 | return m_qName.Name; |
|
115 | return m_qName.Name; | |
115 | } |
|
116 | } | |
116 | } |
|
117 | } | |
117 |
|
118 | |||
118 | public override string NamespaceURI { |
|
119 | public override string NamespaceURI { | |
119 | get { |
|
120 | get { | |
120 | return m_qName.Namespace; |
|
121 | return m_qName.Namespace; | |
121 | } |
|
122 | } | |
122 | } |
|
123 | } | |
123 |
|
124 | |||
124 | public override XmlNameTable NameTable { |
|
125 | public override XmlNameTable NameTable { | |
125 | get { |
|
126 | get { | |
126 | return m_nameTable; |
|
127 | return m_nameTable; | |
127 | } |
|
128 | } | |
128 | } |
|
129 | } | |
129 |
|
130 | |||
130 | public override XmlNodeType NodeType { |
|
131 | public override XmlNodeType NodeType { | |
131 | get { |
|
132 | get { | |
132 | return m_nodeType; |
|
133 | return m_nodeType; | |
133 | } |
|
134 | } | |
134 | } |
|
135 | } | |
135 |
|
136 | |||
136 | public override string Prefix { |
|
137 | public override string Prefix { | |
137 | get { |
|
138 | get { | |
138 | return m_prefix; |
|
139 | return m_prefix; | |
139 | } |
|
140 | } | |
140 | } |
|
141 | } | |
141 |
|
142 | |||
142 | public override ReadState ReadState { |
|
143 | public override ReadState ReadState { | |
143 | get { |
|
144 | get { | |
144 | switch (m_position) { |
|
145 | switch (m_position) { | |
145 | case JsonXmlReaderPosition.Initial: |
|
146 | case JsonXmlReaderPosition.Initial: | |
146 | return ReadState.Initial; |
|
147 | return ReadState.Initial; | |
147 | case JsonXmlReaderPosition.Eof: |
|
148 | case JsonXmlReaderPosition.Eof: | |
148 | return ReadState.EndOfFile; |
|
149 | return ReadState.EndOfFile; | |
149 | case JsonXmlReaderPosition.Closed: |
|
150 | case JsonXmlReaderPosition.Closed: | |
150 | return ReadState.Closed; |
|
151 | return ReadState.Closed; | |
151 | case JsonXmlReaderPosition.Error: |
|
152 | case JsonXmlReaderPosition.Error: | |
152 | return ReadState.Error; |
|
153 | return ReadState.Error; | |
153 | default: |
|
154 | default: | |
154 | return ReadState.Interactive; |
|
155 | return ReadState.Interactive; | |
155 | }; |
|
156 | }; | |
156 | } |
|
157 | } | |
157 | } |
|
158 | } | |
158 |
|
159 | |||
159 | public override string Value { |
|
160 | public override string Value { | |
160 | get { |
|
161 | get { | |
161 | return m_value; |
|
162 | return m_value; | |
162 | } |
|
163 | } | |
163 | } |
|
164 | } | |
164 |
|
165 | |||
165 | public override string GetAttribute(int i) { |
|
166 | public override string GetAttribute(int i) { | |
166 | Safe.ArgumentInRange(i >= 0 && i < AttributeCount, nameof(i)); |
|
167 | Safe.ArgumentInRange(i >= 0 && i < AttributeCount, nameof(i)); | |
167 | return m_attributes[i].Value; |
|
168 | return m_attributes[i].Value; | |
168 | } |
|
169 | } | |
169 |
|
170 | |||
170 | public override string GetAttribute(string name) { |
|
171 | public override string GetAttribute(string name) { | |
171 | if (m_attributes == null) |
|
172 | if (m_attributes == null) | |
172 | return null; |
|
173 | return null; | |
173 | var qName = m_context.Resolve(name); |
|
174 | var qName = m_context.Resolve(name); | |
174 | var attr = Array.Find(m_attributes, x => x.QName == qName); |
|
175 | var attr = Array.Find(m_attributes, x => x.QName == qName); | |
175 | var value = attr?.Value; |
|
176 | var value = attr?.Value; | |
176 | return value == string.Empty ? null : value; |
|
177 | return value == string.Empty ? null : value; | |
177 | } |
|
178 | } | |
178 |
|
179 | |||
179 | public override string GetAttribute(string name, string namespaceURI) { |
|
180 | public override string GetAttribute(string name, string namespaceURI) { | |
180 | if (m_attributes == null) |
|
181 | if (m_attributes == null) | |
181 | return null; |
|
182 | return null; | |
182 | var qName = new XmlQualifiedName(name, namespaceURI); |
|
183 | var qName = new XmlQualifiedName(name, namespaceURI); | |
183 | var attr = Array.Find(m_attributes, x => x.QName == qName); |
|
184 | var attr = Array.Find(m_attributes, x => x.QName == qName); | |
184 | var value = attr?.Value; |
|
185 | var value = attr?.Value; | |
185 | return value == string.Empty ? null : value; |
|
186 | return value == string.Empty ? null : value; | |
186 | } |
|
187 | } | |
187 |
|
188 | |||
188 | public override string LookupNamespace(string prefix) { |
|
189 | public override string LookupNamespace(string prefix) { | |
189 | return m_context.ResolvePrefix(prefix); |
|
190 | return m_context.ResolvePrefix(prefix); | |
190 | } |
|
191 | } | |
191 |
|
192 | |||
192 | public override bool MoveToAttribute(string name) { |
|
193 | public override bool MoveToAttribute(string name) { | |
193 | if (m_attributes == null || m_attributes.Length == 0) |
|
194 | if (m_attributes == null || m_attributes.Length == 0) | |
194 | return false; |
|
195 | return false; | |
195 |
|
196 | |||
196 | var qName = m_context.Resolve(name); |
|
197 | var qName = m_context.Resolve(name); | |
197 | var index = Array.FindIndex(m_attributes, x => x.QName == qName); |
|
198 | var index = Array.FindIndex(m_attributes, x => x.QName == qName); | |
198 | if (index >= 0) { |
|
199 | if (index >= 0) { | |
199 | MoveToAttributeImpl(index); |
|
200 | MoveToAttributeImpl(index); | |
200 | return true; |
|
201 | return true; | |
201 | } |
|
202 | } | |
202 | return false; |
|
203 | return false; | |
203 | } |
|
204 | } | |
204 |
|
205 | |||
205 | public override bool MoveToAttribute(string name, string ns) { |
|
206 | public override bool MoveToAttribute(string name, string ns) { | |
206 | if (m_attributes == null || m_attributes.Length == 0) |
|
207 | if (m_attributes == null || m_attributes.Length == 0) | |
207 | return false; |
|
208 | return false; | |
208 |
|
209 | |||
209 | var qName = m_context.Resolve(name); |
|
210 | var qName = m_context.Resolve(name); | |
210 | var index = Array.FindIndex(m_attributes, x => x.QName == qName); |
|
211 | var index = Array.FindIndex(m_attributes, x => x.QName == qName); | |
211 | if (index >= 0) { |
|
212 | if (index >= 0) { | |
212 | MoveToAttributeImpl(index); |
|
213 | MoveToAttributeImpl(index); | |
213 | return true; |
|
214 | return true; | |
214 | } |
|
215 | } | |
215 | return false; |
|
216 | return false; | |
216 | } |
|
217 | } | |
217 |
|
218 | |||
218 | void MoveToAttributeImpl(int i) { |
|
219 | void MoveToAttributeImpl(int i) { | |
219 | if (!m_isAttribute) { |
|
220 | if (!m_isAttribute) { | |
220 | m_elementQName = m_qName; |
|
221 | m_elementQName = m_qName; | |
221 | m_elementDepth = m_xmlDepth; |
|
222 | m_elementDepth = m_xmlDepth; | |
222 | m_elementPrefix = m_prefix; |
|
223 | m_elementPrefix = m_prefix; | |
223 | m_elementIsEmpty = m_isEmpty; |
|
224 | m_elementIsEmpty = m_isEmpty; | |
224 | m_isAttribute = true; |
|
225 | m_isAttribute = true; | |
225 | } |
|
226 | } | |
226 |
|
227 | |||
227 | var attr = m_attributes[i]; |
|
228 | var attr = m_attributes[i]; | |
228 |
|
229 | |||
229 |
|
230 | |||
230 | m_currentAttribute = i; |
|
231 | m_currentAttribute = i; | |
231 | m_currentAttributeRead = false; |
|
232 | m_currentAttributeRead = false; | |
232 | m_nodeType = XmlNodeType.Attribute; |
|
233 | m_nodeType = XmlNodeType.Attribute; | |
233 |
|
234 | |||
234 | m_xmlDepth = m_elementDepth + 1; |
|
235 | m_xmlDepth = m_elementDepth + 1; | |
235 | m_qName = attr.QName; |
|
236 | m_qName = attr.QName; | |
236 | m_value = attr.Value; |
|
237 | m_value = attr.Value; | |
237 | m_prefix = attr.Prefix; |
|
238 | m_prefix = attr.Prefix; | |
238 | } |
|
239 | } | |
239 |
|
240 | |||
240 | public override bool MoveToElement() { |
|
241 | public override bool MoveToElement() { | |
241 | if (m_isAttribute) { |
|
242 | if (m_isAttribute) { | |
242 | m_value = null; |
|
243 | m_value = null; | |
243 | m_nodeType = XmlNodeType.Element; |
|
244 | m_nodeType = XmlNodeType.Element; | |
244 | m_xmlDepth = m_elementDepth; |
|
245 | m_xmlDepth = m_elementDepth; | |
245 | m_prefix = m_elementPrefix; |
|
246 | m_prefix = m_elementPrefix; | |
246 | m_qName = m_elementQName; |
|
247 | m_qName = m_elementQName; | |
247 | m_isEmpty = m_elementIsEmpty; |
|
248 | m_isEmpty = m_elementIsEmpty; | |
248 | m_isAttribute = false; |
|
249 | m_isAttribute = false; | |
249 | return true; |
|
250 | return true; | |
250 | } |
|
251 | } | |
251 | return false; |
|
252 | return false; | |
252 | } |
|
253 | } | |
253 |
|
254 | |||
254 | public override bool MoveToFirstAttribute() { |
|
255 | public override bool MoveToFirstAttribute() { | |
255 | if (m_attributes != null && m_attributes.Length > 0) { |
|
256 | if (m_attributes != null && m_attributes.Length > 0) { | |
256 | MoveToAttributeImpl(0); |
|
257 | MoveToAttributeImpl(0); | |
257 | return true; |
|
258 | return true; | |
258 | } |
|
259 | } | |
259 | return false; |
|
260 | return false; | |
260 | } |
|
261 | } | |
261 |
|
262 | |||
262 | public override bool MoveToNextAttribute() { |
|
263 | public override bool MoveToNextAttribute() { | |
263 | if (m_isAttribute) { |
|
264 | if (m_isAttribute) { | |
264 | var next = m_currentAttribute + 1; |
|
265 | var next = m_currentAttribute + 1; | |
265 | if (next < AttributeCount) { |
|
266 | if (next < AttributeCount) { | |
266 | MoveToAttributeImpl(next); |
|
267 | MoveToAttributeImpl(next); | |
267 | return true; |
|
268 | return true; | |
268 | } |
|
269 | } | |
269 | return false; |
|
270 | return false; | |
270 | } else { |
|
271 | } else { | |
271 | return MoveToFirstAttribute(); |
|
272 | return MoveToFirstAttribute(); | |
272 | } |
|
273 | } | |
273 |
|
274 | |||
274 | } |
|
275 | } | |
275 |
|
276 | |||
276 | public override bool ReadAttributeValue() { |
|
277 | public override bool ReadAttributeValue() { | |
277 | if (!m_isAttribute || m_currentAttributeRead) |
|
278 | if (!m_isAttribute || m_currentAttributeRead) | |
278 | return false; |
|
279 | return false; | |
279 |
|
280 | |||
280 | ValueNode(m_attributes[m_currentAttribute].Value); |
|
281 | ValueNode(m_attributes[m_currentAttribute].Value); | |
281 | m_currentAttributeRead = true; |
|
282 | m_currentAttributeRead = true; | |
282 | return true; |
|
283 | return true; | |
283 | } |
|
284 | } | |
284 |
|
285 | |||
285 | public override void ResolveEntity() { |
|
286 | public override void ResolveEntity() { | |
286 | /* do nothing */ |
|
287 | /* do nothing */ | |
287 | } |
|
288 | } | |
288 |
|
289 | |||
289 | /// <summary> |
|
290 | /// <summary> | |
290 | /// Determines do we need to increase depth after the current node |
|
291 | /// Determines do we need to increase depth after the current node | |
291 | /// </summary> |
|
292 | /// </summary> | |
292 | /// <returns></returns> |
|
293 | /// <returns></returns> | |
293 | public bool IsSibling() { |
|
294 | public bool IsSibling() { | |
294 | switch (m_nodeType) { |
|
295 | switch (m_nodeType) { | |
295 | case XmlNodeType.None: // start document |
|
296 | case XmlNodeType.None: // start document | |
296 | case XmlNodeType.Attribute: // after attribute only it's content can be iterated with ReadAttributeValue method |
|
297 | case XmlNodeType.Attribute: // after attribute only it's content can be iterated with ReadAttributeValue method | |
297 | return false; |
|
298 | return false; | |
298 | case XmlNodeType.Element: |
|
299 | case XmlNodeType.Element: | |
299 | // if the elemnt is empty the next element will be it's sibling |
|
300 | // if the elemnt is empty the next element will be it's sibling | |
300 | return m_isEmpty; |
|
301 | return m_isEmpty; | |
301 | default: |
|
302 | default: | |
302 | return true; |
|
303 | return true; | |
303 | } |
|
304 | } | |
304 | } |
|
305 | } | |
305 |
|
306 | |||
306 | void ValueNode(string value) { |
|
307 | void ValueNode(string value) { | |
307 | if (!IsSibling()) // the node is nested |
|
308 | if (!IsSibling()) // the node is nested | |
308 | m_xmlDepth++; |
|
309 | m_xmlDepth++; | |
309 |
|
310 | |||
310 | m_qName = XmlQualifiedName.Empty; |
|
311 | m_qName = XmlQualifiedName.Empty; | |
311 | m_nodeType = XmlNodeType.Text; |
|
312 | m_nodeType = XmlNodeType.Text; | |
312 | m_prefix = string.Empty; |
|
313 | m_prefix = string.Empty; | |
313 | m_value = value; |
|
314 | m_value = value; | |
314 | m_isEmpty = false; |
|
315 | m_isEmpty = false; | |
315 | m_attributes = null; |
|
316 | m_attributes = null; | |
316 | } |
|
317 | } | |
317 |
|
318 | |||
318 | void ElementNode(string name, string ns, XmlSimpleAttribute[] attrs, bool empty) { |
|
319 | void ElementNode(string name, string ns, XmlSimpleAttribute[] attrs, bool empty) { | |
319 | if (!IsSibling()) // the node is nested |
|
320 | if (!IsSibling()) // the node is nested | |
320 | m_xmlDepth++; |
|
321 | m_xmlDepth++; | |
321 |
|
322 | |||
322 | var context = m_context; |
|
323 | var context = m_context; | |
323 | List<XmlSimpleAttribute> definedAttrs = null; |
|
324 | List<XmlSimpleAttribute> definedAttrs = null; | |
324 |
|
325 | |||
325 | // define new namespaces |
|
326 | // define new namespaces | |
326 | if (attrs != null) { |
|
327 | if (attrs != null) { | |
327 | foreach (var attr in attrs) { |
|
328 | foreach (var attr in attrs) { | |
328 | if (attr.QName.Name == "xmlns") { |
|
329 | if (attr.QName.Name == "xmlns") { | |
329 | if (context == m_context) |
|
330 | if (context == m_context) | |
330 | context = new XmlNameContext(m_context, m_xmlDepth); |
|
331 | context = new XmlNameContext(m_context, m_xmlDepth); | |
331 | context.DefinePrefix(attr.Value, string.Empty); |
|
332 | context.DefinePrefix(attr.Value, string.Empty); | |
332 | } else if (attr.Prefix == m_xmlnsPrefix) { |
|
333 | } else if (attr.Prefix == m_xmlnsPrefix) { | |
333 | if (context == m_context) |
|
334 | if (context == m_context) | |
334 | context = new XmlNameContext(m_context, m_xmlDepth); |
|
335 | context = new XmlNameContext(m_context, m_xmlDepth); | |
335 | context.DefinePrefix(attr.Value, attr.QName.Name); |
|
336 | context.DefinePrefix(attr.Value, attr.QName.Name); | |
336 | } else { |
|
337 | } else { | |
337 | string attrPrefix; |
|
338 | string attrPrefix; | |
338 | if (string.IsNullOrEmpty(attr.QName.Namespace)) |
|
339 | if (string.IsNullOrEmpty(attr.QName.Namespace)) | |
339 | continue; |
|
340 | continue; | |
340 |
|
341 | |||
341 | // auto-define prefixes |
|
342 | // auto-define prefixes | |
342 | if (!context.LookupNamespacePrefix(attr.QName.Namespace, out attrPrefix) || string.IsNullOrEmpty(attrPrefix)) { |
|
343 | if (!context.LookupNamespacePrefix(attr.QName.Namespace, out attrPrefix) || string.IsNullOrEmpty(attrPrefix)) { | |
343 | // new namespace prefix added |
|
344 | // new namespace prefix added | |
344 | attrPrefix = context.CreateNamespacePrefix(attr.QName.Namespace); |
|
345 | attrPrefix = context.CreateNamespacePrefix(attr.QName.Namespace); | |
345 | attr.Prefix = attrPrefix; |
|
346 | attr.Prefix = attrPrefix; | |
346 |
|
347 | |||
347 | if (definedAttrs == null) |
|
348 | if (definedAttrs == null) | |
348 | definedAttrs = new List<XmlSimpleAttribute>(); |
|
349 | definedAttrs = new List<XmlSimpleAttribute>(); | |
349 |
|
350 | |||
350 | definedAttrs.Add(new XmlSimpleAttribute(attrPrefix, m_xmlnsNamespace, m_xmlnsPrefix, attr.QName.Namespace)); |
|
351 | definedAttrs.Add(new XmlSimpleAttribute(attrPrefix, m_xmlnsNamespace, m_xmlnsPrefix, attr.QName.Namespace)); | |
351 | } |
|
352 | } | |
352 | } |
|
353 | } | |
353 | } |
|
354 | } | |
354 | } |
|
355 | } | |
355 |
|
356 | |||
356 | string p; |
|
357 | string p; | |
357 | // auto-define prefixes |
|
358 | // auto-define prefixes | |
358 | if (!context.LookupNamespacePrefix(ns, out p)) { |
|
359 | if (!context.LookupNamespacePrefix(ns, out p)) { | |
359 | if (context == m_context) |
|
360 | if (context == m_context) | |
360 | context = new XmlNameContext(m_context, m_xmlDepth); |
|
361 | context = new XmlNameContext(m_context, m_xmlDepth); | |
361 | p = context.CreateNamespacePrefix(ns); |
|
362 | p = context.CreateNamespacePrefix(ns); | |
362 | if (definedAttrs == null) |
|
363 | if (definedAttrs == null) | |
363 | definedAttrs = new List<XmlSimpleAttribute>(); |
|
364 | definedAttrs = new List<XmlSimpleAttribute>(); | |
364 |
|
365 | |||
365 | definedAttrs.Add(new XmlSimpleAttribute(p, m_xmlnsNamespace, m_xmlnsPrefix, ns)); |
|
366 | definedAttrs.Add(new XmlSimpleAttribute(p, m_xmlnsNamespace, m_xmlnsPrefix, ns)); | |
366 | } |
|
367 | } | |
367 |
|
368 | |||
368 | if (definedAttrs != null) { |
|
369 | if (definedAttrs != null) { | |
369 | if (attrs != null) |
|
370 | if (attrs != null) | |
370 | definedAttrs.AddRange(attrs); |
|
371 | definedAttrs.AddRange(attrs); | |
371 | attrs = definedAttrs.ToArray(); |
|
372 | attrs = definedAttrs.ToArray(); | |
372 | } |
|
373 | } | |
373 |
|
374 | |||
374 | if (!empty) |
|
375 | if (!empty) | |
375 | m_context = context; |
|
376 | m_context = context; | |
376 |
|
377 | |||
377 | m_nodeType = XmlNodeType.Element; |
|
378 | m_nodeType = XmlNodeType.Element; | |
378 | m_qName = new XmlQualifiedName(name, ns); |
|
379 | m_qName = new XmlQualifiedName(name, ns); | |
379 | m_prefix = p; |
|
380 | m_prefix = p; | |
380 | m_value = null; |
|
381 | m_value = null; | |
381 | m_isEmpty = empty; |
|
382 | m_isEmpty = empty; | |
382 | m_attributes = attrs; |
|
383 | m_attributes = attrs; | |
383 | } |
|
384 | } | |
384 |
|
385 | |||
385 | void EndElementNode(string name, string ns) { |
|
386 | void EndElementNode(string name, string ns) { | |
386 | if (IsSibling()) { |
|
387 | if (IsSibling()) { | |
387 | // closing the element which has children |
|
388 | // closing the element which has children | |
388 | m_xmlDepth--; |
|
389 | m_xmlDepth--; | |
389 | } |
|
390 | } | |
390 |
|
391 | |||
391 | string p; |
|
392 | string p; | |
392 | if (!m_context.LookupNamespacePrefix(ns, out p)) |
|
393 | if (!m_context.LookupNamespacePrefix(ns, out p)) | |
393 | throw new Exception($"Failed to lookup namespace '{ns}'"); |
|
394 | throw new Exception($"Failed to lookup namespace '{ns}'"); | |
394 |
|
395 | |||
395 | if (m_context.Depth == m_xmlDepth) |
|
396 | if (m_context.Depth == m_xmlDepth) | |
396 | m_context = m_context.ParentContext; |
|
397 | m_context = m_context.ParentContext; | |
397 |
|
398 | |||
398 | m_nodeType = XmlNodeType.EndElement; |
|
399 | m_nodeType = XmlNodeType.EndElement; | |
399 | m_prefix = p; |
|
400 | m_prefix = p; | |
400 | m_qName = new XmlQualifiedName(name, ns); |
|
401 | m_qName = new XmlQualifiedName(name, ns); | |
401 | m_value = null; |
|
402 | m_value = null; | |
402 | m_attributes = null; |
|
403 | m_attributes = null; | |
403 | m_isEmpty = false; |
|
404 | m_isEmpty = false; | |
404 | } |
|
405 | } | |
405 |
|
406 | |||
406 | void XmlDeclaration() { |
|
407 | void XmlDeclaration() { | |
407 | if (!IsSibling()) // the node is nested |
|
408 | if (!IsSibling()) // the node is nested | |
408 | m_xmlDepth++; |
|
409 | m_xmlDepth++; | |
409 | m_nodeType = XmlNodeType.XmlDeclaration; |
|
410 | m_nodeType = XmlNodeType.XmlDeclaration; | |
410 | m_qName = new XmlQualifiedName("xml"); |
|
411 | m_qName = new XmlQualifiedName("xml"); | |
411 | m_value = "version='1.0'"; |
|
412 | m_value = "version='1.0'"; | |
412 | m_prefix = string.Empty; |
|
413 | m_prefix = string.Empty; | |
413 | m_attributes = null; |
|
414 | m_attributes = null; | |
414 | m_isEmpty = false; |
|
415 | m_isEmpty = false; | |
415 | } |
|
416 | } | |
416 |
|
417 | |||
417 | public override bool Read() { |
|
418 | public override bool Read() { | |
418 | try { |
|
419 | try { | |
419 | string elementName; |
|
420 | string elementName; | |
420 | XmlSimpleAttribute[] elementAttrs = null; |
|
421 | XmlSimpleAttribute[] elementAttrs = null; | |
421 | MoveToElement(); |
|
422 | MoveToElement(); | |
422 |
|
423 | |||
423 | switch (m_position) { |
|
424 | switch (m_position) { | |
424 | case JsonXmlReaderPosition.Initial: |
|
425 | case JsonXmlReaderPosition.Initial: | |
425 | m_jsonLocalName = m_jsonRootName; |
|
426 | m_jsonLocalName = m_jsonRootName; | |
426 | m_jsonSkip = false; |
|
427 | m_jsonSkip = false; | |
427 | XmlDeclaration(); |
|
428 | XmlDeclaration(); | |
428 | m_position = JsonXmlReaderPosition.Declaration; |
|
429 | m_position = JsonXmlReaderPosition.Declaration; | |
429 | return true; |
|
430 | return true; | |
430 | case JsonXmlReaderPosition.Declaration: |
|
431 | case JsonXmlReaderPosition.Declaration: | |
431 | elementAttrs = new[] { |
|
432 | elementAttrs = new[] { | |
432 | new XmlSimpleAttribute(m_xsiPrefix, m_xmlnsNamespace, m_xmlnsPrefix, m_xsiNamespace), |
|
433 | new XmlSimpleAttribute(m_xsiPrefix, m_xmlnsNamespace, m_xmlnsPrefix, m_xsiNamespace), | |
433 | string.IsNullOrEmpty(m_jsonPrefix) ? |
|
434 | string.IsNullOrEmpty(m_jsonPrefix) ? | |
434 | new XmlSimpleAttribute(m_xmlnsPrefix, string.Empty, string.Empty, m_jsonNamespace) : |
|
435 | new XmlSimpleAttribute(m_xmlnsPrefix, string.Empty, string.Empty, m_jsonNamespace) : | |
435 | new XmlSimpleAttribute(m_jsonPrefix, m_xmlnsNamespace, m_xmlnsPrefix, m_jsonNamespace) |
|
436 | new XmlSimpleAttribute(m_jsonPrefix, m_xmlnsNamespace, m_xmlnsPrefix, m_jsonNamespace) | |
436 | }; |
|
437 | }; | |
437 | break; |
|
438 | break; | |
438 | case JsonXmlReaderPosition.ValueElement: |
|
439 | case JsonXmlReaderPosition.ValueElement: | |
439 | if (!m_isEmpty) { |
|
440 | if (!m_isEmpty) { | |
440 | if (m_parser.ElementValue != null && !m_parser.ElementValue.Equals(string.Empty)) |
|
441 | if (m_parser.ElementValue != null && !m_parser.ElementValue.Equals(string.Empty)) | |
441 | ValueNode(m_parser.ElementValue); |
|
442 | ValueNode(m_parser.ElementValue); | |
442 | else |
|
443 | else | |
443 | goto case JsonXmlReaderPosition.ValueContent; |
|
444 | goto case JsonXmlReaderPosition.ValueContent; | |
444 | m_position = JsonXmlReaderPosition.ValueContent; |
|
445 | m_position = JsonXmlReaderPosition.ValueContent; | |
445 | return true; |
|
446 | return true; | |
446 | } else { |
|
447 | } else { | |
447 | m_position = JsonXmlReaderPosition.ValueEndElement; |
|
448 | m_position = JsonXmlReaderPosition.ValueEndElement; | |
448 | break; |
|
449 | break; | |
449 | } |
|
450 | } | |
450 | case JsonXmlReaderPosition.ValueContent: |
|
451 | case JsonXmlReaderPosition.ValueContent: | |
451 | EndElementNode(m_jsonValueName, m_jsonNamespace); |
|
452 | EndElementNode(m_jsonValueName, m_jsonNamespace); | |
452 | m_position = JsonXmlReaderPosition.ValueEndElement; |
|
453 | m_position = JsonXmlReaderPosition.ValueEndElement; | |
453 | return true; |
|
454 | return true; | |
454 | case JsonXmlReaderPosition.Eof: |
|
455 | case JsonXmlReaderPosition.Eof: | |
455 | case JsonXmlReaderPosition.Closed: |
|
456 | case JsonXmlReaderPosition.Closed: | |
456 | case JsonXmlReaderPosition.Error: |
|
457 | case JsonXmlReaderPosition.Error: | |
457 | return false; |
|
458 | return false; | |
458 | } |
|
459 | } | |
459 |
|
460 | |||
460 | while (m_parser.Read()) { |
|
461 | while (m_parser.Read()) { | |
461 | var jsonName = m_nameTable.Add(m_parser.ElementName); |
|
462 | var jsonName = m_nameTable.Add(m_parser.ElementName); | |
462 |
|
463 | |||
463 | switch (m_parser.ElementType) { |
|
464 | switch (m_parser.ElementType) { | |
464 | case JsonElementType.BeginObject: |
|
465 | case JsonElementType.BeginObject: | |
465 | if (!EnterJsonObject(jsonName, out elementName)) |
|
466 | if (!EnterJsonObject(jsonName, out elementName)) | |
466 | continue; |
|
467 | continue; | |
467 |
|
468 | |||
468 | m_position = JsonXmlReaderPosition.BeginObject; |
|
469 | m_position = JsonXmlReaderPosition.BeginObject; | |
469 | ElementNode(elementName, m_jsonNamespace, elementAttrs, false); |
|
470 | ElementNode(elementName, m_jsonNamespace, elementAttrs, false); | |
470 | break; |
|
471 | break; | |
471 | case JsonElementType.EndObject: |
|
472 | case JsonElementType.EndObject: | |
472 | if (!LeaveJsonScope(out elementName)) |
|
473 | if (!LeaveJsonScope(out elementName)) | |
473 | continue; |
|
474 | continue; | |
474 |
|
475 | |||
475 | m_position = JsonXmlReaderPosition.EndObject; |
|
476 | m_position = JsonXmlReaderPosition.EndObject; | |
476 | EndElementNode(elementName, m_jsonNamespace); |
|
477 | EndElementNode(elementName, m_jsonNamespace); | |
477 | break; |
|
478 | break; | |
478 | case JsonElementType.BeginArray: |
|
479 | case JsonElementType.BeginArray: | |
479 | if (!EnterJsonArray(jsonName, out elementName)) |
|
480 | if (!EnterJsonArray(jsonName, out elementName)) | |
480 | continue; |
|
481 | continue; | |
481 |
|
482 | |||
482 | m_position = JsonXmlReaderPosition.BeginArray; |
|
483 | m_position = JsonXmlReaderPosition.BeginArray; | |
483 | ElementNode(elementName, m_jsonNamespace, elementAttrs, false); |
|
484 | ElementNode(elementName, m_jsonNamespace, elementAttrs, false); | |
484 | break; |
|
485 | break; | |
485 | case JsonElementType.EndArray: |
|
486 | case JsonElementType.EndArray: | |
486 | if (!LeaveJsonScope(out elementName)) |
|
487 | if (!LeaveJsonScope(out elementName)) | |
487 | continue; |
|
488 | continue; | |
488 |
|
489 | |||
489 | m_position = JsonXmlReaderPosition.EndArray; |
|
490 | m_position = JsonXmlReaderPosition.EndArray; | |
490 | EndElementNode(elementName, m_jsonNamespace); |
|
491 | EndElementNode(elementName, m_jsonNamespace); | |
491 | break; |
|
492 | break; | |
492 | case JsonElementType.Value: |
|
493 | case JsonElementType.Value: | |
493 | if (!VisitJsonValue(jsonName, out m_jsonValueName)) |
|
494 | if (!VisitJsonValue(jsonName, out m_jsonValueName)) | |
494 | continue; |
|
495 | continue; | |
495 |
|
496 | |||
496 | m_position = JsonXmlReaderPosition.ValueElement; |
|
497 | m_position = JsonXmlReaderPosition.ValueElement; | |
497 | if (m_parser.ElementValue == null) |
|
498 | if (m_parser.ElementValue == null) | |
498 | // generate empty element with xsi:nil="true" attribute |
|
499 | // generate empty element with xsi:nil="true" attribute | |
499 | ElementNode( |
|
500 | ElementNode( | |
500 | m_jsonValueName, |
|
501 | m_jsonValueName, | |
501 | m_jsonNamespace, |
|
502 | m_jsonNamespace, | |
502 | new[] { |
|
503 | new[] { | |
503 | new XmlSimpleAttribute("nil", m_xsiNamespace, m_xsiPrefix, "true") |
|
504 | new XmlSimpleAttribute("nil", m_xsiNamespace, m_xsiPrefix, "true") | |
504 | }, |
|
505 | }, | |
505 | true |
|
506 | true | |
506 | ); |
|
507 | ); | |
507 | else |
|
508 | else | |
508 | ElementNode(m_jsonValueName, m_jsonNamespace, elementAttrs, m_parser.ElementValue.Equals(string.Empty)); |
|
509 | ElementNode(m_jsonValueName, m_jsonNamespace, elementAttrs, m_parser.ElementValue.Equals(string.Empty)); | |
509 | break; |
|
510 | break; | |
510 | default: |
|
511 | default: | |
511 | throw new Exception($"Unexpected JSON element {m_parser.ElementType}: {m_parser.ElementName}"); |
|
512 | throw new Exception($"Unexpected JSON element {m_parser.ElementType}: {m_parser.ElementName}"); | |
512 | } |
|
513 | } | |
513 | return true; |
|
514 | return true; | |
514 | } |
|
515 | } | |
515 |
|
516 | |||
516 | m_position = JsonXmlReaderPosition.Eof; |
|
517 | m_position = JsonXmlReaderPosition.Eof; | |
517 | return false; |
|
518 | return false; | |
518 | } catch { |
|
519 | } catch { | |
519 | m_position = JsonXmlReaderPosition.Error; |
|
520 | m_position = JsonXmlReaderPosition.Error; | |
520 | throw; |
|
521 | throw; | |
521 | } |
|
522 | } | |
522 | } |
|
523 | } | |
523 |
|
524 | |||
524 | void SaveJsonName() { |
|
525 | void SaveJsonName() { | |
525 | m_jsonNameStack.Push(new JsonContext { |
|
526 | m_jsonNameStack.Push(new JsonContext { | |
526 | skip = m_jsonSkip, |
|
527 | skip = m_jsonSkip, | |
527 | localName = m_jsonLocalName |
|
528 | localName = m_jsonLocalName | |
528 | }); |
|
529 | }); | |
529 |
|
530 | |||
530 | } |
|
531 | } | |
531 |
|
532 | |||
532 | bool EnterJsonObject(string name, out string elementName) { |
|
533 | bool EnterJsonObject(string name, out string elementName) { | |
533 | SaveJsonName(); |
|
534 | SaveJsonName(); | |
534 | m_jsonSkip = false; |
|
535 | m_jsonSkip = false; | |
535 |
|
536 | |||
536 | if (string.IsNullOrEmpty(name)) { |
|
537 | if (string.IsNullOrEmpty(name)) { | |
537 | if (m_jsonNameStack.Count != 1 && !m_jsonFlattenArrays) |
|
538 | if (m_jsonNameStack.Count != 1 && !m_jsonFlattenArrays) | |
538 | m_jsonLocalName = m_jsonArrayItemName; |
|
539 | m_jsonLocalName = m_jsonArrayItemName; | |
539 | } else { |
|
540 | } else { | |
540 | m_jsonLocalName = name; |
|
541 | m_jsonLocalName = name; | |
541 | } |
|
542 | } | |
542 |
|
543 | |||
543 | elementName = m_jsonLocalName; |
|
544 | elementName = m_jsonLocalName; | |
544 | return true; |
|
545 | return true; | |
545 | } |
|
546 | } | |
546 |
|
547 | |||
547 | /// <summary> |
|
548 | /// <summary> | |
548 | /// Called when JSON parser visits BeginArray ('[') element. |
|
549 | /// Called when JSON parser visits BeginArray ('[') element. | |
549 | /// </summary> |
|
550 | /// </summary> | |
550 | /// <param name="name">Optional property name if the array is the member of an object</param> |
|
551 | /// <param name="name">Optional property name if the array is the member of an object</param> | |
551 | /// <returns>true if element should be emited, false otherwise</returns> |
|
552 | /// <returns>true if element should be emited, false otherwise</returns> | |
552 | bool EnterJsonArray(string name, out string elementName) { |
|
553 | bool EnterJsonArray(string name, out string elementName) { | |
553 | SaveJsonName(); |
|
554 | SaveJsonName(); | |
554 |
|
555 | |||
555 | if (string.IsNullOrEmpty(name)) { |
|
556 | if (string.IsNullOrEmpty(name)) { | |
556 | // m_jsonNameStack.Count == 1 means the root node |
|
557 | // m_jsonNameStack.Count == 1 means the root node | |
557 | if (m_jsonNameStack.Count != 1 && !m_jsonFlattenArrays) |
|
558 | if (m_jsonNameStack.Count != 1 && !m_jsonFlattenArrays) | |
558 | m_jsonLocalName = m_jsonArrayItemName; |
|
559 | m_jsonLocalName = m_jsonArrayItemName; | |
559 |
|
560 | |||
560 | m_jsonSkip = false; // we should not flatten arrays inside arrays or in the document root |
|
561 | m_jsonSkip = false; // we should not flatten arrays inside arrays or in the document root | |
561 | } else { |
|
562 | } else { | |
562 | m_jsonLocalName = name; |
|
563 | m_jsonLocalName = name; | |
563 | m_jsonSkip = m_jsonFlattenArrays; |
|
564 | m_jsonSkip = m_jsonFlattenArrays; | |
564 | } |
|
565 | } | |
565 | elementName = m_jsonLocalName; |
|
566 | elementName = m_jsonLocalName; | |
566 |
|
567 | |||
567 | return !m_jsonSkip; |
|
568 | return !m_jsonSkip; | |
568 | } |
|
569 | } | |
569 |
|
570 | |||
570 | bool VisitJsonValue(string name, out string elementName) { |
|
571 | bool VisitJsonValue(string name, out string elementName) { | |
571 | if (string.IsNullOrEmpty(name)) { |
|
572 | if (string.IsNullOrEmpty(name)) { | |
572 | // m_jsonNameStack.Count == 0 means that JSON document consists from simple value |
|
573 | // m_jsonNameStack.Count == 0 means that JSON document consists from simple value | |
573 | elementName = (m_jsonNameStack.Count == 0 || m_jsonFlattenArrays) ? m_jsonLocalName : m_jsonArrayItemName; |
|
574 | elementName = (m_jsonNameStack.Count == 0 || m_jsonFlattenArrays) ? m_jsonLocalName : m_jsonArrayItemName; | |
574 | } else { |
|
575 | } else { | |
575 | elementName = name; |
|
576 | elementName = name; | |
576 | } |
|
577 | } | |
577 | return true; |
|
578 | return true; | |
578 | } |
|
579 | } | |
579 |
|
580 | |||
580 | bool LeaveJsonScope(out string elementName) { |
|
581 | bool LeaveJsonScope(out string elementName) { | |
581 | elementName = m_jsonLocalName; |
|
582 | elementName = m_jsonLocalName; | |
582 | var skip = m_jsonSkip; |
|
583 | var skip = m_jsonSkip; | |
583 |
|
584 | |||
584 | var prev = m_jsonNameStack.Pop(); |
|
585 | var prev = m_jsonNameStack.Pop(); | |
585 | m_jsonLocalName = prev.localName; |
|
586 | m_jsonLocalName = prev.localName; | |
586 | m_jsonSkip = prev.skip; |
|
587 | m_jsonSkip = prev.skip; | |
587 |
|
588 | |||
588 | return !skip; |
|
589 | return !skip; | |
589 | } |
|
590 | } | |
590 |
|
591 | |||
|
592 | protected override void Dispose(bool disposing) { | |||
|
593 | if (disposing) | |||
|
594 | Safe.Dispose(m_parser); | |||
|
595 | base.Dispose(true); | |||
|
596 | } | |||
|
597 | ||||
591 | public override string ToString() { |
|
598 | public override string ToString() { | |
592 | switch (NodeType) { |
|
599 | switch (NodeType) { | |
593 | case XmlNodeType.Element: |
|
600 | case XmlNodeType.Element: | |
594 | return $"<{Name} {string.Join(" ", (m_attributes ?? new XmlSimpleAttribute[0]).Select(x => $"{x.Prefix}{(string.IsNullOrEmpty(x.Prefix) ? "" : ":")}{x.QName.Name}='{x.Value}'"))} {(IsEmptyElement ? "/" : "")}>"; |
|
601 | return $"<{Name} {string.Join(" ", (m_attributes ?? new XmlSimpleAttribute[0]).Select(x => $"{x.Prefix}{(string.IsNullOrEmpty(x.Prefix) ? "" : ":")}{x.QName.Name}='{x.Value}'"))} {(IsEmptyElement ? "/" : "")}>"; | |
595 | case XmlNodeType.Attribute: |
|
602 | case XmlNodeType.Attribute: | |
596 | return $"@{Name}"; |
|
603 | return $"@{Name}"; | |
597 | case XmlNodeType.Text: |
|
604 | case XmlNodeType.Text: | |
598 | return $"{Value}"; |
|
605 | return $"{Value}"; | |
599 | case XmlNodeType.CDATA: |
|
606 | case XmlNodeType.CDATA: | |
600 | return $"<![CDATA[{Value}]]>"; |
|
607 | return $"<![CDATA[{Value}]]>"; | |
601 | case XmlNodeType.EntityReference: |
|
608 | case XmlNodeType.EntityReference: | |
602 | return $"&{Name};"; |
|
609 | return $"&{Name};"; | |
603 | case XmlNodeType.EndElement: |
|
610 | case XmlNodeType.EndElement: | |
604 | return $"</{Name}>"; |
|
611 | return $"</{Name}>"; | |
605 | default: |
|
612 | default: | |
606 | return $".{NodeType} {Name} {Value}"; |
|
613 | return $".{NodeType} {Name} {Value}"; | |
607 | } |
|
614 | } | |
608 | } |
|
615 | } | |
|
616 | ||||
|
617 | #region static methods | |||
|
618 | ||||
|
619 | public static JsonXmlReader CreateJsonXmlReader(TextReader textReader, JsonXmlReaderOptions options = null) { | |||
|
620 | var jsonReader = JsonReader.Create(textReader); | |||
|
621 | return new JsonXmlReader(jsonReader, options); | |||
|
622 | } | |||
|
623 | ||||
|
624 | public static JsonXmlReader CreateJsonXmlReader(Stream stream, JsonXmlReaderOptions options = null) { | |||
|
625 | var jsonReader = JsonReader.Create(stream); | |||
|
626 | return new JsonXmlReader(jsonReader, options); | |||
|
627 | } | |||
|
628 | ||||
|
629 | public static JsonXmlReader CreateJsonXmlReader(string file, JsonXmlReaderOptions options = null) { | |||
|
630 | var jsonReader = JsonReader.Create(file); | |||
|
631 | return new JsonXmlReader(jsonReader, options); | |||
|
632 | } | |||
|
633 | ||||
|
634 | #endregion | |||
609 | } |
|
635 | } | |
610 | } |
|
636 | } |
@@ -1,65 +1,96 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
3 | using System.IO; |
|
3 | using System.IO; | |
4 | using System.Linq; |
|
4 | using System.Linq; | |
|
5 | using System.Reflection; | |||
5 | using System.Text; |
|
6 | using System.Text; | |
6 | using System.Threading.Tasks; |
|
7 | using System.Threading.Tasks; | |
7 | using System.Xml; |
|
8 | using System.Xml; | |
8 | using System.Xml.Linq; |
|
9 | using System.Xml.Linq; | |
|
10 | using System.Xml.Serialization; | |||
9 |
|
11 | |||
10 | namespace Implab.Xml { |
|
12 | namespace Implab.Xml { | |
11 | public static class SerializationHelpers { |
|
13 | public static class SerializationHelpers { | |
12 | public static string SerializeAsString<T>(T obj) { |
|
14 | public static string SerializeAsString<T>(T obj) { | |
13 | return SerializersPool<T>.Instance.SerializeAsString(obj); |
|
15 | return SerializersPool<T>.Instance.SerializeAsString(obj); | |
14 | } |
|
16 | } | |
15 |
|
17 | |||
16 | public static void Serialize<T>(XmlWriter writer, T obj) { |
|
18 | public static void Serialize<T>(XmlWriter writer, T obj) { | |
17 | SerializersPool<T>.Instance.Serialize(writer, obj); |
|
19 | SerializersPool<T>.Instance.Serialize(writer, obj); | |
18 | } |
|
20 | } | |
19 |
|
21 | |||
20 | public static XmlDocument SerializeAsXmlDocument<T>(T obj) { |
|
22 | public static XmlDocument SerializeAsXmlDocument<T>(T obj) { | |
21 | var doc = new XmlDocument(); |
|
23 | var doc = new XmlDocument(); | |
22 | using (var writer = doc.CreateNavigator().AppendChild()) { |
|
24 | using (var writer = doc.CreateNavigator().AppendChild()) { | |
23 | SerializersPool<T>.Instance.Serialize(writer, obj); |
|
25 | SerializersPool<T>.Instance.Serialize(writer, obj); | |
24 | } |
|
26 | } | |
25 | return doc; |
|
27 | return doc; | |
26 | } |
|
28 | } | |
27 |
|
29 | |||
28 | public static XDocument SerializeAsXDocument<T>(T obj) { |
|
30 | public static XDocument SerializeAsXDocument<T>(T obj) { | |
29 | var doc = new XDocument(); |
|
31 | var doc = new XDocument(); | |
30 | using (var writer = doc.CreateWriter()) { |
|
32 | using (var writer = doc.CreateWriter()) { | |
31 | SerializersPool<T>.Instance.Serialize(writer, obj); |
|
33 | SerializersPool<T>.Instance.Serialize(writer, obj); | |
32 | } |
|
34 | } | |
33 | return doc; |
|
35 | return doc; | |
34 | } |
|
36 | } | |
35 |
|
37 | |||
36 | public static void SerializeToFile<T>(string file, T obj) { |
|
38 | public static void SerializeToFile<T>(string file, T obj) { | |
37 | using (var writer = File.CreateText(file)) |
|
39 | using (var writer = File.CreateText(file)) | |
38 | SerializersPool<T>.Instance.Serialize(writer, obj); |
|
40 | SerializersPool<T>.Instance.Serialize(writer, obj); | |
39 | } |
|
41 | } | |
40 |
|
42 | |||
41 | public static void SerializeToElementChild<T>(XmlElement element, T obj) { |
|
43 | public static void SerializeToElementChild<T>(XmlElement element, T obj) { | |
42 | using(var writer = element.CreateNavigator().AppendChild()) |
|
44 | using(var writer = element.CreateNavigator().AppendChild()) | |
43 | SerializersPool<T>.Instance.Serialize(writer, obj); |
|
45 | SerializersPool<T>.Instance.Serialize(writer, obj); | |
44 | } |
|
46 | } | |
45 |
|
47 | |||
46 | public static T Deserialize<T>(XmlReader reader) { |
|
48 | public static T Deserialize<T>(XmlReader reader) { | |
47 | return SerializersPool<T>.Instance.Deserialize(reader); |
|
49 | return SerializersPool<T>.Instance.Deserialize(reader); | |
48 | } |
|
50 | } | |
49 |
|
51 | |||
50 | public static T DeserializeFromFile<T>(string file) { |
|
52 | public static T DeserializeFromFile<T>(string file) { | |
51 | using(var reader = XmlReader.Create(File.OpenText(file))) |
|
53 | using(var reader = XmlReader.Create(File.OpenText(file))) | |
52 | return Deserialize<T>(reader); |
|
54 | return Deserialize<T>(reader); | |
53 | } |
|
55 | } | |
54 |
|
56 | |||
55 | public static T DeserializeFromString<T>(string data) { |
|
57 | public static T DeserializeFromString<T>(string data) { | |
56 | return SerializersPool<T>.Instance.DeserializeFromString(data); |
|
58 | return SerializersPool<T>.Instance.DeserializeFromString(data); | |
57 | } |
|
59 | } | |
58 |
|
60 | |||
59 | public static T DeserializeFromXmlNode<T>(XmlNode node) { |
|
61 | public static T DeserializeFromXmlNode<T>(XmlNode node) { | |
60 | Safe.ArgumentNotNull(node, nameof(node)); |
|
62 | Safe.ArgumentNotNull(node, nameof(node)); | |
61 | using (var reader = node.CreateNavigator().ReadSubtree()) |
|
63 | using (var reader = node.CreateNavigator().ReadSubtree()) | |
62 | return SerializersPool<T>.Instance.Deserialize(reader); |
|
64 | return SerializersPool<T>.Instance.Deserialize(reader); | |
63 | } |
|
65 | } | |
|
66 | ||||
|
67 | public static T DeserializeJson<T>(TextReader textReader) { | |||
|
68 | var options = new JsonXmlReaderOptions { | |||
|
69 | NamespaceUri = typeof(T).GetCustomAttribute<XmlRootAttribute>()?.Namespace, | |||
|
70 | RootName = typeof(T).Name, | |||
|
71 | FlattenArrays = true | |||
|
72 | }; | |||
|
73 | ||||
|
74 | using(var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options)) | |||
|
75 | return Deserialize<T>(reader); | |||
|
76 | } | |||
|
77 | ||||
|
78 | public static T DeserializeJsonFromString<T>(string data) { | |||
|
79 | using(var reader = new StringReader(data)) { | |||
|
80 | return DeserializeJson<T>(reader); | |||
64 | } |
|
81 | } | |
65 | } |
|
82 | } | |
|
83 | ||||
|
84 | public static void SerializeJson<T>(TextWriter writer, T obj) { | |||
|
85 | var doc = SerializeAsXmlDocument(obj); | |||
|
86 | XmlToJson.Default.Transform(doc, null, writer); | |||
|
87 | } | |||
|
88 | ||||
|
89 | public static string SerializeJsonAsString<T>(T obj) { | |||
|
90 | using(var writer = new StringWriter()) { | |||
|
91 | SerializeJson(writer, obj); | |||
|
92 | return writer.ToString(); | |||
|
93 | } | |||
|
94 | } | |||
|
95 | } | |||
|
96 | } |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now