Mercurial > pub > ImplabNet
annotate Implab.Format.Test/JsonTests.cs @ 227:8d5de4eb9c2c v2
Reimplemented JsonXmlReader, added support for null values: JSON null values are
mapped to empty nodes with 'xsi:nil' attribute set to 'true'
author | cin |
---|---|
date | Sat, 09 Sep 2017 03:53:13 +0300 |
parents | Implab.Test/Implab.Format.Test/JsonTests.cs@d6a8cba73acc |
children | 6fa235c5a760 |
rev | line source |
---|---|
158 | 1 using NUnit.Framework; |
2 using System; | |
182 | 3 using Implab.Formats.JSON; |
183 | 4 using Implab.Automaton; |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
5 using Implab.Xml; |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
6 using System.Xml; |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
7 using System.Text; |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
8 |
158 | 9 namespace Implab.Format.Test { |
182 | 10 [TestFixture] |
158 | 11 public class JsonTests { |
182 | 12 [Test] |
13 public void TestScannerValidTokens() { | |
183 | 14 using (var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { |
15 | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
16 Tuple<JsonTokenType, object>[] expexted = { |
183 | 17 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d), |
18 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), | |
19 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d), | |
20 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), | |
21 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d), | |
22 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), | |
23 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d), | |
24 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), | |
25 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d), | |
26 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), | |
27 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d), | |
28 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), | |
29 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d), | |
30 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), | |
31 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"), | |
32 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), | |
33 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"), | |
34 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, " ["), | |
35 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, "]"), | |
36 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, "{"), | |
37 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, "}"), | |
38 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, ":") | |
39 }; | |
182 | 40 |
183 | 41 object value; |
42 JsonTokenType tokenType; | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
43 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
|
44 |
183 | 45 Assert.IsTrue(scanner.ReadToken(out value, out tokenType)); |
46 Assert.AreEqual(expexted[i].Item1, tokenType); | |
47 Assert.AreEqual(expexted[i].Item2, value); | |
48 } | |
49 | |
50 Assert.IsFalse(scanner.ReadToken(out value, out tokenType)); | |
51 } | |
52 } | |
53 | |
54 [Test] | |
55 public void TestScannerBadTokens() { | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
56 var bad = new[] { |
183 | 57 " 1", |
58 " literal", | |
59 " \"", | |
60 "\"unclosed string", | |
61 "1.bad", | |
62 "001", // should be read as three numbers | |
63 "--10", | |
64 "+10", | |
65 "1.0.0", | |
66 "1e1.0", | |
67 "l1teral0", | |
68 ".123", | |
69 "-.123" | |
182 | 70 }; |
71 | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
72 foreach (var json in bad) { |
183 | 73 using (var scanner = new JSONScanner(json)) { |
74 try { | |
75 object value; | |
76 JsonTokenType token; | |
77 scanner.ReadToken(out value, out token); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
78 if (!Object.Equals(value, json)) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
79 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value); |
183 | 80 continue; |
81 } | |
82 Assert.Fail("Token '{0}' shouldn't pass", json); | |
83 } catch (ParserException e) { | |
84 Console.WriteLine(e.Message); | |
85 } | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
86 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
87 } |
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 [Test] |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
91 public void JsonXmlReaderSimpleTest() { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
92 var json = "\"some text\""; |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
93 //Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
94 //Console.WriteLine("XML"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
95 /*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
|
96 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
|
97 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
98 AssertRead(xmlReader, XmlNodeType.XmlDeclaration); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
99 AssertRead(xmlReader, XmlNodeType.Element); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
100 AssertRead(xmlReader, XmlNodeType.Text); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
101 AssertRead(xmlReader, XmlNodeType.EndElement); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
102 Assert.IsFalse(xmlReader.Read()); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
103 }*/ |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
104 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
105 //DumpJsonParse("\"text value\""); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
106 //DumpJsonParse("null"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
107 //DumpJsonParse("true"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
108 //DumpJsonParse("{}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
109 //DumpJsonParse("[]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
110 DumpJsonParse("{\"one\":1, \"two\":2}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
111 DumpJsonParse("[1,2,3]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
112 DumpJsonParse("[{\"info\": [7,8,9]}]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
113 DumpJsonFlatParse("[1,2,[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
114 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
115 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
116 void AssertRead(XmlReader reader, XmlNodeType expected) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
117 Assert.IsTrue(reader.Read()); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
118 Console.WriteLine($"{new string(' ', reader.Depth*2)}{reader}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
119 Assert.AreEqual(expected, reader.NodeType); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
120 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
121 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
122 void DumpJsonParse(string json) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
123 Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
124 Console.WriteLine("XML"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
125 using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
126 while (xmlReader.Read()) |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
127 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
|
128 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
129 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
130 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
131 void DumpJsonFlatParse(string json) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
132 Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
133 Console.WriteLine("XML"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
134 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
|
135 Indent = true, |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
136 CloseOutput = false, |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
137 ConformanceLevel = ConformanceLevel.Document |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
138 })) |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
139 using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
140 xmlWriter.WriteNode(xmlReader, false); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
141 } |
158 | 142 } |
143 } | |
144 } | |
145 |