comparison Implab.Format.Test/JsonTests.cs @ 229:5f7a3e1d32b9 v2

JsonXmlReader performance tuning JsonScanner now operates strings and doesn't parses number and literals. Added SerializationHelpers to common serialize/deserialize operations
author cin
date Tue, 12 Sep 2017 19:07:42 +0300
parents 6fa235c5a760
children 302ca905c19e
comparison
equal deleted inserted replaced
228:6fa235c5a760 229:5f7a3e1d32b9
3 using Implab.Automaton; 3 using Implab.Automaton;
4 using Implab.Xml; 4 using Implab.Xml;
5 using System.Xml; 5 using System.Xml;
6 using Implab.Formats; 6 using Implab.Formats;
7 using Implab.Formats.Json; 7 using Implab.Formats.Json;
8 using System.IO;
8 9
9 namespace Implab.Format.Test { 10 namespace Implab.Format.Test {
10 [TestFixture] 11 [TestFixture]
11 public class JsonTests { 12 public class JsonTests {
12 13
13 [Test] 14 [Test]
14 public void TestScannerValidTokens() { 15 public void TestScannerValidTokens() {
15 using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { 16 using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) {
16 17
17 Tuple<JsonTokenType, object>[] expexted = { 18 Tuple<JsonTokenType, object>[] expexted = {
18 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d), 19 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "9123"),
19 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), 20 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
20 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d), 21 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-123"),
21 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), 22 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
22 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d), 23 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0"),
23 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), 24 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
24 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d), 25 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0.1"),
25 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), 26 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
26 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d), 27 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.2"),
27 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), 28 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
28 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d), 29 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.1e3"),
29 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), 30 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
30 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d), 31 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "1.3E-3"),
31 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), 32 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
32 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"), 33 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"),
33 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), 34 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
34 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"), 35 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"),
35 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, null), 36 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, null),
37 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, null), 38 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, null),
38 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, null), 39 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, null),
39 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, null) 40 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, null)
40 }; 41 };
41 42
42 object value; 43 string value;
43 JsonTokenType tokenType; 44 JsonTokenType tokenType;
44 for (var i = 0; i < expexted.Length; i++) { 45 for (var i = 0; i < expexted.Length; i++) {
45 46
46 Assert.IsTrue(scanner.ReadToken(out value, out tokenType)); 47 Assert.IsTrue(scanner.ReadToken(out value, out tokenType));
47 Assert.AreEqual(expexted[i].Item1, tokenType); 48 Assert.AreEqual(expexted[i].Item1, tokenType);
71 }; 72 };
72 73
73 foreach (var json in bad) { 74 foreach (var json in bad) {
74 using (var scanner = JsonStringScanner.Create(json)) { 75 using (var scanner = JsonStringScanner.Create(json)) {
75 try { 76 try {
76 object value; 77 string value;
77 JsonTokenType token; 78 JsonTokenType token;
78 scanner.ReadToken(out value, out token); 79 scanner.ReadToken(out value, out token);
79 if (!Object.Equals(value, json)) { 80 if (!Object.Equals(value, json)) {
80 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value); 81 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value);
81 continue; 82 continue;
107 //DumpJsonParse("null"); 108 //DumpJsonParse("null");
108 //DumpJsonParse("true"); 109 //DumpJsonParse("true");
109 //DumpJsonParse("{}"); 110 //DumpJsonParse("{}");
110 //DumpJsonParse("[]"); 111 //DumpJsonParse("[]");
111 DumpJsonParse("{\"one\":1, \"two\":2}"); 112 DumpJsonParse("{\"one\":1, \"two\":2}");
112 DumpJsonParse("[1,2,3]"); 113 DumpJsonParse("[1,\"\",2,3]");
113 DumpJsonParse("[{\"info\": [7,8,9]}]"); 114 DumpJsonParse("[{\"info\": [7,8,9]}]");
114 DumpJsonFlatParse("[1,2,[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); 115 DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]");
115 } 116 }
116 117
117 void AssertRead(XmlReader reader, XmlNodeType expected) { 118 void AssertRead(XmlReader reader, XmlNodeType expected) {
118 Assert.IsTrue(reader.Read()); 119 Assert.IsTrue(reader.Read());
119 Console.WriteLine($"{new string(' ', reader.Depth*2)}{reader}"); 120 Console.WriteLine($"{new string(' ', reader.Depth*2)}{reader}");
120 Assert.AreEqual(expected, reader.NodeType); 121 Assert.AreEqual(expected, reader.NodeType);
121 } 122 }
122 123
123 void DumpJsonParse(string json) { 124 void DumpJsonParse(string json) {
124 Console.WriteLine($"JSON: {json}"); 125 Console.WriteLine($"JSON: {json}");
125 Console.WriteLine("XML"); 126 Console.WriteLine("XML");
126 using (var xmlReader = new JsonXmlReader(new JsonParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { 127 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) {
127 while (xmlReader.Read()) 128 while (xmlReader.Read())
128 Console.WriteLine($"{new string(' ', xmlReader.Depth * 2)}{xmlReader}"); 129 Console.WriteLine($"{new string(' ', xmlReader.Depth * 2)}{xmlReader}");
129 } 130 }
130 } 131 }
131 132
135 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { 136 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings {
136 Indent = true, 137 Indent = true,
137 CloseOutput = false, 138 CloseOutput = false,
138 ConformanceLevel = ConformanceLevel.Document 139 ConformanceLevel = ConformanceLevel.Document
139 })) 140 }))
140 using (var xmlReader = new JsonXmlReader(new JsonParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { 141 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) {
141 xmlWriter.WriteNode(xmlReader, false); 142 xmlWriter.WriteNode(xmlReader, false);
142 } 143 }
143 } 144 }
144 } 145 }
145 } 146 }