# HG changeset patch # User cin # Date 1504918393 -10800 # Node ID 8d5de4eb9c2cd3628fd2b2dc3191e278909da7d9 # Parent 9428ea36838ea946c43c01d27d66521aa2398fd5 Reimplemented JsonXmlReader, added support for null values: JSON null values are mapped to empty nodes with 'xsi:nil' attribute set to 'true' diff -r 9428ea36838e -r 8d5de4eb9c2c .hgignore --- a/.hgignore Fri Aug 25 02:16:35 2017 +0300 +++ b/.hgignore Sat Sep 09 03:53:13 2017 +0300 @@ -18,3 +18,6 @@ Implab.Test/Implab.Format.Test/bin/ Implab.Test/Implab.Format.Test/obj/ *.suo +Implab.Format.Test/bin/ +Implab.Format.Test/obj/ +packages/ diff -r 9428ea36838e -r 8d5de4eb9c2c Implab.Format.Test/Implab.Format.Test.csproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Format.Test/Implab.Format.Test.csproj Sat Sep 09 03:53:13 2017 +0300 @@ -0,0 +1,57 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {4D364996-7ECD-4193-8F90-F223FFEA49DA} + Library + Implab.Format.Test + Implab.Format.Test + v4.5 + 0.2 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + full + true + bin\Release + prompt + 4 + false + + + + ..\packages\NUnit.3.8.1\lib\net45\nunit.framework.dll + True + + + + + + + + + + + {F550F1F8-8746-4AD0-9614-855F4C4B7F05} + Implab + + + + + + + + + \ No newline at end of file diff -r 9428ea36838e -r 8d5de4eb9c2c Implab.Format.Test/JsonTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Format.Test/JsonTests.cs Sat Sep 09 03:53:13 2017 +0300 @@ -0,0 +1,145 @@ +using NUnit.Framework; +using System; +using Implab.Formats.JSON; +using Implab.Automaton; +using Implab.Xml; +using System.Xml; +using System.Text; + +namespace Implab.Format.Test { + [TestFixture] + public class JsonTests { + [Test] + public void TestScannerValidTokens() { + using (var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { + + Tuple[] expexted = { + new Tuple(JsonTokenType.Number, 9123d), + new Tuple(JsonTokenType.ValueSeparator, ", "), + new Tuple(JsonTokenType.Number, -123d), + new Tuple(JsonTokenType.ValueSeparator, ", "), + new Tuple(JsonTokenType.Number, 0d), + new Tuple(JsonTokenType.ValueSeparator, ", "), + new Tuple(JsonTokenType.Number, 0.1d), + new Tuple(JsonTokenType.ValueSeparator, ", "), + new Tuple(JsonTokenType.Number, -0.2d), + new Tuple(JsonTokenType.ValueSeparator, ", "), + new Tuple(JsonTokenType.Number, -0.1e3d), + new Tuple(JsonTokenType.ValueSeparator, ", "), + new Tuple(JsonTokenType.Number, 1.3E-3d), + new Tuple(JsonTokenType.ValueSeparator, ", "), + new Tuple(JsonTokenType.String, "some \t\n text"), + new Tuple(JsonTokenType.ValueSeparator, ", "), + new Tuple(JsonTokenType.Literal, "literal"), + new Tuple(JsonTokenType.BeginArray, " ["), + new Tuple(JsonTokenType.EndArray, "]"), + new Tuple(JsonTokenType.BeginObject, "{"), + new Tuple(JsonTokenType.EndObject, "}"), + new Tuple(JsonTokenType.NameSeparator, ":") + }; + + object value; + 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) { + using (var scanner = new JSONScanner(json)) { + try { + object 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.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}"); + DumpJsonParse("[1,2,3]"); + DumpJsonParse("[{\"info\": [7,8,9]}]"); + DumpJsonFlatParse("[1,2,[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); + } + + void AssertRead(XmlReader reader, XmlNodeType expected) { + Assert.IsTrue(reader.Read()); + Console.WriteLine($"{new string(' ', reader.Depth*2)}{reader}"); + Assert.AreEqual(expected, reader.NodeType); + } + + void DumpJsonParse(string json) { + Console.WriteLine($"JSON: {json}"); + Console.WriteLine("XML"); + using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { + 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 + })) + using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { + xmlWriter.WriteNode(xmlReader, false); + } + } + } +} + diff -r 9428ea36838e -r 8d5de4eb9c2c Implab.Format.Test/packages.config --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Format.Test/packages.config Sat Sep 09 03:53:13 2017 +0300 @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff -r 9428ea36838e -r 8d5de4eb9c2c Implab.Test/Implab.Format.Test/Implab.Format.Test.csproj --- a/Implab.Test/Implab.Format.Test/Implab.Format.Test.csproj Fri Aug 25 02:16:35 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {4D364996-7ECD-4193-8F90-F223FFEA49DA} - Library - Implab.Format.Test - Implab.Format.Test - v4.5 - 0.2 - - - true - full - false - bin\Debug - DEBUG; - prompt - 4 - false - - - full - true - bin\Release - prompt - 4 - false - - - - - ..\..\packages\NUnit.2.6.4\lib\nunit.framework.dll - - - - - - - - - {F550F1F8-8746-4AD0-9614-855F4C4B7F05} - Implab - - - - - - - - - \ No newline at end of file diff -r 9428ea36838e -r 8d5de4eb9c2c Implab.Test/Implab.Format.Test/JsonTests.cs --- a/Implab.Test/Implab.Format.Test/JsonTests.cs Fri Aug 25 02:16:35 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -using NUnit.Framework; -using System; -using Implab.Formats.JSON; -using Implab.Automaton; - -namespace Implab.Format.Test { - [TestFixture] - public class JsonTests { - [Test] - public void TestScannerValidTokens() { - using (var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { - - Tuple[] expexted = { - new Tuple(JsonTokenType.Number, 9123d), - new Tuple(JsonTokenType.ValueSeparator, ", "), - new Tuple(JsonTokenType.Number, -123d), - new Tuple(JsonTokenType.ValueSeparator, ", "), - new Tuple(JsonTokenType.Number, 0d), - new Tuple(JsonTokenType.ValueSeparator, ", "), - new Tuple(JsonTokenType.Number, 0.1d), - new Tuple(JsonTokenType.ValueSeparator, ", "), - new Tuple(JsonTokenType.Number, -0.2d), - new Tuple(JsonTokenType.ValueSeparator, ", "), - new Tuple(JsonTokenType.Number, -0.1e3d), - new Tuple(JsonTokenType.ValueSeparator, ", "), - new Tuple(JsonTokenType.Number, 1.3E-3d), - new Tuple(JsonTokenType.ValueSeparator, ", "), - new Tuple(JsonTokenType.String, "some \t\n text"), - new Tuple(JsonTokenType.ValueSeparator, ", "), - new Tuple(JsonTokenType.Literal, "literal"), - new Tuple(JsonTokenType.BeginArray, " ["), - new Tuple(JsonTokenType.EndArray, "]"), - new Tuple(JsonTokenType.BeginObject, "{"), - new Tuple(JsonTokenType.EndObject, "}"), - new Tuple(JsonTokenType.NameSeparator, ":") - }; - - object value; - 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) - using (var scanner = new JSONScanner(json)) { - try { - object 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.Fail("Token '{0}' shouldn't pass", json); - } catch (ParserException e) { - Console.WriteLine(e.Message); - } - } - - } - } -} - diff -r 9428ea36838e -r 8d5de4eb9c2c Implab.Test/Implab.Format.Test/packages.config --- a/Implab.Test/Implab.Format.Test/packages.config Fri Aug 25 02:16:35 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff -r 9428ea36838e -r 8d5de4eb9c2c Implab.Test/Implab.Test.csproj --- a/Implab.Test/Implab.Test.csproj Fri Aug 25 02:16:35 2017 +0300 +++ b/Implab.Test/Implab.Test.csproj Sat Sep 09 03:53:13 2017 +0300 @@ -76,9 +76,7 @@ Implab - - - +