Mercurial > pub > ImplabNet
annotate Implab.Test/JsonTests.cs @ 265:74e048cbaac8 v3 v3.0.10
Restored lost JsonXmlCaseTransform from version 2.1
author | cin |
---|---|
date | Mon, 16 Apr 2018 19:43:18 +0300 |
parents | 3a6e18c432be |
children |
rev | line source |
---|---|
262 | 1 using Xunit; |
158 | 2 using System; |
183 | 3 using Implab.Automaton; |
262 | 4 using Implab.Xml; |
5 using System.Xml; | |
6 using Implab.Formats; | |
7 using Implab.Formats.Json; | |
8 using System.IO; | |
264 | 9 using Implab.Test.Model; |
262 | 10 |
11 namespace Implab.Test { | |
12 public class JsonTests { | |
13 | |
14 [Fact] | |
182 | 15 public void TestScannerValidTokens() { |
228 | 16 using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { |
183 | 17 |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
18 Tuple<JsonTokenType, object>[] expexted = { |
229 | 19 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "9123"), |
228 | 20 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 21 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-123"), |
228 | 22 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 23 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0"), |
228 | 24 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 25 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0.1"), |
228 | 26 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 27 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.2"), |
228 | 28 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 29 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.1e3"), |
228 | 30 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 31 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "1.3E-3"), |
228 | 32 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 33 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"), |
228 | 34 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 35 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"), |
228 | 36 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, null), |
37 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, null), | |
38 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, null), | |
39 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, null), | |
40 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, null) | |
183 | 41 }; |
182 | 42 |
229 | 43 string value; |
183 | 44 JsonTokenType tokenType; |
262 | 45 for (var i = 0; i < expexted.Length; i++) { |
46 | |
47 Assert.True(scanner.ReadToken(out value, out tokenType)); | |
48 Assert.Equal(expexted[i].Item1, tokenType); | |
49 Assert.Equal(expexted[i].Item2, value); | |
183 | 50 } |
51 | |
262 | 52 Assert.False(scanner.ReadToken(out value, out tokenType)); |
183 | 53 } |
54 } | |
55 | |
262 | 56 [Fact] |
183 | 57 public void TestScannerBadTokens() { |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
58 var bad = new[] { |
183 | 59 " 1", |
60 " literal", | |
61 " \"", | |
62 "\"unclosed string", | |
63 "1.bad", | |
64 "001", // should be read as three numbers | |
65 "--10", | |
66 "+10", | |
67 "1.0.0", | |
68 "1e1.0", | |
69 "l1teral0", | |
70 ".123", | |
71 "-.123" | |
182 | 72 }; |
73 | |
262 | 74 foreach (var json in bad) { |
228 | 75 using (var scanner = JsonStringScanner.Create(json)) { |
183 | 76 try { |
229 | 77 string value; |
183 | 78 JsonTokenType token; |
79 scanner.ReadToken(out value, out token); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
80 if (!Object.Equals(value, json)) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
81 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value); |
183 | 82 continue; |
83 } | |
262 | 84 Assert.True(false, $"Token '{json}' shouldn't pass"); |
183 | 85 } catch (ParserException e) { |
86 Console.WriteLine(e.Message); | |
87 } | |
262 | 88 } |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
89 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
90 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
91 |
262 | 92 [Fact] |
93 public void JsonXmlReaderSimpleTest() { | |
94 var json = "\"some text\""; | |
95 //Console.WriteLine($"JSON: {json}"); | |
96 //Console.WriteLine("XML"); | |
97 /*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) { | |
98 Assert.AreEqual(xmlReader.ReadState, System.Xml.ReadState.Initial); | |
99 | |
100 AssertRead(xmlReader, XmlNodeType.XmlDeclaration); | |
101 AssertRead(xmlReader, XmlNodeType.Element); | |
102 AssertRead(xmlReader, XmlNodeType.Text); | |
103 AssertRead(xmlReader, XmlNodeType.EndElement); | |
104 Assert.IsFalse(xmlReader.Read()); | |
105 }*/ | |
106 | |
107 DumpJsonParse("\"text value\""); | |
108 DumpJsonParse("null"); | |
109 DumpJsonParse("true"); | |
110 DumpJsonParse("{}"); | |
111 DumpJsonParse("[]"); | |
112 DumpJsonParse("{\"one\":1, \"two\":2}"); | |
113 DumpJsonParse("[1,\"\",2,3]"); | |
114 DumpJsonParse("[{\"info\": [7,8,9]}]"); | |
115 DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
116 } |
236 | 117 |
264 | 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 | |
262 | 156 void AssertRead(XmlReader reader, XmlNodeType expected) { |
157 Assert.True(reader.Read()); | |
158 Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}"); | |
159 Assert.Equal(expected, reader.NodeType); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
160 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
161 |
262 | 162 void DumpJsonParse(string json) { |
163 Console.WriteLine($"JSON: {json}"); | |
164 Console.WriteLine("XML"); | |
165 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { | |
166 Indent = true, | |
167 CloseOutput = false, | |
168 ConformanceLevel = ConformanceLevel.Document | |
169 })) | |
265 | 170 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "Data", NodesPrefix = "json", CaseTransform = JsonXmlCaseTransform.UcFirst, ArrayItemName = "Item" })) { |
262 | 171 xmlWriter.WriteNode(xmlReader, false); |
172 } | |
173 Console.WriteLine(); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
174 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
175 |
262 | 176 void DumpJsonFlatParse(string json) { |
177 Console.WriteLine($"JSON: {json}"); | |
178 Console.WriteLine("XML"); | |
179 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { | |
180 Indent = true, | |
181 CloseOutput = false, | |
182 ConformanceLevel = ConformanceLevel.Document | |
183 })) | |
184 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { | |
185 xmlWriter.WriteNode(xmlReader, false); | |
186 } | |
187 Console.WriteLine(); | |
158 | 188 } |
189 } | |
190 } | |
191 |