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