##// END OF EJS Templates
working on promises
working on promises

File last commit:

r236:302ca905c19e v2
r243:b1e0ffdf3451 v3
Show More
JsonTests.cs
173 lines | 7.6 KiB | text/x-csharp | CSharpLexer
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 using NUnit.Framework;
using System;
using Implab.Automaton;
using Implab.Xml;
using System.Xml;
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 using Implab.Formats;
using Implab.Formats.Json;
cin
JsonXmlReader performance tuning...
r229 using System.IO;
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227
namespace Implab.Format.Test {
[TestFixture]
cin
JsonReader optimizations
r236 public class JsonTests {
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 [Test]
public void TestScannerValidTokens() {
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) {
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227
Tuple<JsonTokenType, object>[] expexted = {
cin
JsonXmlReader performance tuning...
r229 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "9123"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
cin
JsonXmlReader performance tuning...
r229 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-123"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
cin
JsonXmlReader performance tuning...
r229 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
cin
JsonXmlReader performance tuning...
r229 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0.1"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
cin
JsonXmlReader performance tuning...
r229 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.2"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
cin
JsonXmlReader performance tuning...
r229 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.1e3"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
cin
JsonXmlReader performance tuning...
r229 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "1.3E-3"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"),
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, null),
new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, null),
new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, null),
new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, null),
new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, null)
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 };
cin
JsonXmlReader performance tuning...
r229 string value;
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 JsonTokenType tokenType;
for (var i = 0; i < expexted.Length; i++) {
Assert.IsTrue(scanner.ReadToken(out value, out tokenType));
Assert.AreEqual(expexted[i].Item1, tokenType);
Assert.AreEqual(expexted[i].Item2, value);
}
Assert.IsFalse(scanner.ReadToken(out value, out tokenType));
}
}
[Test]
public void TestScannerBadTokens() {
var bad = new[] {
" 1",
" literal",
" \"",
"\"unclosed string",
"1.bad",
"001", // should be read as three numbers
"--10",
"+10",
"1.0.0",
"1e1.0",
"l1teral0",
".123",
"-.123"
};
foreach (var json in bad) {
cin
Rewritten JsonScanner, JsonParser, fixed naming style
r228 using (var scanner = JsonStringScanner.Create(json)) {
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 try {
cin
JsonXmlReader performance tuning...
r229 string value;
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 JsonTokenType token;
scanner.ReadToken(out value, out token);
if (!Object.Equals(value, json)) {
Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value);
continue;
}
Assert.Fail("Token '{0}' shouldn't pass", json);
} catch (ParserException e) {
Console.WriteLine(e.Message);
}
}
}
}
[Test]
public void JsonXmlReaderSimpleTest() {
var json = "\"some text\"";
//Console.WriteLine($"JSON: {json}");
//Console.WriteLine("XML");
/*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) {
Assert.AreEqual(xmlReader.ReadState, System.Xml.ReadState.Initial);
AssertRead(xmlReader, XmlNodeType.XmlDeclaration);
AssertRead(xmlReader, XmlNodeType.Element);
AssertRead(xmlReader, XmlNodeType.Text);
AssertRead(xmlReader, XmlNodeType.EndElement);
Assert.IsFalse(xmlReader.Read());
}*/
//DumpJsonParse("\"text value\"");
//DumpJsonParse("null");
//DumpJsonParse("true");
//DumpJsonParse("{}");
//DumpJsonParse("[]");
DumpJsonParse("{\"one\":1, \"two\":2}");
cin
JsonXmlReader performance tuning...
r229 DumpJsonParse("[1,\"\",2,3]");
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 DumpJsonParse("[{\"info\": [7,8,9]}]");
cin
JsonXmlReader performance tuning...
r229 DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]");
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 }
cin
JsonReader optimizations
r236
[Test]
public void JsonBenchmark() {
var t = Environment.TickCount;
using (var reader = new JsonXmlReader(JsonReader.Create("e:\\citylots.json"), new JsonXmlReaderOptions { NamespaceUri = "XmlReaderSimpleTest", RootName = "data" })) {
while (reader.Read()) {
}
}
Console.WriteLine($"JsonXmlReader: {Environment.TickCount - t} ms");
t = Environment.TickCount;
using(var reader = JsonReader.Create("e:\\citylots.json")) {
while(reader.Read()) {
}
}
Console.WriteLine($"JsonReader: {Environment.TickCount - t} ms");
t = Environment.TickCount;
using (var reader = XmlReader.Create("file:///e:\\citylots.xml")) {
while (reader.Read()) {
}
}
Console.WriteLine($"XmlReader: {Environment.TickCount - t} ms");
}
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 void AssertRead(XmlReader reader, XmlNodeType expected) {
Assert.IsTrue(reader.Read());
cin
JsonReader optimizations
r236 Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}");
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 Assert.AreEqual(expected, reader.NodeType);
}
void DumpJsonParse(string json) {
Console.WriteLine($"JSON: {json}");
Console.WriteLine("XML");
cin
JsonXmlReader performance tuning...
r229 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) {
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 while (xmlReader.Read())
Console.WriteLine($"{new string(' ', xmlReader.Depth * 2)}{xmlReader}");
}
}
void DumpJsonFlatParse(string json) {
Console.WriteLine($"JSON: {json}");
Console.WriteLine("XML");
using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings {
Indent = true,
CloseOutput = false,
ConformanceLevel = ConformanceLevel.Document
}))
cin
JsonXmlReader performance tuning...
r229 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) {
cin
Reimplemented JsonXmlReader, added support for null values: JSON null values are...
r227 xmlWriter.WriteNode(xmlReader, false);
}
}
}
}