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 |
@@ -6,6 +6,7 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 { | |
@@ -114,6 +115,44 namespace Implab.Test { | |||||
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}"); |
@@ -8,6 +8,7 namespace Implab.Components { | |||||
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 |
@@ -19,4 +19,8 | |||||
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> |
@@ -2,6 +2,7 | |||||
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 | |||
@@ -588,6 +589,12 namespace Implab.Xml { | |||||
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: | |
@@ -606,5 +613,24 namespace Implab.Xml { | |||||
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 | } |
@@ -2,10 +2,12 | |||||
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 { | |
@@ -61,5 +63,34 namespace Implab.Xml { | |||||
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); | |||
|
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