Mercurial > pub > ImplabNet
annotate Implab.Format.Test/JsonTests.cs @ 247:fb70574741a1 v3
working on promises
author | cin |
---|---|
date | Fri, 26 Jan 2018 18:46:27 +0300 |
parents | 302ca905c19e |
children |
rev | line source |
---|---|
158 | 1 using NUnit.Framework; |
2 using System; | |
183 | 3 using Implab.Automaton; |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
4 using Implab.Xml; |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
5 using System.Xml; |
228 | 6 using Implab.Formats; |
7 using Implab.Formats.Json; | |
229 | 8 using System.IO; |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
9 |
158 | 10 namespace Implab.Format.Test { |
182 | 11 [TestFixture] |
236 | 12 public class JsonTests { |
13 | |
182 | 14 [Test] |
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; |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
45 for (var i = 0; i < expexted.Length; i++) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
46 |
183 | 47 Assert.IsTrue(scanner.ReadToken(out value, out tokenType)); |
48 Assert.AreEqual(expexted[i].Item1, tokenType); | |
49 Assert.AreEqual(expexted[i].Item2, value); | |
50 } | |
51 | |
52 Assert.IsFalse(scanner.ReadToken(out value, out tokenType)); | |
53 } | |
54 } | |
55 | |
56 [Test] | |
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 | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
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 } | |
84 Assert.Fail("Token '{0}' shouldn't pass", json); | |
85 } catch (ParserException e) { | |
86 Console.WriteLine(e.Message); | |
87 } | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
88 } |
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 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
92 [Test] |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
93 public void JsonXmlReaderSimpleTest() { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
94 var json = "\"some text\""; |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
95 //Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
96 //Console.WriteLine("XML"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
97 /*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
98 Assert.AreEqual(xmlReader.ReadState, System.Xml.ReadState.Initial); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
99 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
100 AssertRead(xmlReader, XmlNodeType.XmlDeclaration); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
101 AssertRead(xmlReader, XmlNodeType.Element); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
102 AssertRead(xmlReader, XmlNodeType.Text); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
103 AssertRead(xmlReader, XmlNodeType.EndElement); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
104 Assert.IsFalse(xmlReader.Read()); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
105 }*/ |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
106 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
107 //DumpJsonParse("\"text value\""); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
108 //DumpJsonParse("null"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
109 //DumpJsonParse("true"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
110 //DumpJsonParse("{}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
111 //DumpJsonParse("[]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
112 DumpJsonParse("{\"one\":1, \"two\":2}"); |
229 | 113 DumpJsonParse("[1,\"\",2,3]"); |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
114 DumpJsonParse("[{\"info\": [7,8,9]}]"); |
229 | 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 |
118 [Test] | |
119 public void JsonBenchmark() { | |
120 var t = Environment.TickCount; | |
121 using (var reader = new JsonXmlReader(JsonReader.Create("e:\\citylots.json"), new JsonXmlReaderOptions { NamespaceUri = "XmlReaderSimpleTest", RootName = "data" })) { | |
122 while (reader.Read()) { | |
123 } | |
124 } | |
125 Console.WriteLine($"JsonXmlReader: {Environment.TickCount - t} ms"); | |
126 | |
127 t = Environment.TickCount; | |
128 using(var reader = JsonReader.Create("e:\\citylots.json")) { | |
129 while(reader.Read()) { | |
130 } | |
131 } | |
132 | |
133 Console.WriteLine($"JsonReader: {Environment.TickCount - t} ms"); | |
134 | |
135 t = Environment.TickCount; | |
136 using (var reader = XmlReader.Create("file:///e:\\citylots.xml")) { | |
137 while (reader.Read()) { | |
138 } | |
139 } | |
140 | |
141 Console.WriteLine($"XmlReader: {Environment.TickCount - t} ms"); | |
142 } | |
143 | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
144 void AssertRead(XmlReader reader, XmlNodeType expected) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
145 Assert.IsTrue(reader.Read()); |
236 | 146 Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}"); |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
147 Assert.AreEqual(expected, reader.NodeType); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
148 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
149 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
150 void DumpJsonParse(string json) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
151 Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
152 Console.WriteLine("XML"); |
229 | 153 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
154 while (xmlReader.Read()) |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
155 Console.WriteLine($"{new string(' ', xmlReader.Depth * 2)}{xmlReader}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
156 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
157 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
158 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
159 void DumpJsonFlatParse(string json) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
160 Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
161 Console.WriteLine("XML"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
162 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
163 Indent = true, |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
164 CloseOutput = false, |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
165 ConformanceLevel = ConformanceLevel.Document |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
166 })) |
229 | 167 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
168 xmlWriter.WriteNode(xmlReader, false); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
169 } |
158 | 170 } |
171 } | |
172 } | |
173 |