JsonTests.cs
191 lines
| 8.1 KiB
| text/x-csharp
|
CSharpLexer
/ Implab.Test / JsonTests.cs
cin
|
r262 | using Xunit; | ||
using System; | ||||
using Implab.Automaton; | ||||
using Implab.Xml; | ||||
using System.Xml; | ||||
using Implab.Formats; | ||||
using Implab.Formats.Json; | ||||
using System.IO; | ||||
cin
|
r264 | using Implab.Test.Model; | ||
cin
|
r262 | |||
namespace Implab.Test { | ||||
public class JsonTests { | ||||
[Fact] | ||||
public void TestScannerValidTokens() { | ||||
using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { | ||||
Tuple<JsonTokenType, object>[] expexted = { | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.Number, "9123"), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-123"), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0"), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0.1"), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.2"), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.1e3"), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.Number, "1.3E-3"), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), | ||||
new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"), | ||||
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) | ||||
}; | ||||
string value; | ||||
JsonTokenType tokenType; | ||||
for (var i = 0; i < expexted.Length; i++) { | ||||
Assert.True(scanner.ReadToken(out value, out tokenType)); | ||||
Assert.Equal(expexted[i].Item1, tokenType); | ||||
Assert.Equal(expexted[i].Item2, value); | ||||
} | ||||
Assert.False(scanner.ReadToken(out value, out tokenType)); | ||||
} | ||||
} | ||||
[Fact] | ||||
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) { | ||||
using (var scanner = JsonStringScanner.Create(json)) { | ||||
try { | ||||
string value; | ||||
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.True(false, $"Token '{json}' shouldn't pass"); | ||||
} catch (ParserException e) { | ||||
Console.WriteLine(e.Message); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
[Fact] | ||||
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}"); | ||||
DumpJsonParse("[1,\"\",2,3]"); | ||||
DumpJsonParse("[{\"info\": [7,8,9]}]"); | ||||
DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); | ||||
} | ||||
cin
|
r264 | [Fact] | ||
public void XmlToJsonTransform() { | ||||
var person = new Person { | ||||
FirstName = "Charlie", | ||||
LastName = "Brown", | ||||
Age = 19, | ||||
AgeSpecified = true | ||||
}; | ||||
var doc = SerializationHelpers.SerializeAsXmlDocument(person); | ||||
using (var writer = new StringWriter()) { | ||||
XmlToJson.Default.Transform(doc,null, writer); | ||||
Console.WriteLine(writer.ToString()); | ||||
} | ||||
} | ||||
[Fact] | ||||
public void JsonSerialization() { | ||||
var person = new Person { | ||||
FirstName = "Charlie", | ||||
LastName = "Brown", | ||||
Age = 19, | ||||
AgeSpecified = true, | ||||
Tags = new [] { "brave", "stupid" } | ||||
}; | ||||
var data = SerializationHelpers.SerializeJsonAsString(person); | ||||
Console.WriteLine(data); | ||||
var clone = SerializationHelpers.DeserializeJsonFromString<Person>(data); | ||||
Assert.Equal(person.FirstName, clone.FirstName); | ||||
Assert.Equal(person.LastName, clone.LastName); | ||||
Assert.Equal(person.Age, clone.Age); | ||||
Assert.Equal(person.AgeSpecified, clone.AgeSpecified); | ||||
Assert.Equal(person.Tags, person.Tags); | ||||
} | ||||
cin
|
r262 | void AssertRead(XmlReader reader, XmlNodeType expected) { | ||
Assert.True(reader.Read()); | ||||
Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}"); | ||||
Assert.Equal(expected, reader.NodeType); | ||||
} | ||||
void DumpJsonParse(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 | ||||
})) | ||||
using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { | ||||
xmlWriter.WriteNode(xmlReader, false); | ||||
} | ||||
Console.WriteLine(); | ||||
} | ||||
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 | ||||
})) | ||||
using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { | ||||
xmlWriter.WriteNode(xmlReader, false); | ||||
} | ||||
Console.WriteLine(); | ||||
} | ||||
} | ||||
} | ||||